【发布时间】:2021-12-08 23:58:39
【问题描述】:
所以,我有一个在下面创建的表 nba_schedule。当我尝试从 s3 csv 文件复制数据以使用 COPY 插入到表中时,我收到此错误 InternalError_: Cannot COPY into nonexistent table newsletter_schedule。
我认为这是因为这一切都发生在同一个事务中,这是我在这里应该做的。此外,redshift 变量位于一个 env 文件中,我不会共享加载该文件的代码。
redshift_table = 'nba_schedule'
# Connect to redshift
conn_string = "dbname={} port={} user={} password={} host={}".format(
redshift_dbname, redshift_port, redshift_user, redshift_password, redshift_host)
conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
logging.info("Creating newsletter_schedule table in Redshift")
sql = f"""DROP TABLE IF EXISTS {schema + "." + redshift_table}"""
cursor.execute(sql)
sql = f"""CREATE TABLE IF NOT EXISTS {schema + "." + redshift_table} (
Date DATE,
Player_Name VARCHAR(255),
Player_Nickname VARCHAR(13),
Player_No VARCHAR(13),
Points VARCHAR(255),
Rebounds VARCHAR(255),
Assists VARCHAR(255),
Blocks VARCHAR(1),
3PM VARCHAR(1),
3PA VARCHAR(1),
FGM VARCHAR(50),
FGA VARCHAR(255),
three_percent VARCHAR(50),
fg_percent VARCHAR(50)
)
"""
cursor.execute(sql)
sql =f"""
COPY newsletter_schedule
FROM 's3://random_sample_data/nba_redshift/{s3_file}'
CREDENTIALS 'aws_iam_role=arn:aws:iam::4254514352:role/SampleRole'
DELIMITER ','
IGNOREHEADER 1
EMPTYASNULL
QUOTE '"'
CSV
REGION 'us-east-1';
"""
cursor.execute(sql)
conn.commit()
有什么想法吗?
【问题讨论】: