dynamodb通过ID数组获取所有项

时间:2018-11-03 06:45:53

标签: node.js amazon-dynamodb dynamodb-queries

我有一个表,该表的属性名称为id,且类型为HASH。我想从ID数组中获取所有项目。

{
    TableName: `MyTable`,
    FilterExpression: 'id IN (:id)',
    ExpressionAttributeValues: { ':id': ids },
};

我应该怎么做才能获得我所有的ID?

3 个答案:

答案 0 :(得分:2)

  

您还可以使用DocumentClientbatchGet

const AWS = require('aws-sdk');
const documentClient = new AWS.DynamoDB.DocumentClient();

let queryParams = {RequestItems: {}};
queryParams.RequestItems['tableName'] = {
  Keys: [{'id': 'Value1'}, {'id': 'value2'}],
  ProjectionExpression: 'id' //define other fileds that you have Ex: 'id,name'
};

documentClient.batchGet(queryParams, function (err, data) {
  if (err) {
    console.log('failure:getItemByBatch data from Dynamo error', err);
  } else {
    console.log('success:getItemByBatch data from Dynamo data');
    console.log(data)
  }
});

答案 1 :(得分:1)

请使用BatchGetItem API从DynamoDB表中获取多个值。

BatchGetItem

示例:-

var dynamodb = new AWS.DynamoDB({maxRetries: 5, retryDelayOptions: {base: 300} });

var table = "Movies";

var year_val = 2015;
var title = "The Big New Movie";

var params = {
    "RequestItems" : {
        "Movies" : {
            "Keys" : [ {
                "yearkey" : {N : "2016"},
                "title" : {S : "The Big New Movie 1"}
            } ]
        }
    },
    "ReturnConsumedCapacity" : "TOTAL"
};

dynamodb.batchGetItem(params, function(err, data) {
    if (err) {
        console.error("Unable to get item. Error JSON:", JSON.stringify(err,
                null, 2));
    } else {
        console.log("Movie data:", JSON.stringify(data, null, 2));
    }
});

答案 2 :(得分:0)

在C#中,下面的代码是使用BatchGet或CreateBatchGet从具有不同guid的dynamodb表中按ID数组获取所有项

        string tablename = "AnyTableName"; //table whose data you want to fetch

        var BatchRead = ABCContext.Context.CreateBatchGet<ABCTable>(  

            new DynamoDBOperationConfig
            {
                OverrideTableName = tablename; 
            });

        foreach(string Id in IdList) // in case you are taking string from input
        {
            Guid objGuid = Guid.Parse(Id); //parsing string to guid
            BatchRead.AddKey(objGuid);
        }

        await BatchRead.ExecuteAsync();
        var result = BatchRead.Results;

// ABCTable是表模式,用于在要提取的dynamodb和数据中创建