DynamoDB Localをaws-java-sdkから操作

ダウンロードと起動

Additional Tools and Libraries For Amazon DynamoDB - Amazon DynamoDB
ここまでは公式ドキュメントに載ってる通り。

$ java –Djava.library.path=. -jar DynamoDBLocal.jar

aws-java-sdkから呼び出す

基本的にエンドポイントを"http://localhost:8000"にするだけ。
アクセスキーとリージョンはDBファイル名に使われるので好きな名前で。
以下で動かすと"AccessKey_us-east-1.db"という名前のファイルが作成された

public static void main(String[] args) {
    String accessKey = "AccessKey";
    String secretAccessKey = "SecretKey";
    AWSCredentials cre = new BasicAWSCredentials(accessKey, secretAccessKey);
    AmazonDynamoDBClient client = new AmazonDynamoDBClient(cre);
    client.setEndpoint("http://localhost:8000");
}

動作確認

String tableName = "DynamoTest";

// テーブル一覧
System.out.println(client.listTables());

// テーブル作成
List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
keySchema.add(new KeySchemaElement().withAttributeName("HogeId").withKeyType(KeyType.HASH));
AttributeDefinition attrDef = new AttributeDefinition().withAttributeName("HogeId").withAttributeType(ScalarAttributeType.S);
ProvisionedThroughput pt = new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(5L);
System.out.println(client.createTable(new CreateTableRequest(tableName, keySchema).withAttributeDefinitions(attrDef).withProvisionedThroughput(pt)));

// アイテム追加
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put("HogeId", new AttributeValue("AAAA"));
item.put("foo", new AttributeValue("valueA"));
item.put("bar", new AttributeValue().withSS(Arrays.asList("valueB", "valueC")));
System.out.println(client.putItem(new PutItemRequest(tableName, item)));

// アイテム取得
Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("HogeId", new AttributeValue("AAAA"));
System.out.println(client.getItem(new GetItemRequest(tableName, key)));

// アイテム更新
Map<String, AttributeValueUpdate> attributes = new HashMap<String, AttributeValueUpdate>();
attributes.put("bar", new AttributeValueUpdate(new AttributeValue().withS("valueD"), AttributeAction.PUT));
System.out.println(client.updateItem(new UpdateItemRequest(tableName, key, attributes)));

// スキャン
System.out.println(client.scan(new ScanRequest(tableName)));

// アイテム削除
System.out.println(client.deleteItem(new DeleteItemRequest(tableName, key)));

// テーブル削除
System.out.println(client.deleteTable(new DeleteTableRequest(tableName)));

実行結果

{TableNames: [],}
{TableDescription: {AttributeDefinitions: [{AttributeName: HogeId,AttributeType: S}],TableName: DynamoTest,KeySchema: [{AttributeName: HogeId,KeyType: HASH}],TableStatus: ACTIVE,CreationDateTime: Fri Sep 13 14:12:03 JST 2013,ProvisionedThroughput: {NumberOfDecreasesToday: 0,ReadCapacityUnits: 10,WriteCapacityUnits: 5},TableSizeBytes: 0,ItemCount: 0,}}
{}
{Item: {HogeId={S: AAAA,}, foo={S: valueA,}, bar={SS: [valueB, valueC],}},}
{}
{Items: [{HogeId={S: AAAA,}, foo={S: valueA,}, bar={S: valueD,}}],Count: 1,ScannedCount: 1,}
{}
{TableDescription: {AttributeDefinitions: [{AttributeName: HogeId,AttributeType: S}],TableName: DynamoTest,KeySchema: [{AttributeName: HogeId,KeyType: HASH}],TableStatus: ACTIVE,CreationDateTime: Fri Sep 13 14:12:03 JST 2013,ProvisionedThroughput: {NumberOfDecreasesToday: 0,ReadCapacityUnits: 10,WriteCapacityUnits: 5},TableSizeBytes: 0,ItemCount: 0,}}