【问题标题】:bulk update based on dynamodb field基于 dynamodb 字段的批量更新
【发布时间】:2017-05-26 16:58:20
【问题描述】:

基于非主键字段进行批量 dynamodb 更新的最佳方法是什么?例如,如果我有一个带有 deploymentKey = "UK" 的表,则将字段配置更新为 "uk-config"。我查看了亚马逊文档,特别是“update-item”,但仅基于主键进行更新。

另外,我查看了一个 java 示例,它返回表中的所有项目,但在我的情况下,它一直没有返回任何项目。

// Copyright 2012-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// Licensed under the Apache License, Version 2.0.
package com.amazonaws.codesamples.document;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.document.BatchGetItemOutcome;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item; 
import com.amazonaws.services.dynamodbv2.document.TableKeysAndAttributes;
import com.amazonaws.services.dynamodbv2.model.KeysAndAttributes;

public class DocumentAPIBatchGet {
    static AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
static DynamoDB dynamoDB = new DynamoDB(client);

static String forumTableName = "Forum";
static String threadTableName = "Thread";

public static void main(String[] args) throws IOException {
    retrieveMultipleItemsBatchGet();
}

private static void retrieveMultipleItemsBatchGet() {

    try {

        TableKeysAndAttributes forumTableKeysAndAttributes = new TableKeysAndAttributes(forumTableName);
        // Add a partition key
        forumTableKeysAndAttributes.addHashOnlyPrimaryKeys("Name", "Amazon S3", "Amazon DynamoDB");

        TableKeysAndAttributes threadTableKeysAndAttributes = new TableKeysAndAttributes(threadTableName);
        // Add a partition key and a sort key
        threadTableKeysAndAttributes.addHashAndRangePrimaryKeys("ForumName", "Subject", "Amazon DynamoDB",
            "DynamoDB Thread 1", "Amazon DynamoDB", "DynamoDB Thread 2", "Amazon S3", "S3 Thread 1");

        System.out.println("Making the request.");

        BatchGetItemOutcome outcome = dynamoDB.batchGetItem(forumTableKeysAndAttributes,
            threadTableKeysAndAttributes);

        Map<String, KeysAndAttributes> unprocessed = null;

        do {
            for (String tableName : outcome.getTableItems().keySet()) {
                System.out.println("Items in table " + tableName);
                List<Item> items = outcome.getTableItems().get(tableName);
                for (Item item : items) {
                    System.out.println(item.toJSONPretty());
                }
            }
            // Check for unprocessed keys which could happen if you exceed
            // provisioned
            // throughput or reach the limit on response size.
            unprocessed = outcome.getUnprocessedKeys();

            if (unprocessed.isEmpty()) {
                System.out.println("No unprocessed keys found");
            }
            else {
                System.out.println("Retrieving the unprocessed keys");
                outcome = dynamoDB.batchGetItemUnprocessed(unprocessed);
            }

        } while (!unprocessed.isEmpty());

    }
    catch (Exception e) {
        System.err.println("Failed to retrieve items.");
        System.err.println(e.getMessage());
    }
  }
}

【问题讨论】:

    标签: amazon-web-services amazon-dynamodb


    【解决方案1】:

    DynamoDB UpdateItem 与分区键一起使用 - 如果您不知道,则无法使用它。如果没有分区和排序键(如果表有),就无法进行更新(批量或其他方式)。

    我没有使用 Java,但基本上,您必须扫描表以查找正确的值,然后一项一项地更新每个项目。如果你有很多数据,它会很慢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      • 2020-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多