我在理解如何从Android应用中为dynamoDB创建List时遇到问题。
我可以从我的应用中存储并加载{
"deviceId": "920e185a-63ed-46ca-bfca-f7a484ccb708"
}
。但是然后添加List并将我的JSON对象存储为列表中的Map是我的努力。
我想创建和填充的dynamoDB结构看起来像JSON格式:
{
"data": [
{
"heartBeatId": 1,
"status": 1,
"timeStamp": 1502183502711
},
{
"heartBeatId": 2,
"status": 1,
"timeStamp": 1502183502812
}
],
"deviceId": "90563daa-63ae-40c5-905e-9e8c02f9624f"
}
然后我想用心跳对象填充数据列表,该对象是Map类型。
{
"heartBeatId": 2,
"status": 1,
"timeStamp": 1502183502812
}
因此,在定义@DynamoDBTable(tableName = "heartbeat")
类以创建此结构时遇到问题。
我在这里阅读了List和DynamoDBDocument:
@DynamoDBTable(tableName = "heartbeat")
课程现在看起来如何。
我希望有人可以帮助我定义它,或者告诉我如何继续获得我需要的结构。
package com.example.android.awsdynamoapp;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBAttribute;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBDocument;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBHashKey;
import com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBTable;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@DynamoDBTable(tableName = "heartbeat")
public class HeartBeat implements DynamoDBTable {
private String deviceId;
private Map<String, Integer> data;
private List dataList;
public HeartBeat(){};
public HeartBeat(String deviceId){this.deviceId = deviceId;};
public HeartBeat(String deviceId, List dataList) {
this.deviceId = deviceId;
this.dataList = dataList;
// this.timeStamp = timeStamp;
}
@DynamoDBHashKey(attributeName = "deviceId")
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public void setTimeStamp(String timeStamp) {
//this.timeStamp = timeStamp;
}
public void setData(String deviceId) {
Map<String, Integer> data = new HashMap<>();
}
@DynamoDBAttribute(attributeName = "data")
public List getData() {
return this.dataList;
}
@DynamoDBDocument
public static class JsonArray {
private String heartBeatId;
private String status;
private String timeStamp;
}
/**
* The name of the table to use for this class.
*/
@Override
public String tableName() {
return "heartbeat";
}
@Override
public Class<? extends Annotation> annotationType() {
return null;
}
}
答案 0 :(得分:2)
可以按如下方式创建地图对象列表。上述结构中的好处是data
中的所有属性都是数字。如果需要,您可以将其更改为String。根据您的预期输出,我将其定义为Long
。
属性定义: -
private List<Map<String, Long>> data;
@DynamoDBAttribute(attributeName = "data")
public List<Map<String, Long>> getData() {
return data;
}
public void setData(List<Map<String, Long>> data) {
this.data = data;
}
填充数据: -
MoviesWithData movies = new MoviesWithData();
movies.setYearKey(1917);
movies.setTitle("Tilte with list of map");
Map<String, Long> dataMap1 = new HashMap<>();
dataMap1.put("heartBeatId", 1L);
dataMap1.put("status", 1L);
dataMap1.put("timeStamp", 1502183502812L);
Map<String, Long> dataMap2 = new HashMap<>();
dataMap2.put("heartBeatId", 2L);
dataMap2.put("status", 2L);
dataMap2.put("timeStamp", 1503183502812L);
movies.setData(Arrays.asList(dataMap1, dataMap2));
dynamoDBMapper.save(movies, consistentDynamoDBMapperConfig);
示例输出: -