Use the below code snippet from a notebook
%scala
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
/**
* For details on what this query means, checkout https://dev.mysql.com/doc/refman/8.0/en/processlist-table.html
**/
def printConnections: Unit = {
val metastoreURL = spark.sparkContext.hadoopConfiguration.get("javax.jdo.option.ConnectionURL")
val metastoreUser = spark.sparkContext.hadoopConfiguration.get("javax.jdo.option.ConnectionUserName")
val metastorePassword = spark.sparkContext.hadoopConfiguration.get("javax.jdo.option.ConnectionPassword")
val connection = DriverManager.getConnection(metastoreURL, metastoreUser, metastorePassword)
val statement = connection.createStatement()
val resultSet = statement.executeQuery("SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST ORDER BY Host")
val rsmd = resultSet.getMetaData();
val columnsNumber = rsmd.getColumnCount();
(1 to columnsNumber).foreach { i =>
print(rsmd.getColumnName(i) + "\t\t\t\t\t\t\t")
}
println();
while (resultSet.next()) {
var cumulativeLength = 0
(1 to columnsNumber).foreach { i =>
val data = if (resultSet.getString(i) != null) resultSet.getString(i).trim() else ""
print(data + "\t\t\t\t\t\t");
}
println();
}
statement.close
connection.close
}
printConnections