我想将过滤的数据从dynamodb导出到csv。我有一个使用名称和ID过滤数据的功能。现在我需要将过滤后的数据导出到excel ..我该如何解决?我看过这个npm dynamodb-csv。但这不能解决我的问题。
答案 0 :(得分:0)
我正在使用以下代码段进行导出和导入:
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient({ region: 'eu-central-1' });
const READ_FROM_TABLE = "example-table-prod"
const STORE_TO_TABLE = "example-table-dev"
const fs = require('fs');
async function loadAllResults() {
var results = []
var currentResults = await loadAdditionalResults()
results = results.concat(currentResults.Items)
while (currentResults.LastEvaluatedKey) {
currentResults = await loadAdditionalResults(currentResults.LastEvaluatedKey)
results = results.concat(currentResults.Items)
}
fs.writeFile("output.json", JSON.stringify(results, null, 2), () => {
console.log(results.length)
})
return results
}
async function loadAdditionalResults(start) {
console.log("Loading more results...")
var params = {
TableName: READ_FROM_TABLE,
}
if (start) {
params.ExclusiveStartKey = start
}
return new Promise((resolve, reject) => {
dynamoDb.scan(params, (error, result) => {
if (error) {
console.log(error);
reject(error)
} else if (result) {
resolve(result)
} else {
reject("Unknown error")
}
})
})
}
async function storeAllResults(results) {
var count = 0
for (result of results) {
await storeItem(result)
count++
console.log("Stored " + count + " / " + results.length)
}
}
async function storeItem(item) {
const params = {
TableName: STORE_TO_TABLE,
Item: item
};
return new Promise((resolve, reject) => {
dynamoDb.put(params, function (error, data) {
if (error) {
console.log(error);
reject({ error: 'Could not create item' });
}
resolve("Stored");
});
});
};
async function main() {
let results = await loadAllResults()
storeAllResults(results)
}
main()
随时使用对您有用的任何代码段。另外请注意,您可以使用https://www.npmjs.com/package/json2csv
将json转换为csv答案 1 :(得分:0)
我遇到了同样的问题,我使用上面的代码来解决我的问题。但我想从 DynamoDB 表生成一个 CSV 文件。 谢谢,@Carl Ambroselli,我修改了你的代码以获得我想要的结果。
我在上面的代码中使用了“papaparse”并将我的 DynamoDB 记录转换为 CSV 格式。
dynamodb-csv 包中提到了限制,因为 CSV 列标题是根据 DynamoDB 表中的第一行确定的。
我们可以在字段中传递列名以克服限制。
const fs = require('fs');
const async = require('async');
const AWS = require('aws-sdk');
const Papa = require("papaparse");
const DYNAMODB_REGION = 'XXXXXXXX';
AWS.config.update({
accessKeyId: 'XXXXXXXXXXX',
secretAccessKey: 'XXXXXXXXXX',
region: 'XXXXXX',
});
const docClient = new AWS.DynamoDB.DocumentClient({
region: DYNAMODB_REGION
});
const READ_FROM_TABLE = "tableName"
main();
async function loadAllResults() {
var results = [];
var currentResults = await loadAdditionalResults();
results = results.concat(currentResults.Items);
while (currentResults.LastEvaluatedKey) {
currentResults = await loadAdditionalResults(currentResults.LastEvaluatedKey)
results = results.concat(currentResults.Items)
}
return results;
}
async function loadAdditionalResults(start) {
console.log("Loading more results..."+READ_FROM_TABLE)
var params = {
TableName: READ_FROM_TABLE,
//Limit: 1000,
}
if (start) {
params.ExclusiveStartKey = start
}
return new Promise((resolve, reject) => {
docClient.scan(params, (error, result) => {
if (error) {
console.log(error);
reject(error);
} else if (result) {
resolve(result);
} else {
reject("Unknown error");
}
})
})
}
async function main() {
let tableData = await loadAllResults();
var csv = Papa.unparse(tableData);
fs.createWriteStream('test.csv', { flags: 'w' }).write(csv);
/*var csv = Papa.unparse({
fields: ["id", "productName", "price", "quantity"],
data: tableData
});
fs.createWriteStream('test.csv', { flags: 'w' }).write(csv);*/
}
输出为