【问题标题】:Updating multiple records in DynamoDB更新 DynamoDB 中的多条记录
【发布时间】:2017-09-14 01:28:39
【问题描述】:

如何在单个查询中更新 DynamoDB 中的多条记录? 我有一个 csv 文件作为基于 csv 文件的输入,我必须更新数据库中的多条记录(只有一个属性)。 有没有可用的API? 或者这可以使用批处理(Spring-batch)来完成?

【问题讨论】:

    标签: amazon-dynamodb batch-processing


    【解决方案1】:

    DynamoDB 没有直接的batchUpdate API。它确实有 batch get itembatch write item API。

    但是,您可以使用batchWriteItem API 来更新项目。

    1) 使用下面的BatchWriteItemSpec 类来构造请求

    BatchWriteItemSpec

    2) 使用下面的TableWriteItems 类来构造需要更新的项目

    TableWriteItems

    3) 使用下面的addItemToPut 方法(或withItemsToPut)添加新属性

    addItemToPut

    如果项目(即分区键)不可用,batchWriteItem API 会创建一个新项目。如果分区键可用,它将更新现有项目。您的用例属于此类别。

    代码示例:-

    files - 是表名

    fileName - 是分区键

    transcriptionnew - 添加新属性(或者也可以更新现有属性值)

    DynamoDB dynamoDB = new DynamoDB(dynamoDBClient);
    
                Item itemUpdate1 = new Item();
    
                itemUpdate1.withKeyComponent("fileName", "file1")
                        .withString("transcriptionnew", "new value");
    
                Item itemUpdate2 = new Item();
    
                itemUpdate2.withKeyComponent("fileName", "file2")
                        .withString("transcriptionnew", "new value");
    
                TableWriteItems tableWriteItems = new TableWriteItems("files").withItemsToPut(itemUpdate1, itemUpdate2);
    
                BatchWriteItemSpec batchWriteItemSpec = new BatchWriteItemSpec().withTableWriteItems(tableWriteItems);
    
                BatchWriteItemOutcome batchWriteItemOutCome = dynamoDB.batchWriteItem(batchWriteItemSpec);
    
                if (batchWriteItemOutCome.getUnprocessedItems().isEmpty()) {
                    //All items are processed
                    return true;
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-18
      • 2016-05-01
      • 2013-05-02
      • 2014-03-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多