【问题标题】:Packaging scala class on databricks (error: not found: value dbutils)在 databricks 上打包 scala 类(错误:未找到:值 dbutils)
【发布时间】:2019-05-24 15:07:18
【问题描述】:

试图用一个类制作一个包

package x.y.Log


import scala.collection.mutable.ListBuffer
import org.apache.spark.sql.{DataFrame}
import org.apache.spark.sql.functions.{lit, explode, collect_list, struct}
import org.apache.spark.sql.types.{StructField, StructType}
import java.util.Calendar
import java.text.SimpleDateFormat
import org.apache.spark.sql.functions._
import spark.implicits._

class Log{
...
}

在同一个笔记本上一切正常,但是一旦我尝试创建可以在其他笔记本中使用的包,我就会收到错误:

<notebook>:11: error: not found: object spark
import spark.implicits._
       ^
<notebook>:21: error: not found: value dbutils
  val notebookPath = dbutils.notebook.getContext().notebookPath.get
                     ^
<notebook>:22: error: not found: value dbutils
  val userName = dbutils.notebook.getContext.tags("user")
                 ^
<notebook>:23: error: not found: value dbutils
  val userId = dbutils.notebook.getContext.tags("userId")
               ^
<notebook>:41: error: not found: value spark
    var rawMeta =  spark.read.format("json").option("multiLine", true).load("/FileStore/tables/xxx.json")
                   ^
<notebook>:42: error: value $ is not a member of StringContext
    .filter($"Name".isin(readSources))

有人知道如何用这些库打包这个类吗?

【问题讨论】:

    标签: scala apache-spark databricks


    【解决方案1】:

    假设您运行的是 Spark 2.x,则声明 import spark.implicits._ 仅在您的作用域中有 SparkSession 对象时才有效。对象 Implicits 在 SparkSession 对象中定义。此对象扩展了 Spark Link to SparkSession code on Github 先前版本的 SQLImplicits。您可以查看链接进行验证

    package x.y.Log
    
    
    import scala.collection.mutable.ListBuffer
    import org.apache.spark.sql.DataFrame
    import org.apache.spark.sql.functions.{lit, explode, collect_list, struct}
    import org.apache.spark.sql.types.{StructField, StructType}
    import java.util.Calendar
    import java.text.SimpleDateFormat
    import org.apache.spark.sql.functions._
    import org.apache.spark.sql.SparkSession
    
    class Log{
    
      val spark: SparkSession = SparkSession.builder.enableHiveSupport().getOrCreate()
    
      import spark.implicits._
    
      ...[rest of the code below]
    }
    

    【讨论】:

    • 成功了,谢谢。还为 dbutils 问题导入了 import com.databricks.dbutils_v1.DBUtilsHolder.dbutils
    猜你喜欢
    • 2019-05-25
    • 2019-01-04
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    相关资源
    最近更新 更多