【问题标题】:Unload multiple files from Redshift to S3将多个文件从 Redshift 卸载到 S3
【发布时间】:2018-03-22 03:32:07
【问题描述】:

您好,我正在尝试将多个表从 Redshift 卸载到特定的 S3 存储桶,但出现以下错误:

 psycopg2.InternalError: Specified unload destination on S3 is not empty. Consider using a different bucket / prefix, manually removing the target files in S3, or using the ALLOWOVERWRITE option.

如果我在 unload_function 上添加 'allowoverwrite' 选项,它会在表之前覆盖并卸载 S3 中的最后一个表。

这是我给出的代码:

import psycopg2

def unload_data(r_conn, aws_iam_role, datastoring_path, region, table_name):
     unload = '''unload ('select * from {}')
                    to '{}'
                    credentials 'aws_iam_role={}'
                    manifest
                    gzip
                    delimiter ',' addquotes escape parallel off '''.format(table_name, datastoring_path, aws_iam_role)

     print ("Exporting table to datastoring_path")
     cur = r_conn.cursor()
     cur.execute(unload)
     r_conn.commit()

def main():
     host_rs = 'dataingestion.*********.us******2.redshift.amazonaws.com'
     port_rs = '5439'
     database_rs = '******'
     user_rs = '******'
     password_rs = '********'
     rs_tables = [ 'Employee', 'Employe_details' ]

     iam_role = 'arn:aws:iam::************:role/RedshiftCopyUnload'
     s3_datastoring_path = 's3://mysamplebuck/'
     s3_region = 'us_*****_2'
     print ("Exporting from source")
     src_conn = psycopg2.connect(host = host_rs,
                                 port = port_rs,
                                 database = database_rs,
                                 user = user_rs,
                                 password = password_rs)
     print ("Connected to RS")

     for i, tabe in enumerate(rs_tables):
          if tabe[0] == tabe[-1]:
              print("No files to read!")
          unload_data(src_conn, aws_iam_role = iam_role, datastoring_path = s3_datastoring_path, region = s3_region, table_name = rs_tables[i])
          print (rs_tables[i])


if __name__=="__main__":
main()

【问题讨论】:

  • 您说使用“allowoverwrite”选项时出现问题,但我并没有真正理解您的意思 - 请您能更好/不同地解释一下吗?
  • 感谢您的回复。如果我在卸载变量中添加“allowoverwrite”,如下所示:unload = '''unload ('select * from {}') to '{}' credentials 'aws_iam_role={}' manifest gzip delimiter ',' addquotes escape allowoverwrite '' '.format(table_name, datastoring_path, aws_iam_role) 所有表都能够同时写入 s3 存储桶,并被下一个表覆盖。最后我可以看到 s3 存储桶中的最后一张表。

标签: python-3.x amazon-s3 amazon-redshift amazon-redshift-spectrum


【解决方案1】:

抱怨您将数据保存到同一目的地。

这就像将您计算机上的所有文件复制到同一目录 - 将有文件被覆盖。

您应该将datastoring_path 更改为每个表都不同,例如:

.format(table_name, datastoring_path + '/' + table_name, aws_iam_role)

【讨论】:

  • 非常感谢。我还想为每个表命名,但我是 python 编码的新手,所以我做不到。您的回答给出了确切的解决方案。
猜你喜欢
  • 2014-03-12
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 2018-07-18
  • 2014-09-01
  • 2021-08-04
相关资源
最近更新 更多