2017-05-26 251 views
0

基於非主鍵字段來執行批量dynamodb更新的最佳方法是什麼?例如,如果我有一個表deploymentKey =「UK」,那麼更新字段配置爲「uk-config」。我查看了亞馬遜的文檔,特別是「更新項目」,但只是基於主鍵進行更新。基於dynamodb字段的批量更新

另外我看了一個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()); 
    } 
    } 
} 

回答

0

DynamoDB的updateItem 作品與分區鍵 - 如果你不知道,你不能使用它。如果沒有分區和排序鍵(如果表格有一個),則無法進行更新(批量或其他)。

我不是在Java工作,但基本上,你必須掃描表尋找適當的值,然後逐一更新每個項目。如果你有很多數據,它會變得很慢。