gpt4 book ai didi

scala - Azure Databricks Notebook 在包中时无法找到 "dbutils"

转载 作者:行者123 更新时间:2023-12-03 21:15:56 26 4
gpt4 key购买 nike

我正在创建一个类用于与 azure 存储 blob 进行通信,它工作正常,但如果我尝试将此类放入包中,则会出现错误“错误:未找到:值 dbutils” 。如果我删除类上方的“package Libraries.Custom”,它就可以正常工作。

我正在使用 Scala 在 azure databricks 笔记本中创建一个类。有人可以帮我吗。代码如下:

类代码

package Libraries.Custom

import org.apache.spark.sql._

class BlobContext {
// Basic Azure Storage Configurations
val blob_account_name = "REPLACE_BY_ACTUAL_VALUE"
val blob_account_access_key = "REPLACE_BY_ACTUAL_VALUE"
val blob_container_name = "REPLACE_BY_ACTUAL_VALUE"
val blob_server = s"REPLACE_BY_ACTUAL_VALUE"
val blob_wasbs = s"REPLACE_BY_ACTUAL_VALUE"

val spark = SparkSession
.builder()
.appName("Path SOM")
.master("local[*]")
.config("spark.ui.enabled", "false")
.getOrCreate()

def SetConfigurations {
spark.conf.set(blob_server, blob_account_access_key)
}

def ReadData(fileName: String, fileType: String): DataFrame = {
SetConfigurations
val dataFrame = spark.read.format(fileType).option("header", "true").load(s"${blob_wasbs}${fileName}.${fileType}")
return dataFrame
}

def WriteData(fileDataFrame: DataFrame, fileName: String, fileType: String) {
val absTempFilePath = s"${blob_wasbs}SPARK_NEW_${fileName}.temp"
val absFilePath = s"${blob_wasbs}SPARK_NEW_${fileName}.${fileType}"

fileDataFrame.repartition(1).write.format(fileType).mode("overwrite").option("header", "true").option("inferSchema", "true").option("delimiter", ",").csv(absTempFilePath)

val partition_path = dbutils.fs.ls(absTempFilePath + "/").filter(file=>file.name.endsWith(".csv"))(0).path
dbutils.fs.cp(partition_path, absFilePath)
dbutils.fs.rm(absTempFilePath,recurse=true)
}
}

错误

<notebook>:37: error: not found: value dbutils
val partition_path = dbutils.fs.ls(absTempFilePath + "/").filter(file=>file.name.endsWith(".csv"))(0).path
^
<notebook>:38: error: not found: value dbutils
dbutils.fs.cp(partition_path, absFilePath)
^
<notebook>:39: error: not found: value dbutils
dbutils.fs.rm(absTempFilePath,recurse=true)
^
<notebook>:39: error: not found: value recurse
dbutils.fs.rm(absTempFilePath,recurse=true)
^
Compilation failed.

最佳答案

尝试添加此导入:

import com.databricks.dbutils_v1.DBUtilsHolder.dbutils

关于scala - Azure Databricks Notebook 在包中时无法找到 "dbutils",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53945565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com