jeudi 27 août 2020

How to call Databricks dbutils using Scala or Java Reflection

Databricks does not offer the dbutils api for Scala 2.12

To build a jar using dbutils - that is still available during runtime - I want to call

com.databricks.dbutils_v1.DBUtilsHolder.dbutils.secrets.get(scope = "myScope", key = "myKey")

using reflection.

Just calling the get method by reflection works fine:

val secretReader = com.databricks.dbutils_v1.DBUtilsHolder.dbutils.secrets
val l = secretReader.getClass.getMethod("get", s.getClass, k.getClass)
l.invoke(secretReader, s, k).asInstanceOf[String]

returning "myFancySecret"

But trying to use reflection to call the object dbutils.secrets by reflection fails.

My current version looks like this:

val s = "myScope"
val k = "myKey"
val su = Class.forName("com.databricks.dbutils_v1.SecretUtils$").getField("MODULE$").get(null)
val l = su.getClass.getMethod("get", s.getClass, k.getClass)
l.invoke(su, s, k).asInstanceOf[String]

resulting in

java.lang.NoSuchMethodException: com.databricks.dbutils_v1.SecretUtils$.get(java.lang.String, java.lang.String)





Aucun commentaire:

Enregistrer un commentaire