DynamoDB List type(DynamoDB 列表类型)
问题描述
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 列表类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:DynamoDB 列表类型
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01