Cha*_* Mu 6 apache-spark apache-spark-sql snowflake-cloud-data-platform databricks
我写这篇文章不是为了问问题,而是分享知识。我正在使用 Spark 连接到雪花。但我无法访问雪花。数据块中的内部 JDBC 驱动程序似乎有问题。
这是我得到的错误。
java.lang.NoClassDefFoundError:net/snowflake/client/jdbc/internal/snowflake/common/core/S3FileEncryptionMaterial
Run Code Online (Sandbox Code Playgroud)
我尝试了许多版本的雪花 jdbc 驱动程序和雪花驱动程序。好像我可以匹配到正确的。
提问者给出的答案(我只是从问题中提取它以获得更好的网站可用性:
第 1 步:使用 Spark 版本 - 2.3.0 创建集群。和 Scala 版本 - 2.11
第 2 步:将 snowflake-jdbc-3.5.4.jar 附加到集群。
https://mvnrepository.com/artifact/net.snowflake/snowflake-jdbc/3.5.4
第 3 步:将 spark-snowflake_2.11-2.3.2 驱动程序附加到集群。
https://mvnrepository.com/artifact/net.snowflake/spark-snowflake_2.11/2.3.2
这是示例代码。
val SNOWFLAKE_SOURCE_NAME = "net.snowflake.spark.snowflake"
val sfOptions = Map(
"sfURL" -> "<snowflake_url>",
"sfAccount" -> "<your account name>",
"sfUser" -> "<your account user>",
"sfPassword" -> "<your account pwd>",
"sfDatabase" -> "<your database name>",
"sfSchema" -> "<your schema name>",
"sfWarehouse" -> "<your warehouse name>",
"sfRole" -> "<your account role>",
"region_id"-> "<your region name, if you are out of us region>"
)
val df: DataFrame = sqlContext.read
.format(SNOWFLAKE_SOURCE_NAME)
.options(sfOptions)
.option("dbtable", "<your table>")
.load()
Run Code Online (Sandbox Code Playgroud)
如果您使用 Databricks,则有一个由 Databricks 和 Snowflake 人员联合创建的Databricks Snowflake 连接器。您只需提供一些项目即可创建 Spark 数据框(请参见下文 - 从 Databricks 文档复制)。
# snowflake connection options
options = dict(sfUrl="<URL for your Snowflake account>",
sfUser=user,
sfPassword=password,
sfDatabase="<The database to use for the session after connecting>",
sfSchema="<The schema to use for the session after connecting>",
sfWarehouse="<The default virtual warehouse to use for the session after connecting>")
df = spark.read \
.format("snowflake") \
.options(**options) \
.option("dbtable", "<The name of the table to be read>") \
.load()
display(df)
Run Code Online (Sandbox Code Playgroud)
只要您使用正确授予的所有访问权限来访问自己的数据库,即使在我们第一次尝试期间,也只需几分钟。
祝你好运!
| 归档时间: |
|
| 查看次数: |
2745 次 |
| 最近记录: |