DynamoDB嵌套项插入Android
嘿,我正在尝试将DynamoDB与我的android应用程序集成,并将具有嵌套对象的项插入到数据库中,这些对象具有自己的属性,如DynamoDB嵌套项插入Android,android,database,amazon-web-services,kotlin,amazon-dynamodb,Android,Database,Amazon Web Services,Kotlin,Amazon Dynamodb,嘿,我正在尝试将DynamoDB与我的android应用程序集成,并将具有嵌套对象的项插入到数据库中,这些对象具有自己的属性,如Video对象,如下所示: User{ "Name" : "MyName" "Video": { "videoType": "BRIGHTCOVE", "v
Video
对象,如下所示:
User{
"Name" : "MyName"
"Video": {
"videoType": "BRIGHTCOVE",
"viewingTime": 156,
"videoSize": 7120441,
"url": "5378655747001",
"URL": "5378655747001"
}
"Password" : "123456"
你知道怎么做并在项目中插入项目吗?到目前为止,我只使用
document添加了正常的密钥对值,如Name
和Password
。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语言(未经测试)
publicstaticvoidmain(字符串[]args){
//创建具有所需区域的DynamoDB客户端
AmazonDynamoDB dynamoDB=createDynamoDbClient(“您的地区”);
试一试{
PutItemRequest PutItemRequest=新的PutItemRequest();
setTableName(“您的表名”);
Map item=newhashmap();
项目。投入(“PK”,新属性值(“PK”);
项目。投入(“SK”,新属性值(“SK”);
Map attributeValues=new HashMap();
AttributeValue.put(“视频类型”,新的AttributeValue(“BRIGHTCOVE”);
AttributeValue.put(“viewingTime”,新的AttributeValue(),带n(“156”);
AttributeValue.put(“videoSize”,新的AttributeValue(),带n(“7120441”);
AttributeValue.put(“url”,新的AttributeValue(“5378655747001”);
AttributeValue.put(“URL”,新的AttributeValue(“5378655747001”);
item.put(“视频”,新的AttributeValue(),带有m(AttributeValue);
putItemRequest.setItem(项目);
PutItemResult PutItemResult=dynamoDB.putItem(putItemRequest);
System.out.println(“成功放置项”);
//处理putItemResult
}捕获(例外e){
//处理错误
}
}
你能提供一个代码片段来说明它是如何实现的吗?因为在Android中使用文档API时,除了常规原语之外,它不允许使用Map
属性Set
或DynamoDBEntry
类型,所以我真的不知道如何在代码中执行它。我没有在Java中使用文档API,但我拼凑了一段代码片段来说明这个概念。
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
}
}