So i have a dynamodb table called tableX with the fields:
random_fld1, random_fld2, primary_key1, and sort_key1
all fields are Strings.
What would be a getItem example with those keys and fields. I haven't been able to get the Keys: portion working with getItem.
So i have a dynamodb table called tableX with the fields:
random_fld1, random_fld2, primary_key1, and sort_key1
all fields are Strings.
What would be a getItem example with those keys and fields. I haven't been able to get the Keys: portion working with getItem.
Share Improve this question asked Jul 6, 2017 at 11:59 MessakMessak 4631 gold badge4 silver badges16 bronze badges2 Answers
Reset to default 10You can use DynamoDB's DocumentClient get() as:
'use strict';
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10' });
var params = {
TableName: 'tableX',
Key: {
'primary_key1': 'PARTITION_KEY_VALUE',
'sort_key1': 'SORT_KEY_VALUE'
}
};
docClient.get(params, function(err, data) {
if (err) console.log(err);
else console.log(data);
});
See the documentation.
Here is the get item code for above table structure.
The below code uses local Dynamodb table.
var AWS = require("aws-sdk");
var creds = new AWS.Credentials('akid', 'secret', 'session');
AWS.config.update({
region: "us-west-2",
endpoint: "http://localhost:8000",
credentials: creds
});
var docClient = new AWS.DynamoDB.DocumentClient();
var table = "tablex";
var params = {
TableName: table,
Key:{
"primary_key1": "p1",
"sort_key1": "s1"
},
};
docClient.get(params, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
}
});