使用copy命令将数据从s3加载到redshift

时间:2016-07-17 09:52:11

标签: arrays json amazon-s3 amazon-redshift

我有一个场景,我需要将数据从Amazon S3加载到Amazong Redshift数据库。

S3中的文件都是JSON,它们位于存储桶中的各种文件夹下。每个文件夹都指出它的生成日期。

例如,这是S3中的示例文件夹结构:

  1. 铲斗(B1)
    A.文件夹(F1)
      一世。 file1.json,file2.json ......等等 B.文件夹(F2)
      II。 file22.json,file23.json ......等等
  2. 每个JSON文件都没有单个根元素。它们具有以下格式 -

      

    file1.json

    {
    Key : Value,
    Key1 : [ some  array],
    Key2 : value3,
    }
    
    {
    Key : Value1,
    Key1 : [ some  array1],
    Key2 : value2,
    }
    

    我想知道是否有办法使用copy命令递归加载这些数据(因为json位于多个文件夹中)到redshift中。

    (OR)

    如果有更好的方法来加载数据而不是使用复制命令。

1 个答案:

答案 0 :(得分:0)

选项1:密钥前缀匹配

在S3中,没有文件夹这样的东西。相反,路径被视为对象键。只要您的密钥遵循通用模式并假设除了您要导入的那些文件之外没有其他文件匹配该模式,例如:

subscription = Payola::Subscription.find_by(stripe_id: event.data.object.subscription)
# this is wrong
subscription.fail_payment_date1 = nil
subscription.fail_payment_date2 = nil
subscription.update

# this is allowed
subscription.fail_payment_date1 = nil
subscription.fail_payment_date2 = nil
subscription.save

# this is also allowed
subscription.update(fail_payment_date1: nil, fail_payment_date2: nil)

然后,以下s3 └── b1-bucket ├── f1/20160728/file1.json ├── f1/20160728/file2.json ├── f1/20160729/file1.json └── f2/20160728/file1.json 命令将匹配(并复制)所有这些文件:

COPY

如上所述in the documentation

  

s3:// copy_from_s3_objectpath参数可以引用单个文件或一组具有相同键前缀的对象或文件夹。

选项2:清单文件

另一种选择是将清单文件添加到COPY your_table FROM 's3://b1-bucket/f' CREDENTIALS '' FORMAT AS JSON 'auto'; 语句中,该语句基本上只是一个包含要导入的文件列表的文件:

COPY

请参阅http://docs.aws.amazon.com/redshift/latest/dg/loading-data-files-using-manifest.html

相关问题