最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using getItem with primary and sort keys - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 10

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.

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));        
    }
});
发布评论

评论列表(0)

  1. 暂无评论