-
Notifications
You must be signed in to change notification settings - Fork 152
/
dynamoDBtoCSV.js
274 lines (241 loc) · 7.64 KB
/
dynamoDBtoCSV.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
const program = require("commander");
const AWS = require("aws-sdk");
const unmarshal = require("dynamodb-marshaler").unmarshal;
const Papa = require("papaparse");
const fs = require("fs");
let headers = [];
let unMarshalledArray = [];
program
.version("0.1.1")
.option("-t, --table [tablename]", "Add the table you want to output to csv")
.option("-i, --index [indexname]", "Add the index you want to output to csv")
.option("-k, --keyExpression [keyExpression]", "The name of the partition key to filter results on")
.option("-v, --keyExpressionValues [keyExpressionValues]", "The key value expression for keyExpression. See: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html")
.option("-c, --count", "Only get count, requires -k flag")
.option("-a, --stats [fieldname]", "Gets the count of all occurances by a specific field name (only string fields are supported")
.option("-d, --describe", "Describe the table")
.option("-S, --select [select]", "Select specific fields")
.option("-r, --region [regionname]")
.option(
"-e, --endpoint [url]",
"Endpoint URL, can be used to dump from local DynamoDB"
)
.option("-p, --profile [profile]", "Use profile from your credentials file")
.option("-m, --mfa [mfacode]", "Add an MFA code to access profiles that require mfa.")
.option("-f, --file [file]", "Name of the file to be created")
.option(
"-ec --envcreds",
"Load AWS Credentials using AWS Credential Provider Chain"
)
.option("-s, --size [size]", "Number of lines to read before writing.", 5000)
.parse(process.argv);
const options = program.opts();
if (!options.table) {
console.log("You must specify a table");
program.outputHelp();
process.exit(1);
}
if (options.region && AWS.config.credentials) {
AWS.config.update({ region: options.region });
} else {
AWS.config.loadFromPath(__dirname + "/config.json");
}
if (options.endpoint) {
AWS.config.update({ endpoint: options.endpoint });
}
if (options.profile) {
let newCreds = new AWS.SharedIniFileCredentials({ profile: options.profile });
newCreds.profile = options.profile;
AWS.config.update({ credentials: newCreds });
}
if (options.envcreds) {
let newCreds = AWS.config.credentials;
newCreds.profile = options.profile;
AWS.config.update({
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
},
region: process.env.AWS_DEFAULT_REGION
});
}
if (options.mfa && options.profile) {
const creds = new AWS.SharedIniFileCredentials({
tokenCodeFn: (serial, cb) => {cb(null, options.mfa)},
profile: options.profile
});
// Update config to include MFA
AWS.config.update({ credentials: creds });
} else if (options.mfa && !options.profile) {
console.log('error: MFA requires a profile(-p [profile]) to work');
process.exit(1);
}
const dynamoDB = new AWS.DynamoDB();
const query = {
TableName: options.table,
IndexName: options.index,
Select: options.count ? "COUNT" : (options.select ? "SPECIFIC_ATTRIBUTES" : (options.index ? "ALL_PROJECTED_ATTRIBUTES" : "ALL_ATTRIBUTES")),
KeyConditionExpression: options.keyExpression,
ExpressionAttributeValues: JSON.parse(options.keyExpressionValues),
ProjectionExpression: options.select,
Limit: 1000
};
const scanQuery = {
TableName: options.table,
IndexName: options.index,
ProjectionExpression: options.select,
Limit: 1000
};
// if there is a target file, open a write stream
if (!options.describe && options.file) {
var stream = fs.createWriteStream(options.file, { flags: 'a' });
}
let rowCount = 0;
let writeCount = 0;
let writeChunk = options.size;
const describeTable = () => {
dynamoDB.describeTable(
{
TableName: options.table
},
function (err, data) {
if (!err) {
console.dir(data.Table);
} else console.dir(err);
}
);
};
const scanDynamoDB = (query) => {
dynamoDB.scan(query, function (err, data) {
if (!err) {
unMarshalIntoArray(data.Items); // Print out the subset of results.
if (data.LastEvaluatedKey) {
// Result is incomplete; there is more to come.
query.ExclusiveStartKey = data.LastEvaluatedKey;
if (rowCount >= writeChunk) {
// once the designated number of items has been read, write out to stream.
unparseData(data.LastEvaluatedKey);
}
scanDynamoDB(query);
} else {
unparseData("File Written");
}
} else {
console.dir(err);
}
});
};
const appendStats = (params, items) => {
for (let i = 0; i < items.length; i++) {
let item = items[i];
let key = item[options.stats].S;
if (params.stats[key]) {
params.stats[key]++;
} else {
params.stats[key] = 1;
}
rowCount++;
}
}
const printStats = (stats) => {
if (stats) {
console.log("\nSTATS\n----------");
Object.keys(stats).forEach((key) => {
console.log(key + " = " + stats[key]);
});
writeCount += rowCount;
rowCount = 0;
}
}
const processStats = (params, data) => {
let query = params.query;
appendStats(params, data.Items);
if (data.LastEvaluatedKey) {
// Result is incomplete; there is more to come.
query.ExclusiveStartKey = data.LastEvaluatedKey;
if (rowCount >= writeChunk) {
// once the designated number of items has been read, print the final count.
printStats(params.stats);
}
queryDynamoDB(params);
}
};
const processRows = (params, data) => {
let query = params.query;
unMarshalIntoArray(data.Items); // Print out the subset of results.
if (data.LastEvaluatedKey) {
// Result is incomplete; there is more to come.
query.ExclusiveStartKey = data.LastEvaluatedKey;
if (rowCount >= writeChunk) {
// once the designated number of items has been read, write out to stream.
unparseData(data.LastEvaluatedKey);
}
queryDynamoDB(params);
} else {
unparseData("File Written");
}
};
const queryDynamoDB = (params) => {
let query = params.query;
dynamoDB.query(query, function (err, data) {
if (!err) {
if (options.stats) {
processStats(params, data);
} else {
processRows(params, data);
}
} else {
console.dir(err);
}
});
};
const unparseData = (lastEvaluatedKey) => {
var endData = Papa.unparse({
fields: [...headers],
data: unMarshalledArray
});
if (writeCount > 0) {
// remove column names after first write chunk.
endData = endData.replace(/(.*\r\n)/, "");;
}
if (options.file) {
writeData(endData);
} else {
console.log(endData);
}
// Print last evaluated key so process can be continued after stop.
console.log("last key:");
console.log(lastEvaluatedKey);
// reset write array. saves memory
unMarshalledArray = [];
writeCount += rowCount;
rowCount = 0;
}
const writeData = (data) => {
stream.write(data);
};
const unMarshalIntoArray = (items) => {
if (items.length === 0) return;
items.forEach(function (row) {
let newRow = {};
// console.log( 'Row: ' + JSON.stringify( row ));
Object.keys(row).forEach(function (key) {
if (headers.indexOf(key.trim()) === -1) {
// console.log( 'putting new key ' + key.trim() + ' into headers ' + headers.toString());
headers.push(key.trim());
}
let newValue = unmarshal(row[key]);
if (typeof newValue === "object") {
newRow[key] = JSON.stringify(newValue);
} else {
newRow[key] = newValue;
}
});
// console.log( newRow );
unMarshalledArray.push(newRow);
rowCount++;
});
}
if (options.describe) describeTable(scanQuery);
if (options.keyExpression) queryDynamoDB({ "query": query, stats: {} });
else scanDynamoDB(scanQuery);