简体   繁体   English

如何使用nodejs将过滤后的数据从dynamodb导出到csv?

[英]How to export filtered data from dynamodb to csv using nodejs?

I want to export filtered data from dynamodb to csv.我想将过滤后的数据从 dynamodb 导出到 csv。 I have a function for filtering data using name and id..now to i need to export the filtered data to excel..how can i fix this?我有一个 function 用于使用名称和 id 过滤数据..现在我需要将过滤后的数据导出到 excel..我该如何解决这个问题? I had seen this npm dynamodb-csv .我见过这个 npm dynamodb-csv but this didnt solve my problem..但这并没有解决我的问题..

I'm using the following code snippet for export and import:我正在使用以下代码片段进行导出和导入:

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()

Feel free to use whatever snippet of that is useful to you.随意使用对您有用的任何片段。 Also note that you can convert the json to csv using https://www.npmjs.com/package/json2csv另请注意,您可以使用https://www.npmjs.com/package/json2csv将 json 转换为 csv

I had the same issue and I used the above code to fix my problem.我有同样的问题,我使用上面的代码来解决我的问题。 But I wanted to generate a CSV file from the DynamoDB table.但我想从 DynamoDB 表中生成一个 CSV 文件。 Thanks, @Carl Ambroselli, I modify your code for my desired result.谢谢@Carl Ambroselli,我修改了您的代码以获得所需的结果。

I used "papaparse" in the above code and got my DynamoDB records into CSV format.我在上面的代码中使用了“papaparse”并将我的 DynamoDB 记录转换为 CSV 格式。

dynamodb-csv package has mentioned limitation as CSV column headers are determined based on the first row in your DynamoDB table. dynamodb-csv package 提到了限制,因为 CSV 列标题是根据 DynamoDB 表中的第一行确定的。

We can pass column names in fields to overcome limitations.我们可以在字段中传递列名来克服限制。

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);*/
}

Output as Output

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM