拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。 网站地图
IMG-LOGO
主页 文章列表 汇入表中的JSON资料(DynamoDB、nodeJS)

汇入表中的JSON资料(DynamoDB、nodeJS)

by 白鹭 - 2022-02-17 1956 0 0

我想使用以下代码将资料从我的 JSON 档案汇入 DynamoDB:

var AWS = require("aws-sdk");
var fs = require('fs');

AWS.config.update({
    region: "us-west-2",
    endpoint: "http://localhost:8000"
});

var docClient = new AWS.DynamoDB.DocumentClient();

console.log("Importing Pays into DynamoDB. Please wait.");

var allPays = JSON.parse(fs.readFileSync('paysdata.JSON', 'utf8'));
allPays.forEach(function(pays) {
    var params = {
        TableName: "Pays",
        Item: {
            "region":  pays.region,
            "name": pays.name,
            "name.common": pays.name.common,
            "languages":  pays.languages,
            "area": pays.area
        }
    };

    docClient.put(params, function(err, data) {
       if (err) {
           console.error("Unable to add Pays", pays.name.common, ". Error JSON:", JSON.stringify(err, null, 2));
       } else {
           console.log("PutItem succeeded:", pays.name.common);
       }
    });
});

但我仍然收到此错误 这里 有没有人知道如何解决这个问题?我尝试在网站上遵循相同的步骤: Create table using nodejs Dynamodb?

但它不起作用。

我的 CreateTable.js

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

AWS.config.update({
  region: "us-west-2",
  endpoint: "http://localhost:8000"
});

var dynamodb = new AWS.DynamoDB();

var params = {
    TableName : "Pays",
    KeySchema: [       
        { AttributeName: "region", KeyType: "HASH"},  //Partition key
        { AttributeName: "name", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [       
        { AttributeName: "region", AttributeType: "S" },
        { AttributeName: "name", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
};

dynamodb.createTable(params, function(err, data) {
    if (err) {
        console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
    } else {
        console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
    }
});

一些资料 :

[
    {
        "name": {
            "common": "Aruba",
            "official": "Aruba",
            "native": {
                "nld": {
                    "official": "Aruba",
                    "common": "Aruba"
                },
                "pap": {
                    "official": "Aruba",
                    "common": "Aruba"
                }
            }
        },
        "translations": {
            "ces": {
                "official": "Aruba",
                "common": "Aruba"
            },
            "deu": {
                "official": "Aruba",
                "common": "Aruba"
            },
            "jpn": {
                "official": "\u30a2\u30eb\u30d0",
                "common": "\u30a2\u30eb\u30d0"
            },
            "kor": {
                "official": "\uc544\ub8e8\ubc14",
                "common": "\uc544\ub8e8\ubc14"
            },

uj5u.com热心网友回复:

        "name": {
            "common": "Aruba",
            "official": "Aruba",
            "native": {
                "nld": {
                    "official": "Aruba",
                    "common": "Aruba"
                },
                "pap": {
                    "official": "Aruba",
                    "common": "Aruba"
                }
            }
        }

name是一个物件,因此您不能pays.name作为 的值name,因为它是“字符串”资料型别。

要解决此问题,您可以将表定义更改为:

var params = {
    TableName : "Pays",
    KeySchema: [       
        { AttributeName: "region", KeyType: "HASH"},  //Partition key
        { AttributeName: "name.common", KeyType: "RANGE" }  //Sort key
    ],
    AttributeDefinitions: [       
        { AttributeName: "region", AttributeType: "S" },
        { AttributeName: "name.common", AttributeType: "S" }
    ],
    ProvisionedThroughput: {       
        ReadCapacityUnits: 10, 
        WriteCapacityUnits: 10
    }
};
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *