0
votes

I have a relatively large MongoDB collection that I'm migrating into Redshift. It's ~600mm documents, so I want the copy to be as efficient as possible. The problem is, I have an array field in my Mongo collection, but I'd like to insert each value from the array into separate rows in Redshift.

Mongo:

{
  id: 123,
  names: ["market", "fashion", "food"]
}

In Redshift, I want columns for "id" and "names", where the primary key is (id, name). So I should get 3 new Redshift rows from that one mongo document.

Is it possible to do that with a Redshift COPY command? I can export my data as either a csv or json into s3, but I don't want to have to do any additional processing on the data due to how long it takes to do that many documents.

2

2 Answers

0
votes

You can probably do it on COPY with triggers, but it'd be quite awkward and the performance would be miserable (since you can't just transform the row and would need to do INSERTs from the trigger function).

It's a trivial transform, though, why not just pass it through any scripting language on export?

You can also import as-is, and transform afterwards (should be pretty fast on Redshift):

CREATE TABLE mydata_load (
  id    int4,
  names text[]
);

do the copy

CREATE TABLE mydata AS SELECT id, unnest(names) as name FROM mydata_load;
0
votes

Redshift does not have support for Arrays as PostgreSQL does, so you cannot just insert the data as is.

However, MongoDB has a simple aggregate function which allows you to unwind arrays exactly as you want - by using the other columns as the keys. So I'd export the result of that into a JSON, and then store it into Redshift using JSONPaths.