问题描述
I have problems understanding how to create a List for dynamoDB from my Android app.
I can store and load the {
"deviceId": "920e185a-63ed-46ca-bfca-f7a484ccb708"
}
from my app. But then to add the List and store my JSON objects as Map in the List is were i struggle.
The dynamoDB structure I want to create and populate looks like this in a JSON format:
{
"data": [
{
"heartBeatId": 1,
"status": 1,
"timeStamp": 1502183502711
},
{
"heartBeatId": 2,
"status": 1,
"timeStamp": 1502183502812
}
],
"deviceId": "90563daa-63ae-40c5-905e-9e8c02f9624f"
}
Then I want to populate the data List with the heartbeat object which is of Map type.
{
"heartBeatId": 2,
"status": 1,
"timeStamp": 1502183502812
}
So im having problems defining the @DynamoDBTable(tableName = "heartbeat")
class to create this structure.
I have been reading about the List and DynamoDBDocument here:
DynamoDBDocument
DataType List
This how the @DynamoDBTable(tableName = "heartbeat")
class looks right now.
I hope someone can help me define it or give me clues on how to proceed getting the structure i need.
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;
}
}
The list of map object can be created as follows. The good thing in the above structure is that all the attributes in data
is a Number. You can change it to String if needed as well. Based on your expected output, I have defined it as Long
.
Attribute Definition:-
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;
}
Populate the 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);
Sample output:-
这篇关于DynamoDB 列表类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!