简体   繁体   English

下载 AWS DynamoDB 数据到 Excel 格式为 CSV

[英]Download AWS DynamoDB data to Excel with CSV format

I want to download AWS DynamoDB data to Excel to allow me to work with the data locally.我想将 AWS DynamoDB 数据下载到 Excel 以允许我在本地处理数据。 However, I have not been to get the data in a perfect CSV format.但是,我一直没有得到一个完美的CSV格式的数据。

What I have done: I use a Node.js application, which runs in AWS Lambda service to connect to the DynamoDB database.我做了什么:我使用 Node.js 应用程序,它在 AWS Lambda 服务中运行以连接到 DynamoDB 数据库。 In addition, I can query the data from DynamoDB and then convert it to a CSV format, as detailed below:另外,我可以从DynamoDB查询数据,然后转换成CSV格式,具体如下:

const AWS = require("aws-sdk");

AWS.config.update({ region: "us-east-1"})
const dynamo = new AWS.DynamoDB.DocumentClient({apiversion: "2012-08-10"});


exports.handler = async (event, context) => {
    
  let body;
  const headers = {
      "Content-Type": "text/csv",
      'Content-disposition': 'attachment; filename=testing.csv'
  };
  
 var params = {
     KeyConditionExpression: 'dataId = :id',
     ExpressionAttributeValues: {
         ':id': event.pathParameters.id,
     },
     TableName: "Table1",
 };
 body = await dynamo.query(params).promise();
        
 //-----------------------------------
 // convert json to csv
 const items = body.Items
 const replacer = (key, value) => value === null ? '' : value
 const header = Object.keys(items[0])
 let csv = [header.join(','), 
            ...items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
            ].join('\r\n')

body = JSON.stringify(csv);
  return {
     body,
     headers,
  };
};

The above solution works, but the output is not perfect;上述解决方案有效,但 output 并不完美; a sample is shown below (Note that there are three columns: relativeHumidity, waterTemperature, and airTemperature):下面显示了一个示例(请注意,有三列:relativeHumidity、waterTemperature 和 airTemperature):

"relativeHumidity,waterTemperature,airTemperature\r\n26.123206154221034,21.716873058693757,23.859491598934557\r\n26.966163183232673,18.09642888420125,21.47952617547989\r\n33.79030978475366,18.995791668472204,17.451627574004128\r\n40.6641803491319,19.89060168145951,17.61247262137161" "relativeHumidity,waterTemperature,airTemperature\r\n26.123206154221034,21.716873058693757,23.859491598934557\r\n26.966163183232673,18.09642888420125,21.47952617547989\r\n33.79030978475366,18.995791668472204,17.451627574004128\r\n40.6641803491319,19.89060168145951,17.61247262137161"

However, I want an output that looks as shown below:但是,我想要一个如下所示的 output:

relativeHumidity,waterTemperature,airTemperature 26.123206154221034,21.716873058693757,23.859491598934557 26.966163183232673,18.09642888420125,21.47952617547989 33.79030978475366,18.995791668472204,17.451627574004128 40.6641803491319,19.89060168145951,17.61247262137161 relativeHumidity,waterTemperature,airTemperature 26.123206154221034,21.716873058693757,23.859491598934557 26.966163183232673,18.09642888420125,21.47952617547989 33.79030978475366,18.995791668472204,17.451627574004128 40.6641803491319,19.89060168145951,17.61247262137161

I would appreciate any guide on how to achieve this.我将不胜感激有关如何实现这一目标的任何指南。 Note that I have tried this , but the data is being exported to S3 in json format.请注意,我已经尝试过这个,但数据正在以 json 格式导出到 S3。

let inputString =  `"relativeHumidity,waterTemperature,airTemperature\r\n26.123206154221034,21.716873058693757,23.859491598934557\r\n26.966163183232673,18.09642888420125,21.47952617547989\r\n33.79030978475366,18.995791668472204,17.451627574004128\r\n40.6641803491319,19.89060168145951,17.61247262137161"`;
let result = inputString.split("\n");
result.forEach( val => {
  console.log(val);
})

Since AWS Lambda requires to return an object that is compatible with JSON.stringify, I am switching to Express, Node.js, and body-parser, which simplify the solution to the problem.由于AWS Lambda需要返回与 JSON.stringify 兼容的 object,因此我切换到 Express、Node.js 和 body-parser,这简化了问题的解决方案。

const bodyParser = require("body-parser");
const AWS = require("aws-sdk");
AWS.config.update({ region: "us-east-1" });
const dynamo = new AWS.DynamoDB.DocumentClient({ apiversion: "2012-08-10" });

const app = express();
const port = 3000;
app.use(bodyParser.json());

app.get("/", async (req, res) => {
  let body;
  res.setHeader(
    "Content-disposition",
    "attachment; filename=shifts-report.csv"
  );
  res.set("Content-Type", "text/csv");

  let csv;
  var params = {
    KeyConditionExpression: "deviceNameDate = :dnd",
    ExpressionAttributeValues: {
      ":dnd": "Factory1/Section1/Container1/2022-11-16",
    },
    ProjectionExpression: "waterTemperature, airTemperature, relativeHumidity",
    TableName: "factory1-section1-container1",
  };
  body = await dynamo.query(params).promise();

  // convert json to csv
  const items = body.Items;
  const replacer = (key, value) => (value === null ? "" : value);
  const header = Object.keys(items[0]);
  csv = [
    header.join(","),
    ...items.map((row) =>
      header
        .map((fieldName) => JSON.stringify(row[fieldName], replacer))
        .join(",")
    ),
  ].join("\n");

  res.send(csv);
});

app.listen(port, () => {
  console.log(`App started on port ${port}`);
});

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

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