大佬教程收集整理的这篇文章主要介绍了如何通过 Python 将 blob 数据移动到 Snowflake,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将数据从 ADLS blob 移动到 snowflake 表。
我可以用 UI 做同样的事情。
UI 遵循的步骤:
生成以下 SAS 令牌:
sp=rl&st=2021-06-01T05:45:37Z&se=2021-06-01T13:45:37Z&spr=https&sv=2020-02-10&sr=c&sig=rYYY4o%2YY3jj%2XXXXXAB%2Bo8ygrtyAVCnPOxomlOc%3D
能够在 snowflake Web UI 中使用上述令牌加载表:
copy into FirsT_LEVEl.MOVIES
from 'azure://adlsedmadifpoc.blob.core.windows.net/airflow-dif/raw-area/'
credentials=(azure_sas_token='sp=rl&st=2021-06-01T05:45:37Z&se=2021-06-01T13:45:37Z&spr=https&sv=2020-02-10&sr=c&sig=rYYY4o%2YY3jj%2XXXXXAB%2Bo8ygrtyAVCnPOxomlOc%3D')
FORCE = TRUE file_format = (type = cSV);
我正在尝试用 Python 做同样的事情:
from azure.storage.blob import BlobserviceClIEnt,generate_blob_sas,BlobSasPermissions
from datetiR_923_11845@e import datetiR_923_11845@e,timedelta
import snowflake.connector
def generate_sas_token(file_name):
sas = generate_blob_sas(account_name="xxxx",account_key="p5V2GELxxxxQ4tVgLdj9inKwwYWlAnYpKtGHAg==",container_name="airflow-dif",blob_name=file_name,permission=BlobSasPermissions(read=TruE),expiry=datetiR_923_11845@e.utcNow() + timedelta(hours=2))
print (sas)
return sas
sas = generate_sas_token("raw-area/movIEsDB.csv")
# Connectio String
conn = snowflake.connector.connect(user='xx',password='xx@123',account='xx.southeast-asia.azure',database='xx')
# Create cursor
cur = conn.cursor()
cur.execute(
f"copy into FirsT_LEVEl.MOVIES FROM 'azure://xxx.blob.core.windows.net/airflow-dif/raw-area/movIEsDB.csv' credentials=(azure_sas_token='{sas}') file_format = (type = cSV) ;")
cur.execute(f" Commit ;")
# Execute sql statement
cur.close()
conn.close()
代码中生成的SAS令牌:
se=2021-06-01T07%3A42%3A11Z&sp=rt&sv=2020-06-12&sr=b&sig=ZhZMPSI%yyyyAPTqqE0%3D
我在通过 python 生成 sas 令牌时无法使用 List 权限。
我面临以下错误:
cursor=cursor,snowflake.connector.errors.programR_923_11845@ingError: 091003 (22000): Failure using stage area. Cause: [Server Failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. (Status Code: 403; Error Code: AuthenticationFailed)]
我将来可能会在该文件夹中包含 csv 文件列表。
任何帮助表示赞赏。谢谢。
以下代码有效:
from azure.storage.blob import generate_container_sas,ContainerSasPermissions
from datetiR_923_11845@e import datetiR_923_11845@e,timedelta
import snowflake.connector
def get_sas_token():
container_sas_token = generate_container_sas(
account_name = 'XX',account_key = 'p5V2GEL3AqGuPMMYXXXQ4tVgLdj9inKwwYWlAnYpKtGHAg==',container_name = 'airflow-dif',permission=ContainerSasPermissions(read=True,list=TruE),expiry=datetiR_923_11845@e.utcnow() + timedelta(hours=1)
)
print (container_sas_token)
return container_sas_token
sas = get_sas_token()
# Connectio String
conn = snowflake.connector.connect(user='XX',password='XX@123',account='XX.southeast-asia.azure',database='XX')
# Create cursor
cur = conn.cursor()
cur.execute(
f"copy into FIRST_LEVEl.MOVIES FROM 'azure://XX.blob.core.windows.net/airflow-dif/raw-area/' credentials=(azure_sas_token='{sas}') FORCE = TRUE file_format = (type = cSV) ;")
print (cur.fetchone())
cur.execute(f" Commit ;")
# Execute SQL statement
cur.close()
conn.close()
感谢 Gaurav 的投入。
以上是大佬教程为你收集整理的如何通过 Python 将 blob 数据移动到 Snowflake全部内容,希望文章能够帮你解决如何通过 Python 将 blob 数据移动到 Snowflake所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。