一种可能的解决方法是检查 AWS Glue 与 Snowflake 之间的连接是否正确配置并且可以正常通信。同时,还需要检查 AWS Glue 作业使用的 IAM 角色是否具有适当的访问权限以访问 Snowflake 表。以下是一个示例,展示如何在 AWS Glue 作业中使用 boto3 和 snowflake-connector-python 包连接 Snowflake 并将数据写入表中:
import snowflake.connector
import boto3
import uuid
import os
# Set up the connection properties
connection_properties = {
"account": "myaccount",
"user": "myuser",
"password": "mypassword",
"database": "mydatabase",
"schema": "myschema",
"warehouse": "mywarehouse",
}
# Set up the AWS credentials
session = boto3.Session()
credentials = session.get_credentials()
aws_access_key_id = credentials.access_key
aws_secret_access_key = credentials.secret_key
# Add the AWS keys to the Snowflake connection properties
connection_properties['aws_access_key_id'] = aws_access_key_id
connection_properties['aws_secret_access_key'] = aws_secret_access_key
try:
# Connect to Snowflake
conn = snowflake.connector.connect(**connection_properties)
cursor = conn.cursor()
# Create a unique temporary table name
temp_table_name = "temp_" + str(uuid.uuid4()).replace("-", "")
# Write data to the temporary table
cursor.execute("CREATE TABLE " + temp_table_name + " (col1 string, col2 int)")
cursor.execute("INSERT INTO " + temp_table_name + " VALUES ('foo', 1), ('bar', 2)")
# Copy data from the temporary table to a target table
cursor.execute(f"COPY INTO final_table FROM {temp_table_name}")
# Clean up the temporary table
cursor.execute(f"DROP TABLE IF EXISTS {temp_table_name}")
# Commit the changes
conn.commit()
except Exception as e:
print("Error while writing to Snowflake table:", e)
conn.rollback()
finally:
# Close the connection
conn.close()
在上