0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DynamoDB サンプルデータ投入手順(BatchWrite)

0
Posted at

概要

DynamoDB の batch-write-item を使ってサンプルデータを一括投入しようとしたところ、
Windows環境特有の問題でハマったので、手順と対策をまとめます。

前提

対象テーブル: dev-tcoev-simulations

PK: corporation_id

SK: simulation_id

AWS CLI または Node.js 実行環境あり

※適宜、自分の環境に読み替えてください

結論(重要)

⚠️ Windows環境では AWS CLI の file:// が壊れやすいです

特に以下に注意:

文字コード(UTF-8 / BOM)

日本語文字

PowerShell / Git Bash の挙動差

👉 最も安定する方法は Node.js 経由で投入すること

方法①(参考)AWS CLI
コマンド
aws dynamodb batch-write-item
--request-items file://sample.json
よくあるエラー
❌ エラー1
Unable to load paramfile ... could not be decoded
原因

UTF-8(BOM付き)

UTF-16

日本語文字

❌ エラー2
'in ' requires string as left operand
原因

CLIのパース失敗(文字コード or 不可視文字)

回避策
✔ fileb を使う
aws dynamodb batch-write-item
--request-items fileb://sample.json
✔ 日本語を使わない
EV_Simulation_Tokyo_01
方法②(推奨)Node.jsで投入
メリット

文字コード問題を回避できる

JSONの検証がしやすい

デバッグしやすい

実装手順
① スクリプト作成

seed-batch.js

const fs = require("fs");
const path = require("path");
const { DynamoDBClient, BatchWriteItemCommand } = require("@aws-sdk/client-dynamodb");

async function main() {
const filePath = path.join(__dirname, "sample.json");
let raw = fs.readFileSync(filePath, "utf8");

// BOM除去(重要)
if (raw.charCodeAt(0) === 65279) {
raw = raw.slice(1);
}

const requestItems = JSON.parse(raw);

const client = new DynamoDBClient({
region: "ap-northeast-1"
});

const command = new BatchWriteItemCommand({
RequestItems: requestItems
});

const result = await client.send(command);

console.log("Batch write completed");
console.log(JSON.stringify(result, null, 2));

if (Object.keys(result.UnprocessedItems || {}).length > 0) {
console.warn("UnprocessedItemsあり");
}
}

main().catch(console.error);
② 実行
node seed-batch.js

※同じディレクトリに sample.json がある前提

BOM問題について(重要)
症状
SyntaxError: Unexpected token

または

not valid JSON
原因

ファイル先頭に BOM が含まれている

U+FEFF (65279)
確認方法
node -e "const fs=require('fs'); const s=fs.readFileSync('sample.json','utf8'); console.log(s.charCodeAt(0));"
対応
if (raw.charCodeAt(0) === 65279) {
raw = raw.slice(1);
}
JSON形式について
DynamoDB形式(今回使用)
{
"corporation_id": { "S": "corp-001" }
}
DocumentClient形式(参考)
{
"corporation_id": "corp-001"
}
制約

BatchWriteItem 最大25件

今回は20件 → OK

トラブルまとめ
問題 原因 解決
CLIでdecodeエラー BOM / UTF-16 fileb or Node
JSON parseエラー BOM slice(1)
日本語で失敗 CLIの文字コード問題 英数字にする
20件で失敗 JSON汚染 Nodeで検証
推奨運用
開発・検証時

Node.jsでseed投入

本番運用

スクリプト化 + retry対応

ベストプラクティス

JSONはUTF-8(BOMなし)

CLI使用時は日本語を避ける

seedデータはコード化(再現性確保)

まとめ

AWS CLIは便利だが環境依存が強い

Windows環境では特にハマりやすい

Node.jsでの投入が最も安定・再現性が高い

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?