目前网上的攻略讲的不详细,且大多为使用Node.js进行操作,本文将介绍一种仅在java和Windows环境下使用DynamoDB的步骤
安装Java环境及配置环境变量不再赘述。
jdk8下载地址
DynamoDB下载地址
选择地区下载.zip文件即可
下载完成后解压文件,在解压后的目录使用使用PowerShell运行如下命令
java -D"java.library.path=./DynamoDBLocal_lib" -jar DynamoDBLocal.jar
运行结果如图
AWS CLI 下载地址
注意,该版本为V2,建议使用最新版DynamoDB
傻瓜式安装,可以自行选择安装地址
确认Dynamo DB 启动,打开CMD
输入aws,出现提示即为安装成功
D:\devtools>aws configure
AWS Access Key ID [none]: 1
AWS Secret Access Key [none]: 1
Default region name [none]: cn-north-1
Default output format [none]: json
D:\devtools>aws dynamodb list-tables --endpoint-url http://localhost:8000
{
"TableNames": []
}
可以看到当前表为空
使用AWS官方文档提供的例子来创建表,注意,为方便阅读添加了换行,不可直接执行
aws dynamodb create-table \
--table-name Music \
--attribute-definitions \
AttributeName=Artist,AttributeType=S \
AttributeName=SongTitle,AttributeType=S \
--key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
--table-class STANDARD
可执行命令如下,在本地环境下操作均需要添加–endpoint属性
aws dynamodb create-table --table-name Music --attribute-definitions AttributeName=Artist,AttributeType=S AttributeName=SongTitle,AttributeType=S --key-schema AttributeName=Artist,KeyType=HASH AttributeName=SongTitle,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 --table-class STANDARD --endpoint-url http://localhost:8000
执行结果如下,配置输出以JSON形式展示
{
"TableDescription": {
"AttributeDefinitions": [
{
"AttributeName": "Artist",
"AttributeType": "S"
},
{
"AttributeName": "SongTitle",
"AttributeType": "S"
}
],
"TableName": "Music",
"KeySchema": [
{
"AttributeName": "Artist",
"KeyType": "HASH"
},
{
"AttributeName": "SongTitle",
"KeyType": "RANGE"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": "2022-09-06T11:12:12.387000+08:00",
"ProvisionedThroughput": {
"LastIncreaseDateTime": "1970-01-01T08:00:00+08:00",
"LastDecreaseDateTime": "1970-01-01T08:00:00+08:00",
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
},
"TableSizeBytes": 0,
"ItemCount": 0,
"TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/Music"
}
}
命令如下,为方便阅读添加分行.可以看到在windows系统下使用JSON是多么的难搞
aws dynamodb put-item ^
--table-name Music ^
--item ^
"{\"Artist\": {\"S\": \"No One You Know\"}, \"SongTitle\": {\"S\": \"Call Me Today\"}, \"AlbumTitle\": {\"S\": \"Somewhat Famous\"}, \"Awards\": {\"N\": \"1\"}}" ^
--endpoint-url http://localhost:8000
//下面的是在Linux下执行的,建议使用文件
aws dynamodb put-item
--table-name Music \
--item \
'{"Artist": {"S": "No One You Know"}, "SongTitle": {"S": "Howdy"}, "AlbumTitle": {"S": "Somewhat Famous"}, "Awards": {"N": "2"}}'
我们也可以使用文件插入数据
命令行如下
D:\devtools>aws dynamodb put-item --table-name Music --item file://C:\Users\akon\Desktop\1.json --endpoint-url http://localhost:8000
不报错代表插入成功
命令行,可以看到读到了刚才的文件
D:\devtools>aws dynamodb get-item --consistent-read --table-name Music --key "{ \"Artist\": {\"S\": \"No One You Know\"}, \"SongTitle\": {\"S\": \"Call Me Today\"}}" --endpoint-url http://localhost:8000
{
"Item": {
"Artist": {
"S": "No One You Know"
},
"AlbumTitle": {
"S": "Somewhat Famous"
},
"Awards": {
"N": "1"
},
"SongTitle": {
"S": "Call Me Today"
}
}
}
参考网址
参考https://blog.csdn.net/Dracarys_/article/details/110628208