使用Node.js将数据加载到Redshift中

时间:2015-06-07 14:30:30

标签: node.js postgresql amazon-redshift

使用node.js将数据插入Amazon Redshift有哪些方法?

这应该非常简单,但我无法找到任何有效加载的具体示例。

1 个答案:

答案 0 :(得分:16)

这样做的一种方法是使用AWS node.js SDK(文档中有example)将数据加载到S3中,然后使用node-pg来{{3将数据导入Redshift:

var pg = require('pg');

var conString = "postgres://user:password@db-endpoint:port/schema";

var client = new pg.Client(conString);
    client.connect(function(err) {
      if(err) {
        return console.error('could not connect to postgres', err);
      }

      //assuming credentials are exported as enviornment variables, 
      //both CLI- and S3cmd-style are supported here.
      //Also, you may want to specify the file's format (e.g. CSV), 
      //max errors, etc.
      var copyCmd = 'copy my_redshift_table from \'s3://your_bucket/your_file\' credentials \'aws_access_key_id=' 
      + (process.env.AWS_ACCESS_KEY || process.env.AWS_ACCESS_KEY_ID)
      + ';aws_secret_access_key=' 
      + (process.env.AWS_SECRET_KEY || process.env.AWS_SECRET_ACCESS_KEY)
      + '\'';

      client.query(copyCmd, function(err, result) {
        if(err) {
          return console.error('error running query', err);
        }
        logger.info("redhshift load: no errors, seem to be successful!");
        client.end();
      });
    });

请注意,您无需运行任何特殊驱动程序。