DynamoDB-バッチ取得

バッチ取得操作は、単一または複数のアイテムの属性を返します。これらの操作は通常、主キーを使用して目的のアイテムを識別することで構成されます。ザ・BatchGetItem 操作は、個々の操作の制限と独自の制約の対象となります。

バッチ検索操作での次の要求により、拒否されます-

  • 100個以上のアイテムをリクエストしてください。
  • スループットを超えるリクエストを行います。

バッチ取得操作は、制限を超える可能性のある要求の部分的な処理を実行します。

For example−制限を超えるのに十分なサイズの複数のアイテムを取得する要求は、要求処理の一部になり、未処理の部分を示すエラーメッセージが表示されます。未処理のアイテムが戻ってきたら、テーブルを調整するのではなく、これを管理するためのバックオフアルゴリズムソリューションを作成します。

ザ・ BatchGet操作は最終的に一貫した読み取りで実行され、強く一貫した読み取りを変更する必要があります。また、並行して検索を実行します。

Note−返品されたアイテムの順序。DynamoDBはアイテムをソートしません。また、要求されたアイテムがないことを示すものでもありません。さらに、これらの要求は容量単位を消費します。

すべてのBatchGet操作には RequestItems 読み取りの一貫性、属性名、主キーなどのパラメーター。

Response −操作が成功すると、HTTP 200応答が生成されます。これは、消費された容量単位、テーブル処理メトリック、および未処理のアイテムなどの特性を示します。

Javaを使用したバッチ取得

BatchGet操作でJavaを使用するには、DynamoDBクラスインスタンスを作成する必要があります。 TableKeysAndAttributes アイテムの主キー値リストを記述し、TableKeysAndAttributesオブジェクトをに渡すクラスインスタンス BatchGetItem 方法。

以下は、BatchGet操作の例です-

DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient ( 
   new ProfileCredentialsProvider()));  

TableKeysAndAttributes forumTableKeysAndAttributes = new TableKeysAndAttributes 
   (forumTableName);
   
forumTableKeysAndAttributes.addHashOnlyPrimaryKeys (
   "Title",
   "Updates",  
   "Product Line 1"
); 
TableKeysAndAttributes threadTableKeysAndAttributes = new TableKeysAndAttributes (
   threadTableName);
      
threadTableKeysAndAttributes.addHashAndRangePrimaryKeys (
   "ForumTitle",
   "Topic",  
   "Product Line 1",
   "P1 Thread 1", 
   "Product Line 1",
   "P1 Thread 2", 
   "Product Line 2",
   "P2 Thread 1"
); 
BatchGetItemOutcome outcome = dynamoDB.batchGetItem ( 
   forumTableKeysAndAttributes, threadTableKeysAndAttributes);
      
for (String tableName : outcome.getTableItems().keySet()) { 
   System.out.println("Table items " + tableName); 
   List<Item> items = outcome.getTableItems().get(tableName); 
   for (Item item : items) { 
      System.out.println(item); 
   } 
}

次の大きな例を確認できます。

Note−次のプログラムは、以前に作成されたデータソースを想定している場合があります。実行を試みる前に、サポートライブラリを取得し、必要なデータソース(必要な特性を持つテーブル、またはその他の参照ソース)を作成します。

このプログラムは、Eclipse IDE、AWS認証ファイル、およびEclipse AWSJavaプロジェクト内のAWSToolkitも使用します。

package com.amazonaws.codesamples.document;

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

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
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 BatchGetOpSample { 
   static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient ( 
      new ProfileCredentialsProvider())); 
      
   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); 
         
         //Create partition key 
         forumTableKeysAndAttributes.addHashOnlyPrimaryKeys (
            "Name", 
            "XYZ Melt-O-tron", 
            "High-Performance Processing"
         ); 
         TableKeysAndAttributes threadTableKeysAndAttributes = 
            new TableKeysAndAttributes(threadTableName); 
         
         //Create partition key and sort key 
         threadTableKeysAndAttributes.addHashAndRangePrimaryKeys (
            "ForumName",
            "Subject",  
            "High-Performance Processing",
            "HP Processing Thread One", 
            "High-Performance Processing",
            "HP Processing Thread Two", 
            "Melt-O-Tron",
            "MeltO Thread One"
         );
         System.out.println("Processing..."); 
         BatchGetItemOutcome outcome = dynamoDB.batchGetItem(forumTableKeysAndAttributes,
            threadTableKeysAndAttributes); 
              
         Map<String, KeysAndAttributes> unprocessed = null;    
         do { 
            for (String tableName : outcome.getTableItems().keySet()) { 
               System.out.println("Table items for " + tableName); 
               List<Item> items = outcome.getTableItems().get(tableName); 
               
               for (Item item : items) { 
                  System.out.println(item.toJSONPretty()); 
               } 
            } 
            // Confirm no unprocessed items 
            unprocessed = outcome.getUnprocessedKeys(); 
                 
            if (unprocessed.isEmpty()) { 
               System.out.println("All items processed."); 
            } else { 
               System.out.println("Gathering unprocessed items..."); 
               outcome = dynamoDB.batchGetItemUnprocessed(unprocessed); 
            } 
         } while (!unprocessed.isEmpty()); 
      } catch (Exception e) { 
         System.err.println("Could not get items."); 
         System.err.println(e.getMessage()); 
      }   
   } 
}