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

Load data into Redshift using Node.js

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

这应该很简单,但我找不到任何有效加载的具体示例。

这样做的一种方法是使用 AWS node.js SDK (there's an example in the documentation), then use node-pg to COPY 将数据加载到 S3 中,将数据加载到 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();
      });
    });

请注意,您不需要任何特殊的驱动程序即可 运行。