Skip to content

Instantly share code, notes, and snippets.

@yuta-imai
Last active December 24, 2015 11:09
Show Gist options
  • Save yuta-imai/6789202 to your computer and use it in GitHub Desktop.
Save yuta-imai/6789202 to your computer and use it in GitHub Desktop.
// DynamoDBクライアントの初期化
AmazonDynamoDB client = new AmazonDynamoDBClient();
// Transaction管理テーブルの作成
TransactionManager.verifyOrCreateTransactionTable(
client, "Transactions" /*tableName*/,
10 /*readCapacityUnits*/,
10 /*writeCapacityUnits*/,
10 * 60 /*waitTimeSeconds*/);
// 同じくTransaction管理に必要なテーブルを作成
TransactionManager.verifyOrCreateTransactionImagesTable(
client, "TransactionImages" /*tableName*/,
10 /*readCapacityUnits*/,
10 /*writeCapacityUnits*/,
10 * 60 /*waitTimeSeconds*/);
// Transactionマネージャの初期化
TransactionManager txManager = new TransactionManager(client, "Transactions", "TransactionImages");
// Transaction開始
Transaction t1 = txManager.newTransaction();
// データを"Reply"テーブルにput
Map<String, AttributeValue> reply = new HashMap<String, AttributeValue>();
reply.put("Id", new AttributeValue("Amazon DynamoDB#Transactions?"));
reply.put("ReplyDateTime", new AttributeValue("(the current date and time)"));
reply.put("PostedBy", new AttributeValue("DavidY@AWS"));
reply.put("Message", new AttributeValue("Transactions are now available!"));
t1.putItem(new PutItemRequest()
.withTableName("Reply")
.withItem(reply));
// もうひとつのデータを更新
Map<String, AttributeValue> thread = new HashMap<String, AttributeValue>();
thread.put("ForumName", new AttributeValue("Amazon DynamoDB"));
thread.put("Subject", new AttributeValue("Transactions?"));
Map<String, AttributeValueUpdate> threadUpdates = new HashMap<String, AttributeValueUpdate>();
threadUpdates.put("Replies", new AttributeValueUpdate(new AttributeValue().withN("1"), "ADD"));
t1.updateItem(new UpdateItemRequest()
.withTableName("Thread")
.withKey(thread)
.withAttributeUpdates(threadUpdates));
// TransactionをCommit
t1.commit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment