首页 > 解决方案 > 使用 Synapse Analytics 将数据帧写入 SQL 专用数据库

问题描述

我想从我的 Azure Data Lake Storage Gen2 加载一个数据帧,并将其写入我在 Synapse 中创建的 SQL 专用数据库。

这就是我所做的:

df = spark.read.format("delta").load(BronzePath)
df.write.format("com.databricks.spark.sqldw").option("url", jdbcUrl).save()

我有以下错误:

java.lang.ClassNotFoundException: Failed to find data source: com.databricks.spark.sqldw.

正在做:

df.write.mode("overwrite").saveAsTable("MyTable")

在 Spark 默认数据库(蓝色十字)中创建表。那不是我需要的。我想将我的表放在专用数据库中(蓝色箭头):

在此处输入图像描述

标签: pysparkapache-spark-sqlazure-synapse

解决方案


如果与本指南不同,请发布更多代码,包括 jdbc url 。我没有看到在 conf 中设置存储密钥的代码,而且您似乎也在使用不同的方式进行保存。


# Otherwise, set up the Blob storage account access key in the notebook session conf.
spark.conf.set(
  "fs.azure.account.key.<your-storage-account-name>.blob.core.windows.net",
  "<your-storage-account-access-key>")

# Get some data from an Azure Synapse table.
df = spark.read \
  .format("com.databricks.spark.sqldw") \
  .option("url", "jdbc:sqlserver://<the-rest-of-the-connection-string>") \
  .option("tempDir", "wasbs://<your-container-name>@<your-storage-account-name>.blob.core.windows.net/<your-directory-name>") \
  .option("forwardSparkAzureStorageCredentials", "true") \
  .option("dbTable", "<your-table-name>") \
  .load()

# Load data from an Azure Synapse query.
df = spark.read \
  .format("com.databricks.spark.sqldw") \
  .option("url", "jdbc:sqlserver://<the-rest-of-the-connection-string>") \
  .option("tempDir", "wasbs://<your-container-name>@<your-storage-account-name>.blob.core.windows.net/<your-directory-name>") \
  .option("forwardSparkAzureStorageCredentials", "true") \
  .option("query", "select x, count(*) as cnt from table group by x") \
  .load()

# Apply some transformations to the data, then use the
# Data Source API to write the data back to another table in Azure Synapse.

df.write \
  .format("com.databricks.spark.sqldw") \
  .option("url", "jdbc:sqlserver://<the-rest-of-the-connection-string>") \
  .option("forwardSparkAzureStorageCredentials", "true") \
  .option("dbTable", "<your-table-name>") \
  .option("tempDir", "wasbs://<your-container-name>@<your-storage-account-name>.blob.core.windows.net/<your-directory-name>") \
  .save()

另请阅读


在 Spark 默认数据库(蓝色十字)中创建表。那不是我需要的。我想将我的表放在专用数据库中(蓝色箭头):

如此处所述 “Spark 将为您创建一个默认的本地 Hive 元存储(使用 Derby)。”

所以当你不给它 path/jdbcurl ( df.write.mode("overwrite").saveAsTable("MyTable")) 时,它会保存到本地 Hive。


推荐阅读