Dynamodb-update-items

提供:Dev Guides
移動先:案内検索

DynamoDB-アイテムの更新

DynamoDBでアイテムを更新するには、主にアイテムの完全なプライマリキーとテーブル名を指定します。 変更する属性ごとに新しい値が必要です。 操作は UpdateItem を使用します。これは、既存のアイテムを変更するか、欠落しているアイテムの発見時にそれらを作成します。

更新では、操作の前後に元の値と新しい値を表示して、変更を追跡することができます。 UpdateItemは ReturnValues パラメーターを使用してこれを実現します。

_ -操作ではキャパシティーユニットの消費は報告されませんが、 ReturnConsumedCapacity パラメーターを使用できます。

このタスクを実行するには、GUIコンソール、Java、またはその他のツールを使用します。

GUIツールを使用してアイテムを更新する方法

コンソールに移動します。 左側のナビゲーションペインで、テーブル*を選択します。 必要なテーブルを選択し、[*アイテム]タブを選択します。

GUIツールを使用してアイテムを更新

更新に必要なアイテムを選択し、[アクション]を選択します。編集

アイテムを選択

[アイテムの編集]ウィンドウで必要な属性または値を変更します。

Javaを使用してアイテムを更新する

アイテムの更新操作でJavaを使用するには、Tableクラスインスタンスを作成し、 updateItem メソッドを呼び出す必要があります。 次に、アイテムの主キーを指定し、属性の変更の詳細を示す UpdateExpression を提供します。

以下は同じ例です-

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

Table table = dynamoDB.getTable("ProductList");

Map<String, String> expressionAttributeNames = new HashMap<String, String>();
expressionAttributeNames.put("#M", "Make");
expressionAttributeNames.put("#P", "Price
expressionAttributeNames.put("#N", "ID");

Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":val1",
   new HashSet<String>(Arrays.asList("Make1","Make2")));
expressionAttributeValues.put(":val2", 1);      //Price

UpdateItemOutcome outcome =  table.updateItem(
   "internalID",                                //key attribute name
   111,                                         //key attribute value
   "add #M :val1 set #P = #P - :val2 remove #N",//UpdateExpression
   expressionAttributeNames,
   expressionAttributeValues);
*updateItem* メソッドは、次の例で見ることができる条件を指定することもできます-
Table table = dynamoDB.getTable("ProductList");
Map<String, String> expressionAttributeNames = new HashMap<String, String>();
expressionAttributeNames.put("#P", "Price");

Map<String, Object> expressionAttributeValues = new HashMap<String, Object>();
expressionAttributeValues.put(":val1", 44); //change Price to 44
expressionAttributeValues.put(":val2", 15); //only if currently 15

UpdateItemOutcome outcome = table.updateItem (new PrimaryKey("internalID",111),
   "set #P = :val1",                       //Update
   "#P = :val2",                           //Condition
   expressionAttributeNames,
   expressionAttributeValues);

カウンターを使用してアイテムを更新する

DynamoDBではアトミックカウンターを使用できます。つまり、UpdateItemを使用して、他の要求に影響を与えることなく属性値を増減します。さらに、カウンターは常に更新されます。

以下は、その方法を説明する例です。

-次のサンプルでは、​​以前に作成されたデータソースを想定しています。 実行を試みる前に、サポートライブラリを取得し、必要なデータソース(必要な特性を備えたテーブル、または他の参照ソース)を作成します。

このサンプルでは、​​Eclipse IDE、AWS認証情報ファイル、およびEclipse AWS Javaプロジェクト内のAWS Toolkitも使用します。

package com.amazonaws.codesamples.document;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.document.DeleteItemOutcome;
import com.amazonaws.services.dynamodbv2.document.DynamoDB;
import com.amazonaws.services.dynamodbv2.document.Item;
import com.amazonaws.services.dynamodbv2.document.Table;

import com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome;
import com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec;
import com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec;
import com.amazonaws.services.dynamodbv2.document.utils.NameMap;
import com.amazonaws.services.dynamodbv2.document.utils.ValueMap;
import com.amazonaws.services.dynamodbv2.model.ReturnValue;

public class UpdateItemOpSample {
   static DynamoDB dynamoDB = new DynamoDB(new AmazonDynamoDBClient(
      new ProfileCredentialsProvider()));
   static String tblName = "ProductList";

   public static void main(String[] args) throws IOException {
      createItems();
      retrieveItem();

     //Execute updates
      updateMultipleAttributes();
      updateAddNewAttribute();
      updateExistingAttributeConditionally();

     //Item deletion
      deleteItem();
   }
   private static void createItems() {
      Table table = dynamoDB.getTable(tblName);
      try {
         Item item = new Item()
            .withPrimaryKey("ID", 303)
            .withString("Nomenclature", "Polymer Blaster 4000")
            .withStringSet( "Manufacturers",
            new HashSet<String>(Arrays.asList("XYZ Inc.", "LMNOP Inc.")))
            .withNumber("Price", 50000)
            .withBoolean("InProduction", true)
            .withString("Category", "Laser Cutter");
            table.putItem(item);

         item = new Item()
            .withPrimaryKey("ID", 313)
            .withString("Nomenclature", "Agitatatron 2000")
            .withStringSet( "Manufacturers",
            new HashSet<String>(Arrays.asList("XYZ Inc,", "CDE Inc.")))
            .withNumber("Price", 40000)
            .withBoolean("InProduction", true)
            .withString("Category", "Agitator");
            table.putItem(item);
      } catch (Exception e) {
         System.err.println("Cannot create items.");
         System.err.println(e.getMessage());
      }
   }
   private static void updateAddNewAttribute() {
      Table table = dynamoDB.getTable(tableName);
      try {
         Map<String, String> expressionAttributeNames = new HashMap<String, String>();
         expressionAttributeNames.put("#na", "NewAttribute");
         UpdateItemSpec updateItemSpec = new UpdateItemSpec()
            .withPrimaryKey("ID", 303)
            .withUpdateExpression("set #na = :val1")
            .withNameMap(new NameMap()
            .with("#na", "NewAttribute"))
            .withValueMap(new ValueMap()
            .withString(":val1", "A value"))
            .withReturnValues(ReturnValue.ALL_NEW);
            UpdateItemOutcome outcome =  table.updateItem(updateItemSpec);

        //Confirm
         System.out.println("Displaying updated item...");
         System.out.println(outcome.getItem().toJSONPretty());
      } catch (Exception e) {
         System.err.println("Cannot add an attribute in " + tableName);
         System.err.println(e.getMessage());
      }
   }
}