简体   繁体   English

Amazon Web Services,在Android程序中读取存储DynamoDB Json数据

[英]Amazon Web Services, Read store DynamoDB Json data in Android program

I store Json Data into a DynamoDB table, the data looks like this: 我将Json数据存储到DynamoDB表中,数据如下所示:

"payload": {
"M": {
  "state": {
    "M": {
      "reported": {
        "M": {
          "alive": {
            "BOOL": true
          },
          "reg": {
            "N": "0"
          },
          "timestamp": {
            "N": "1520357203.4857106"
          }
        }
      }
    }
  }
}

I need to retrieve this data to an android program, using the scan command to read all the data in the table: 我需要使用scan命令将此数据检索到android程序中,以读取表中的所有数据:

DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
      List<PcObject2DO> result = dynamoDBMapper.scan(PcObject2DO.class, scanExpression);

Where 哪里

PcObject2DO

is the class with the accesors methods that Amazon provide for the DynamoDB table, all fields in the table are strings, simply. 是具有Amazon为DynamoDB表提供的accesors方法的类,简单地说,表中的所有字段都是字符串。 I receive this error in the scan command: 我在扫描命令中收到此错误:

com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMappingException: Expected S in value {M:......

and mentioned the use of custom marshaler, the problem is retrieve the data of the field "payload" that is in DynamoDB Json format. 并提到使用自定义封送处理程序,问题是检索DynamoDB Json格式的“有效负载”字段的数据。 The documentation about read Json and the use of marshalers are vague and not cover all needs. 有关已读Json和使用警司的文档含糊不清,无法涵盖所有​​需求。 After Investigate in forums and get sample code, I tested using marshalers of many types, create a class for the payload, and also change the type of the table accesors, but the problem is still there, the data of the payload field cannot be retrieved. 在论坛上进行调查并获得了示例代码之后,我使用了多种类型的封送程序进行了测试,为有效负载创建了一个类,还更改了表加速器的类型,但是问题仍然存在,有效负载字段的数据无法检索。

I Include the payload class, the custom marshaller, and the changes made in the table accesors, the changes are commented. 我包括有效负载类,自定义编组器和表访问器中所做的更改,并对更改进行注释。 Sorry, Im new in android and AWS, I understand the concept of the marshaler and the seriealized data, but I really dont know how do this or if is possible. 抱歉,我是android和AWS中的新手,我了解封送处理程序和串行数据的概念,但我真的不知道该怎么做或是否可行。

PcObject2DO.java PcObject2DO.java

@DynamoDBTable(tableName = "xxxxxxxxxxxx-thing")

public class PcObject2DO {
    private String _reg;
    private String _timestamp;
    private String _payload;

    //private Map<String, Payload> _payload;

    //private static final JsonMarshaller<Payload>PAYLOAD_JSON_MARSHALLER    = new JsonMarshaller<Payload>();

    @DynamoDBHashKey(attributeName = "reg")
    @DynamoDBAttribute(attributeName = "reg")
    public String getReg() {
        return _reg;
    }

    public void setReg(final String _reg) {
        this._reg = _reg;
    }
    @DynamoDBRangeKey(attributeName = "timestamp")
    @DynamoDBAttribute(attributeName = "timestamp")
    public String getTimestamp() {
        return _timestamp;
    }

    public void setTimestamp(final String _timestamp) {
        this._timestamp = _timestamp;
    }
    @DynamoDBAttribute(attributeName = "payload")
    public String getPayload() {
        return _payload;
   }
    public void setPayload(final String  _payload) {
        this._payload = _payload;
    }
    /*public Set<String> getPayload() {
        if(_payload !=null){
            Set<String> jsonSet = new HashSet<String>(_payload.size());
            for(Payload data : _payload){
                String json = PAYLOAD_JSON_MARSHALLER.marshall(data);
                jsonSet.add(json);
            }
            return jsonSet;
        }else {
            return null;
        }
    }
    public void setPayload(Set<String> jsonSet) {
        if(jsonSet != null){
            _payload = new HashSet<Payload>(jsonSet.size());
            for(String json : jsonSet){
                Payload data =    PAYLOAD_JSON_MARSHALLER.unmarshall(Payload.class, json);
                _payload.add(data);
            }
        }
    }*/
    //-----
}

Payload.Java 有效载荷

public class Payload {
    private Boolean alive;
    private int reg;
    private String timestamp;

    public Boolean getAlive() {return alive;}
    public void setAlive(Boolean alive) {this.alive = alive;}

    public int getReg() {return  reg;}
    public void setReg(int reg){this.reg = reg;}

    public String getTimestamp() {return  timestamp;}
    public void  setTimestamp(String timestamp) {this.timestamp = timestamp;}
}

PayloadMarshaller.java PayloadMarshaller.java

public class PayloadMarshaller implements    DynamoDBMarshaller<List<Payload>> {

    private static final ObjectMapper mapper = new ObjectMapper();
    private static final ObjectWriter writer = mapper.writer();

    @Override
    public String marshall(List<Payload> obj ) {
        try {
            return writer.writeValueAsString(obj);
        }catch (JsonProcessingException e){
            e.printStackTrace();
            return "";
        }
     }

    @Override
    public List<Payload> unmarshall (Class<List<Payload>> clazz, String json){
        final CollectionType type =    mapper.getTypeFactory().constructCollectionType(List.class, Payload.class);
        try{
            return mapper.readValue(json, type);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}

It looks like you have a boolean instead of a String 看起来您有一个布尔值而不是字符串

 "alive": {
        "BOOL": true
      },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM