在ADF中将SQL表数据转换为Excel文件



我正在Azure数据工厂中创建一个工作流,我想在以下任何一个场景中使用SQL表(Azure SQL server)中的数据创建一个excel文件:

  1. 创建excel并上传到blob存储
  2. 创建excel并上传到sharepoint。但我无法在复制活动sink中找到excel连接,用于将数据复制到blob中。有什么办法吗?请建议。

ADF中没有开箱即用的功能来支持Excel作为接收器。你必须使用逻辑应用程序或通过Azure功能,Databricks, Azure批处理等编写自己的自定义逻辑来完成上述任务

在SQL Server机器学习服务中编写一个SQL存储过程Python(仅在配置SQL Server机器学习服务时有效)。然后,ADF可以运行存储过程并将文件放入blob存储中。Blob存储Python库在SQL Managed Instance上不可用,所以下面的脚本使用Blob REST API代替。

EXECUTE sp_execute_external_script @language = N'Python'
,@input_data_1 = N'SELECT A.* FROM MyTable A'
,@input_data_1_name = N'SQLQueryOutput' 
,@script = N'
import pyodbc
import pandas as pd
import requests

# Execute the query to retrieve the data from the SQL Server table
df = SQLQueryOutput
# Write the data to an Excel file
df.to_excel(FileName, index=False)
url = "https://" + AccountName + ".blob.core.windows.net/" + BlobContainerPath + "/" + FileName + "?" + BlobSASToken
payload= open(FileName, "rb")
headers = {
''x-ms-blob-type'': ''BlockBlob'',
''Content-Type'': ''application/vnd.openxmlformats-officedocument.spreadsheetml.sheet''
}
response = requests.request("PUT", url, headers=headers, data=payload)
print(response.text)
'
,@params = N'@BlobSASToken VARCHAR(200), @AccountName VARCHAR(100), @BlobContainerPath VARCHAR(200), @FileName VARCHAR(100)'
,@BlobSASToken = N'sp=xxxxxxxxxxxxx&st=xxxxxxxxxxxx&se=xxxxxxxxxxxx&spr=https&sv=xxxxxxxxxxxxxxx&sr=c&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
,@AccountName = N'myaccountname'
,@BlobContainerPath = N'containername/containerpath'
,@FileName = N'myexcelfilename.xlsx'

最新更新