Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS - One of the required keys was not given a value

New to AWS, trying to put data into a table. After reading the documentation and trying to follow examples I am running into this validation error.

One of the required keys was not given a value

My code:

var conf = require("/directory");
var AccessKey = 'supersecretkey';
var SecretKey = 'supersecretkey';

var tableName = conf.tableCS;

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

console.log("Endpoint: " + conf.endpoint);
AWS.config.update({
    accessKeyId: AccessKey,
    secretAccessKey: SecretKey,
    region: conf.region,
    endpoint: conf.endpoint
});
var docClient = new AWS.DynamoDB.DocumentClient();

var file = process.argv[2];
console.log("File: " + file);

var csv = require("fast-csv");

csv.fromPath(file)
    .on("data", function(data) {
        // Uncomment to see CSV data
        // console.log(data);

        var arrayLength = data.length;
        for (var i = 0; i < arrayLength; i++) {
            // console.log(data[i]);
            var params = {
                TableName: tableName,
                Key: {
                       RefID: data
                }
            };

            console.log("Adding a new item...");
            docClient.put(params, function(err, data) {
                if (err) {
                    console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
                }
                else {
                    console.log("Added item:", JSON.stringify(data, null, 2));
                }
            });
            // Looping through, now putItem into database
        }

    })
    .on("end", function() {
        console.log("done");
    });

I found this, but am not understanding the range key part. I'm creating the table in this following code and have the hash key, but not a range key. Is that my issue? What would I make the range key?

var conf = require("/directory");
var AccessKey = 'supersecretkey';
var SecretKey = 'supersecretkey';

var tableName = conf.tableCSV;

// Take input from command line
process.argv.forEach(function(val, index, array) {
    console.log(index + ': ' + val);
});

console.log("Table: " + tableName);

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

console.log("Endpoint: " + conf.endpoint);
AWS.config.update({
    accessKeyId: AccessKey,
    secretAccessKey: SecretKey,
    region: conf.region,
    endpoint: conf.endpoint
});
var dynamodb = new AWS.DynamoDB();

var params = {
    TableName: tableName,
    KeySchema: [{
        AttributeName: 'RefID',
        KeyType: 'HASH'
    }],
    AttributeDefinitions: [{
        AttributeName: 'RefID',
        AttributeType: 'S'
    }],
    ProvisionedThroughput: {
        ReadCapacityUnits: 1,
        WriteCapacityUnits: 1
    }
};

dynamodb.createTable(params, function(err, table) {
    if (err) {
        console.log(err);
    }
    else {
        console.log("TABLED CREATED");
        console.log(table);
    }
});
like image 309
FF5Ninja Avatar asked Aug 02 '16 15:08

FF5Ninja


People also ask

What is PutItem in DynamoDB?

PDF. Creates a new item, or replaces an old item with a new item. If an item that has the same primary key as the new item already exists in the specified table, the new item completely replaces the existing item.

How do I add items to DynamoDB?

In Amazon DynamoDB, you can use either the DynamoDB API, or PartiQL, a SQL-compatible query language, to add an item to a table. With the DynamoDB API, you use the PutItem operation to add an item to a table. The primary key for this table consists of Artist and SongTitle. You must specify values for these attributes.

Does Put item update item DynamoDB?

The main difference between the two is, PutItem will Replace an entire item while UpdateItem will Update it.

Which statement retrieves an item from the music collection table in DynamoDB?

In SQL, you use the SELECT statement to retrieve data from a table. You can request one or more columns in the result (or all of them, if you use the * operator). The WHERE clause determines which rows to return. The following is a SELECT statement to retrieve a single row from the Music table.


1 Answers

In the table definition, you named the key Ref-ID but in your put operation you are naming the key Info. If your table has a hash key named Ref-ID then every record you insert will require a value for Ref-ID.

like image 104
Mark B Avatar answered Oct 19 '22 14:10

Mark B