💬
AWS SDK for JavaScriptを使ってDynamoDBテーブルの空文字列を取得する方法
はじめに
- 空文字列をscanする方法が分からなかった
- size()でできそうなのでやってみた
なんかググってすぐ出てこなかったのでやってみることにした。
やってみたこと
- 事前準備
- SAMPLEというテーブルを作成(プライマリキー:id)
- 実施
ddb-scan.js
const AWS = require('aws-sdk');
AWS.config.update({region: 'ap-northeast-1'});
const DOC_CLIENT = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});
const TABLE_NAME = 'SAMPLE';
const main = async() =>{
// データ投入
const Items = [
{"value": "DOP","id": "BBB","next": "DDD","prev": "CCC"},
{"value": "SOA","id": "DDD","next": "EEE","prev": "DDD"},
{"value": "SAP","id": "AAA","next": "CCC","prev": ""},
{"value": "DVA","id": "EEE","next": "FFF","prev": "DDD"},
{"value": "SAA","id": "CCC","next": "BBB","prev": "AAA"},
{"value": "SCS","id": "FFF","next": "","prev": "EEE"}
]
await Promise.all(
Items.map(async (elm) => {
const params = {
TableName: TABLE_NAME,
Item: elm
};
await DOC_CLIENT.put(params).promise();
})
);
// データ参照
const TABLE_PARAMS = {
TableName: TABLE_NAME,
ExpressionAttributeNames: {
"#next": "next"
},
ExpressionAttributeValues: {
":len": 0
},
FilterExpression: "size (#next) = :len",
};
const scannedData = await DOC_CLIENT.scan(TABLE_PARAMS, () => {}).promise();
console.log(JSON.stringify(scannedData));
}
main();
実行結果:
$ node ddb-scan.js
{"Items":[{"value":"SCS","id":"FFF","next":"","prev":"EEE"}],"Count":1,"ScannedCount":6}
まとめ
- 空文字列はsizeで取得できる
- 文字数の条件は
ExpressionAttributeValues
を使う必要がある
参考
- Comparison Operator and Function Reference - Amazon DynamoDB https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/Expressions.OperatorsAndFunctions.html
Discussion