How should records be formatted for AWS Kinesis Firehose to Redshift?
Asked Answered
E

2

6

Consider the following:

  • A table in Redshift called 'people' that has fields id, name and age
  • A kinesis firehose stream called 'people' that is configured to write to the 'people' table and the value for 'Redshift table columns' is 'id,name,age'

It's not clear how to format the 'Data' blob. Here's an example of what the code looks like with the data separated by tabs:

let AWS = require('aws-sdk');
let firehose = new AWS.Firehose();
let params = {
  DeliveryStreamName: 'people',
  // id,name,age
  Records: [{Data: '4ccf6d3a-acdf-11e5-ad54-28cfe91fa8f1\tBob\tSmith'}]
};
firehose.putRecordBatch(params, (err, result) => {
  console.log(err || result);
});

Here are some of the docs I have checked:

Earthen answered 27/12, 2015 at 21:29 Comment(0)
E
8

The answer is here:

http://docs.aws.amazon.com/redshift/latest/dg/copy-parameters-data-format.html

Fields need to be pipe '|' separated by default. Rows should be separated by new lines.

Updated corrected code:

let AWS = require('aws-sdk');
let firehose = new AWS.Firehose();
let params = {
  DeliveryStreamName: 'people',
  // id,name,age
  Records: [{Data: '4ccf6d3a-acdf-11e5-ad54-28cfe91fa8f1|Bob|Smith\n'}]
};
firehose.putRecordBatch(params, (err, result) => {
  console.log(err || result);
});
Earthen answered 28/12, 2015 at 0:11 Comment(1)
Thanks for this, not having the \n at the end of the line was causing me issues!Endless
T
3

You can also send straight JSON as long as you properly escape things and use the right COPY options. See COPY FROM JSON and JSON AS. If you use the 'auto' option for JSON AS then it's pretty flexible. If you send multiple dictionaries in one putRecordBatch, do NOT separate them with space or newlines, you can just send

{ key1: "value1", key2: 34 }{ key1: "value2", key2: 45 }
Tishatishri answered 7/7, 2016 at 18:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.