2

我有一个带有名称 id 和 HASH 类型的属性的表。我想从一组 id 中获取所有项目。

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

我应该怎么做才能通过我的 id 获取所有项目?

4

3 回答 3

5

您也可以使用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)
  }
});
于 2018-11-04T13:52:17.760 回答
2

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

批量获取项目

例子:-

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));
    }
});
于 2018-11-04T01:14:44.723 回答
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 中创建 & 要获取的数据

于 2019-12-05T17:03:56.450 回答