AWS SDK for Javascriptで、DynamoDBとDynamoDBのクラスであるDocumentClientを使うので何が違うかということです。要は、DocumentClientを使うことでプライマリキーや属性値の型をいちいちSとかNとか指定せずに済むということらしいです。
The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.
(ドキュメントクライアントは、属性値の概念を抽象化することで、Amazon DynamoDBのアイテムの操作を簡素化します。この抽象化では、入力パラメータとして提供されたネイティブJavaScriptタイプに注釈を付け、また、注釈付き応答データをネイティブJavaScriptの型に変換します。)
引用元:Class: AWS.DynamoDB.DocumentClient
DynamoDBでBatchWriteItem
DynamoDBでは項目の属性値ごとに、属性値名:{型:属性値}
というように型の部分でSやNなどを記述しなければなりません。
var dynamodb = new AWS.DynamoDB({region: AWS_REGION});
var params = {
RequestItems: { /* required */
TABLE_NAME: [
{
DeleteRequest: {
Key: { /* required */
someKey: { /* AttributeValue */
B: new Buffer('...') || 'STRING_VALUE',
BOOL: true || false,
BS: [
new Buffer('...') || 'STRING_VALUE',
/* more items */
],
L: [
/* recursive AttributeValue */,
/* more items */
],
M: {
someKey: /* recursive AttributeValue */,
/* anotherKey: ... */
},
N: 'STRING_VALUE',
NS: [
'STRING_VALUE',
/* more items */
],
NULL: true || false,
S: 'STRING_VALUE',
SS: [
'STRING_VALUE',
/* more items */
]
},
/* anotherKey: ... */
}
},
PutRequest: {
Item: { /* required */
someKey: { /* AttributeValue */
B: new Buffer('...') || 'STRING_VALUE',
BOOL: true || false,
BS: [
new Buffer('...') || 'STRING_VALUE',
/* more items */
],
L: [
/* recursive AttributeValue */,
/* more items */
],
M: {
someKey: /* recursive AttributeValue */,
/* anotherKey: ... */
},
N: 'STRING_VALUE',
NS: [
'STRING_VALUE',
/* more items */
],
NULL: true || false,
S: 'STRING_VALUE',
SS: [
'STRING_VALUE',
/* more items */
]
},
/* anotherKey: ... */
}
}
},
/* more items */
],
/* anotherKey: ... */
},
ReturnConsumedCapacity: 'INDEXES | TOTAL | NONE',
ReturnItemCollectionMetrics: 'SIZE | NONE'
};
dynamodb.batchWriteItem(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
参考:Class: AWS.DynamoDB #batchWriteItem
※尚、上記にTEBLE_NAME
と書かれている部分は参考元ではSomeKey
となっていますが、実際にはここにはテーブル名が入ります。(分かりにくすぎやろ・・・)
DocumentClientでBatchWriteItem
一方、DocumentClientでは、属性値毎の型の記述が不要で、シンプルに属性値名:属性値
と書くだけで済みます。
var dynamodb = new AWS.DynamoDB({region: AWS_REGION});
var documentclient = new AWS.DynamoDB.DocumentClient({service: dynamodb});
var params = {
RequestItems: { /* required */
TABLE_NAME: [
{
DeleteRequest: {
Key: { /* required */
someKey: someValue /* "str" | 10 | true | false | null | [1, "a"] | {a: "b"} */,
/* anotherKey: ... */
}
},
PutRequest: {
Item: { /* required */
someKey: someValue /* "str" | 10 | true | false | null | [1, "a"] | {a: "b"} */,
/* anotherKey: ... */
}
}
},
/* more items */
],
/* anotherKey: ... */
},
ReturnConsumedCapacity: 'INDEXES | TOTAL | NONE',
ReturnItemCollectionMetrics: 'SIZE | NONE'
};
documentclient.batchWrite(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
参考:Class: AWS.DynamoDB.DocumentClient #batchWriteItem
※尚、こちらも上記にTEBLE_NAME
と書かれている部分は参考元ではSomeKey
となっていますが、実際にはここにはテーブル名が入ります。
結論:DocumentClientを使うべし
DocumentClientを使ったほうが遥かに楽だと思います。注意点としては、例えば同じbatchWriteItemでも、DynamoDBではbatchWriteItem
なのにDocumentClientではbatchWrite
だったりと微妙に違ってくるということです。
(ドキュメントややこしい)