AWS DynamoDB及S3操作文档(一)
AWS DynamoDB的简单操作(连接、创建表、添加项目、删除项目、查找项目、修改项目)
1.1 AWS DynamoDB的连接
- 用户登录:
打开以下链接,输入用户名和密码
https://docs.aws.amazon.com/zh_cn/amazondynamodb/latest/developerguide/AccessingDynamoDB.html - 初始登录时,必须先创建表:
点击“服务”—“DynamoDB”—“创建表”:
设置表名称、主键(分区键)、排序键,点击“创建”,完成表的创建:
点击表前的圆形按钮,即可看到表的具体信息:
- 获取用户的Access_key_ID和Secret_access_key:
点击页面右上角的“用户名”—“我的安全凭证”:
点击左侧栏“用户”,可选择创建或删除用户:
点击已创建用户,可对用户进行相关设置及操作:
点击“安全证书”—“创建访问密钥”,即可获得用户的Access_key_ID和Secret_access_key,点击“下载.csv文件”可将一个含key的EXCEL文件下载至本地。
- 确定服务器所在区域代码:
在AWS控制台页面右上角显示“弗吉尼亚北部”,此即服务器所在区域:
可在以下页面查找服务器所在区域对应的编码:
https://docs.aws.amazon.com/zh_cn/general/latest/gr/rande.html
从该页面可知“弗吉尼亚北部”对应编码为“us-east-1”。 - 在Eclipse安装AWS Toolkit for Eclipse插件:
点击“help”—“Eclipse MarketPlace”,输入AWS,点击搜索,即可看到AWS Toolkit for Eclipse插件:
注意:在此可能出现安装不成功的情况,可参考以下教程:
https://blog.csdn.net/u013216537/article/details/78090085 - 在Eclipse新建AWS项目:
点击“file”–“new”—“other”—“AWS”—“AWS Java Project”,创建项目:
在弹出的页面输入Project Name后,点击“Config Aws profiles”,在弹出的页面中输入Access_key_ID和Secret_access_key,点击“Apply and Close”—“finish”即完成AWS项目的创建。
注意:AWS项目导出(EXPORT方式)的jar包在虚拟机环境下运行会显示“profile can not be null”,即配置文件为空,考虑是从Eclipse导出jar包时未导出相关配置?可考虑建立springboot项目。
1.2 AWS DynamoDB的简单操作
- 连接DynamoDB:
AmazonDynamoDB client= AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_WEST_1).build();
DynamoDB dynamoDB = new DynamoDB(client);
//获取需要操作的表
Table table = dynamoDB.getTable(TABLE_NAME);
- 创建表:
String tableName = "Movies";
try {
//设置表的分区键和排序键(主键和副键)
Table table = dynamoDB.createTable(tableName,
Arrays.asList(new KeySchemaElement("year", KeyType.HASH), // 设置分区键
new KeySchemaElement("title", KeyType.RANGE)), // 设置排序键
//设置分区键和排序键的数据的属性,N表示数字,S表示字符串
Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N),
new AttributeDefinition("title", ScalarAttributeType.S)),
//设置预配吞吐量,ProvisionedThroughput 参数是必填项;
new ProvisionedThroughput(10L, 10L));
table.waitForActive();
//输出表的状态和描述
System.out.println("Success. Table status: " +table.getDescription().getTableStatus());
}
- 添加新项目至特定表:
//设置新项目的分区键year和排序键title
int year = 2015;
String title = "The Big New Movie";
//申明一个map型对象为infomap,在info属性中存储plot和rating
final Map<String, Object> infoMap = new HashMap<String, Object>();
infoMap.put("plot", "Nothing happens at all.");
infoMap.put("rating", 0);
try {
//将项目添加至表table中,“year”、“title”为分区键和排序键
PutItemOutcome outcome = table.putItem(new Item().withPrimaryKey("year", year,
"title",title).withMap("info", infoMap));
}
- 从特定表中删除项目:
//设置删除项目必须满足的条件,“year”、“title”为分区键和排序键
//只有在withConditionExpression中的条件满足时才删除项目
//withValueMap中给withConditionExpression中的条件赋值
DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey(new PrimaryKey("year", year, "title",title)).withConditionExpression("info.rating <= :val") .withValueMap(new ValueMap().withNumber(":val", 5.0));
try {
table.deleteItem(deleteItemSpec);
}
- 在特定表中查找项目:
//设置查询项目满足的条件
//withFilterExpression是项目必须满足的条件
ScanSpec scanSpec = new ScanSpec().withProjectionExpression("#yr, title,info.rating")
.withFilterExpression("#yr between :start_yr and :end_yr")
.withNameMap(newNameMap().with("#yr", "year"))
.withValueMap(new ValueMap().withNumber(":start_yr",1950)
.withNumber(":end_yr", 1959));
try {
//利用迭代器输出查询到的项目
ItemCollection<ScanOutcome> items = table.scan(scanSpec);
Iterator<Item> iter = items.iterator();
while (iter.hasNext()) {
Item item = iter.next();
System.out.println(item.toString());
}
}
- 在特定表中修改(更新)项目:
//设置分区键和排序键
int year = 2015;
String title = "The Big New Movie";
//设置需要更新的项目必须满足的条件
//withPrimaryKey中设置分区键和排序键
//withUpdateExpression中设置更新的属性
//withValueMap中设置更新的属性的值,其中a为一个列表
//withReturnValues为返回值
//只有在withConditionExpression中条件满足时才会更新项目
UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey("year",year, "title", title)
.withUpdateExpression("set info.rating = :r, info.plot=:p, info.actors=:a").withConditionExpression("size(info.actors) > :num").withValueMap(new ValueMap().withNumber(":r", 5.5)
.withString(":p","Everything happens all at once.").withList(":a", Arrays.asList("Larry", "Moe", "Curly"))).withReturnValues(ReturnValue.UPDATED_NEW);
try {
UpdateItemOutcome outcome = table.updateItem(updateItemSpec);
}
以上即为DynamoDB的简单操作