如何使用 Azure Synapse 在 Databricks 上删除表或删除行?
Posted
技术标签:
【中文标题】如何使用 Azure Synapse 在 Databricks 上删除表或删除行?【英文标题】:How to drop table or drop row on Databricks with Azure Synapse? 【发布时间】:2021-08-22 02:35:44 【问题描述】:我想使用 DROP TABLE
删除我的表,但我的 Databricks 上出现错误 JDBC,但如果我使用 SELECT
,它不会出现任何错误,我有 2 个不同的函数可以 read_from_synapse
和write_to_synapse
。
这是我的read_from_synapse
,我认为DROP TABLE
可以使用查询
def read_from_synapse(sql, url, tempDir):
df = (spark.read
.format("com.databricks.spark.sqldw")
.option("url", url)
.option("tempDir", tempDir)
.option("forwardSparkAzureStorageCredentials", "true")
.option("query", sql)
.load()
)
return df
这是我的write_to_synapse
可以插入表格的脚本
def write_to_synapse(df, table, write_mode, url, tempDir):
df.write
.format("com.databricks.spark.sqldw")
.option("tableOptions", "CLUSTERED COLUMNSTORE INDEX, DISTRIBUTION = ROUND_ROBIN") # Added at 20200121
.option("url", url)
.option("dbtable", table)
.option("forward_spark_azure_storage_credentials","True")
.option("tempdir", tempDir)
.mode(write_mode)
.save()
更新
我已经尝试过这个链接,但它并没有解决我的问题,它得到了这样的错误
com.databricks.spark.sqldw.SqlDWSideException: Azure Synapse Analytics failed to execute the JDBC query produced by the connector.
这从错误中更详细:
---------------------------------------------------------------------------
Py4JJavaError Traceback (most recent call last)
<command-1485523310718777> in <module>
----> 1 drop_sdf_sales = azSynapse._delete_from_synapse(drop_sql, tempDir=temp_read_dir)
<command-1485523310718391> in _delete_from_synapse(self, sql, url, tempDir)
5 if not tempDir:
6 tempDir = self.azblob._get_blob_path('04-publish', 'sqlDwReadTempDirs')
----> 7 df = UtilAzSynapse.read_from_synapse(sql, url, tempDir)
8 return df
<command-2362013028695578> in read_from_synapse(sql, url, tempDir)
16 .option("tempDir", tempDir)
17 .option("forwardSparkAzureStorageCredentials", "true")
---> 18 .option("query", sql)
19 .load()
20 )
/databricks/spark/python/pyspark/sql/readwriter.py in load(self, path, format, schema, **options)
182 return self._df(self._jreader.load(self._spark._sc._jvm.PythonUtils.toSeq(path)))
183 else:
【问题讨论】:
这能回答你的问题吗? Drop Table in SQL Database from Azure Databricks @Kafels 我认为从你的建议来看它与我的read_from_synapse
脚本有相似之处?因为当我尝试用我的函数DROP TABLE X
时,它得到了一个JDBC错误,但我会先尝试,我会更新给你
如果你的问题是丢表,是的。它有相似之处。我建议您更新从异常添加堆栈跟踪的问题
@Kafels 但我想问一下,是否可以从 Azure Synapse 等数据块中删除外部表?因为当我看到这个documentation 时,它没有解释它可以删除一些表或从中删除一些行?
我认为您应该对 delete
或 drop
采取另一种方法,例如使用 Azure Synapse 提供的适当库。在我看来,spark.read.jdbc
应该只运行 SELECT 查询
【参考方案1】:
使用 Azure Databricks 从 Azure Synapse 表中删除一行:
postActionsSQL = "DELETE from Persons WHERE PersonID=3"
df.write.format("com.databricks.spark.sqldw") \
.option("url", "jdbc:sqlserver://synapse.sql.azuresynapse.net:1433;database=master;user=master@synapse;password=XXXXXXX;encrypt=true;trustServerCertificate=true;hostNameInCertificate=*.sql.azuresynapse.net;loginTimeout=30;") \
.option("tempDir", "wasbs://sampledata@synapse.blob.core.windows.net/temp") \
.option("forwardSparkAzureStorageCredentials", "true") \
.option("dbTable", "Persons") \
.option("postActions",postActionsSQL) \
.mode("overwrite") \
.save()
使用 Azure Databricks 从 Azure Synapse 表中删除表:
postActionsSQL = "DROP TABLE Trip003"
df.write.format("com.databricks.spark.sqldw") \
.option("url", "jdbc:sqlserver://synapse.sql.azuresynapse.net:1433;database=master;user=master@synapse;password=XXXXXXX;encrypt=true;trustServerCertificate=true;hostNameInCertificate=*.sql.azuresynapse.net;loginTimeout=30;") \
.option("tempDir", "wasbs://sampledata@synapse.blob.core.windows.net/temp") \
.option("forwardSparkAzureStorageCredentials", "true") \
.option("dbTable", "Trip003") \
.option("postActions",postActionsSQL) \
.mode("overwrite") \
.save()
【讨论】:
感谢您的回答,但是当我尝试您的脚本时,出现错误AttributeError: 'DataFrame' object has no attribute 'write'
,为什么它需要像 write_to_synapse
函数这样的要求?
确保在编写之前声明数据框。最初我使用df = spark.read \ .format("com.databricks.spark.sqldw") \ .option("url", "jdbc:sqlserver://synapse.sql.azuresynapse.net:1433;database=chepra;user=chepra@synapse;password=XXXXX;encrypt=true;trustServerCertificate=false;hostNameInCertificate=cheprasynapse.sql.azuresynapse.net;loginTimeout=30;") \ .option("tempDir", "wasbs://sampledata@chepra.blob.core.windows.net/temp") \ .option("forwardSparkAzureStorageCredentials", "true") \ .option("dbTable", "trip") \ .load()
在带有com.microsoft.sqlserver.jdbc.SQLServerDriver
的“常规”Azure SQL 上这不起作用。错误是:“不允许创建表作为选择”。错误信息是垃圾,我有一个DROP TABLE
-clause。以上是关于如何使用 Azure Synapse 在 Databricks 上删除表或删除行?的主要内容,如果未能解决你的问题,请参考以下文章
Copy data from and to Salesforce using Azure Data Factory or Azure Synapse Analytics
Azure Synapse Analysis 开箱 Blog - 伍 -- Data Factory Data Pipeline Automation
如何使用 Azure Synapse 在 Databricks 上删除表或删除行?
如何在 Synapse (Azure SQL DW) 上检索视图定义?