DynamoDB QueryPage Mapper学习

DynamoDB QueryPage Mapper

@DynamoDBTypeConverted用法
  • 当我们使用一下DynamoDB Mapper时,可能会用到DynamoDBTypeConverter转换类。
    • Object不能自动给你转为你想要的类型
@DynamoDBTypeConverted(converter = MeshInfoConverter.class)
    @DynamoDBAttribute(attributeName = "meshInfo")
    public List<Map<String, Object>> getMeshInfo() {
        return meshInfo;
    }
  • DynamoDBTypeConverter.java实现:
/**
 * 转换类
 * Created by hyman on 2017/8/16.
 */
public class MeshInfoConverter implements DynamoDBTypeConverter<List<Map<String, AttributeValue>>, List<Map<String, Object>>> {


    @Override
    public List<Map<String, AttributeValue>> convert(List<Map<String, Object>> maps) {
        List<Map<String, AttributeValue>> list = new ArrayList<>();
        if (!maps.isEmpty()) {
            for (int i = 0; i < maps.size(); i++) {
                Map<String, AttributeValue> map = new HashMap<>();
                for (Map.Entry<String, Object> entry : maps.get(i).entrySet()) {
                    if ("deviceIds".equals(entry.getKey())) {
//                        map.put(entry.getKey(),new AttributeValue().withSS(entry.getValue()));
                        List<String> deviceIds = (List<String>) entry.getValue();
                        List<AttributeValue> attributeValues = new ArrayList<>();
                        for (int j = 0; j < deviceIds.size(); j++) {
                            attributeValues.add(new AttributeValue().withS(deviceIds.get(j)));
                        }
                        map.put(entry.getKey(), new AttributeValue().withL(attributeValues));
                    } else {
                        map.put(entry.getKey(), new AttributeValue().withS(String.valueOf(entry.getValue())));
                    }
                }
                list.add(map);
            }
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> unconvert(List<Map<String, AttributeValue>> maps) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (!maps.isEmpty()) {
            for (int i = 0; i < maps.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                for (Map.Entry<String, AttributeValue> entry : maps.get(i).entrySet()) {
                    if ("deviceIds".equals(entry.getKey())) {
                        List<AttributeValue> attributeValueList = entry.getValue().getL();
                        List<String> valueList = new ArrayList<>();
                        for (int j=0;j<attributeValueList.size();j++){
                            valueList.add(attributeValueList.get(j).getS());
                        }
                        map.put(entry.getKey(), valueList);
                    } else {
                        map.put(entry.getKey(), entry.getValue().getS());
                    }
                }
                list.add(map);
            }
        }
        return list;
    }
}

QueryPage

  • 知道主键对排序键分页:
public Map<String, Object> queryVersionPolicyPage(String id, Map<String, AttributeValue> lastEvaluated) {
        Map<String, AttributeValue> eav = new HashMap<String, AttributeValue>();
        eav.put(":val1", new AttributeValue().withS(id));
        DynamoDBQueryExpression queryExpression = new DynamoDBQueryExpression()
                .withKeyConditionExpression("id = :val1")
                .withExpressionAttributeValues(eav).withExclusiveStartKey(lastEvaluated);
        QueryResultPage list = queryPage(VersionPolicy.class, queryExpression);
        Map<String, Object> map = new HashMap<>();
        map.put("queryResultPage", list.getResults());
        map.put("lastEvaluated", list.getLastEvaluatedKey());
        return map;
    }
  • mapper
public QueryResultPage queryPage(Class<T> clazz, DynamoDBQueryExpression queryExpression){
        return DynamoDBUtils.getMapper().queryPage(clazz,queryExpression);
    }
  • 总结:目前测试出QueryPage只能对排序键进行分页。没法直接对主键进行分页查询。ScanPage则很万能,但性能消耗过大,产生费用过高。实际生产中需要我们根据实际业务逻辑去选择使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值