Using getItem with primary and sort keys

10,558

Solution 1

You 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.

Solution 2

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));        
    }
});
Share:
10,558
Messak
Author by

Messak

Updated on June 09, 2022

Comments

  • Messak
    Messak almost 2 years

    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.