android - DynamoDB 嵌套项目插入 Android
问题描述
嘿,我正在尝试将 DynamoDB 与我的 android 应用程序集成,并将项目插入到具有嵌套对象的数据库中,这些对象具有自己的属性Video
,如下所示:
User{
"Name" : "MyName"
"Video": {
"videoType": "BRIGHTCOVE",
"viewingTime": 156,
"videoSize": 7120441,
"url": "5378655747001",
"URL": "5378655747001"
}
"Password" : "123456"
知道如何完成并在项目中插入项目吗?到目前为止,我只设法添加了普通的密钥对值,例如Name
and Password
withdocument.put
方法
解决方案
put
操作参数需要如下所示。该Video
属性是一个Map
简单的键/值对。
{
"TableName": "YOUR_TABLE_NAME",
"Item": {
"Name": {
"S": "MyName"
},
"Password": {
"S": "123456"
},
"Video": {
"M": {
"videoType": {
"S": "BRIGHTCOVE"
},
"viewingTime": {
"N": "156"
},
"videoSize": {
"N": "7120441"
},
"url": {
"S": "5378655747001"
},
"URL": {
"S": "5378655747001"
}
}
}
}
}
在 Java 中(未测试)
public static void main(String[] args) {
// Create the DynamoDB Client with the region you want
AmazonDynamoDB dynamoDB = createDynamoDbClient("YOUR REGION");
try {
PutItemRequest putItemRequest = new PutItemRequest();
putItemRequest.setTableName("YOUR TABLE NAME");
Map<String, AttributeValue> item = new HashMap<String, AttributeValue>();
item.put("PK", new AttributeValue("PK"));
item.put("SK", new AttributeValue("SK"));
Map<String, AttributeValue> attributeValues = new HashMap<String, AttributeValue>();
attributeValues.put("videoType", new AttributeValue("BRIGHTCOVE"));
attributeValues.put("viewingTime", new AttributeValue().withN("156"));
attributeValues.put("videoSize", new AttributeValue().withN("7120441"));
attributeValues.put("url", new AttributeValue("5378655747001"));
attributeValues.put("URL", new AttributeValue("5378655747001"));
item.put("Video", new AttributeValue().withM(attributeValues);
putItemRequest.setItem(item);
PutItemResult putItemResult = dynamoDB.putItem(putItemRequest);
System.out.println("Successfully put item.");
// Handle putItemResult
} catch (Exception e) {
// handle errors
}
}
推荐阅读
- javascript - 使用 reactjs 更改当前页面位置
- r - 是否可以在某些语言环境中的 data.frame/data.table 中使用 unicode 列名?
- node.js - 服务端通过socket.io发送的数据显示异常
- javascript - date.setHours() 函数未将时间完全设置为零
- java - SAML 中的 SLO 是否应该适用于不同的浏览器?
- sh - 如何在 CloudBuild 中进行字符串比较?
- python - 在 Python 中实现数值积分方法
- python - 打开目录时出现 PermissionError
- flutter - 难以将商品添加到购物车 - Flutter
- node.js - 我是否需要安装 node.js 才能在 Rails 上使用 Ruby?