DynamoDBMapper:使用嵌套对象属性过滤器查询/扫描

时间:2018-10-10 06:16:42

标签: java amazon-web-services amazon-dynamodb dynamodb-queries

我正在创建一个DAO类,该类具有一个API来逐页获取产品。对API的请求将包含过滤器列表。过滤按原语和字符串属性的预期进行。

@Getter
@Setter
@DynamoDBTable(tableName = "Entity")
public abstract class EntityDO {

    @DynamoDBHashKey(attributeName = "uid")
    @DynamoDBAutoGeneratedKey
    private String uid;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@DynamoDBDocument
@DynamoDBTable(tableName = "CATEGORIES")
public class CategoryDO extends EntityDO {

    @DynamoDBAttribute(attributeName = "name")
    private String name;

    @DynamoDBAttribute(attributeName = "description")
    private String description;

    @DynamoDBAttribute(attributeName = "imageUrl")
    private String imageUrl;
}

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@DynamoDBTable(tableName = "bs-PRODUCTS")
public class ProductDO extends EntityDO {

    @DynamoDBAttribute(attributeName = "name")
    private String name;

    @DynamoDBAttribute(attributeName = "description")
    private String description;

    @DynamoDBAttribute(attributeName = "longDescription")
    private String longDescription;

    @DynamoDBAttribute(attributeName = "category")
    private CategoryDO category;

    @DynamoDBAttribute(attributeName = "imageUrl")
    private String imageUrl;

    @DynamoDBAttribute(attributeName = "mrp")
    private float mrp;

    @DynamoDBAttribute(attributeName = "discount")
    private float discount;

    @DynamoDBAttribute(attributeName = "tags")
    private List<String> tags;
}


public class ProductDAO extends EntityDAO<ProductDO> {

    @Autowired
    private Logger logger;

    @Autowired
    private DynamoDBMapper dynamoDBMapper;

    @Autowired
    private Map<Filter.Operation, ComparisonOperator> operatorMap;

    @Autowired
    private ProductConverter converter;

    @Override
    public Optional<ProductDO> get(String id) {
        return Optional.ofNullable(dynamoDBMapper.load(ProductDO.class, id));
    }

    @Override
    public List<ProductDO> getAll() {
        return dynamoDBMapper.scan(ProductDO.class, new DynamoDBScanExpression());
    }

    @Override
    public List<ProductDO> get(List<Filter> filters, String previousPageLastKey, int count) {

        Map<String, Condition> scanFilters = new HashMap<>();
        for (Filter filter: filters) {
            ComparisonOperator comparisonOperator = operatorMap.get(filter.getOperation());
            Condition condition = new Condition()
                    .withComparisonOperator(comparisonOperator)
                    .withAttributeValueList(new AttributeValue().withS(filter.getAttributeValue()));
            scanFilters.put(filter.getAttributeName(), condition);
        }

        DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
                .withLimit(count)
                .withScanFilter(scanFilters);
        if (previousPageLastKey != null) {
            Map<String, AttributeValue> exclusiveStartKey = new HashMap<>();
            exclusiveStartKey.put(Constants.UID, new AttributeValue().withS(previousPageLastKey));
            scanExpression.setExclusiveStartKey(exclusiveStartKey);
        }

        return dynamoDBMapper.scan(ProductDO.class, scanExpression);
    }
}

在ScanExpression中应进行哪些更改以对category.uid进行过滤?

我尝试将属性名称传递为category.uid,但没有帮助。

如果这种方法的设计不正确,那么我很乐意得到批评家的意见。如果有多种方法,则可以详细说明其优缺点。

此外,我在AWS控制台上尝试过它,在那也不起作用。 我的桌子看起来像下面 All products

其中一种产品具有如下内容 Check category field, which is a map

检查类别字段,这是一个包含nameuid的地图。我尝试搜索类别名称,但未显示任何结果。 enter image description here

AWS DynamoDB甚至支持对嵌套属性进行过滤。

1 个答案:

答案 0 :(得分:0)

我用ScanFilters陷入了僵局,基本上它们不支持嵌套属性来搜索Dynamodb文档(嵌套对象),这是做过滤的旧方法,而FilterExpressions替代了它。

FilterExpressions也支持嵌套属性。过滤器表达式基本上是字符串,具有属性名称和属性值的占位符。人们可能不会选择使用占位符,但在dynamodb中有很多保留关键字,建议使用此方法。这些占位符是使用地图expressionAttributeNamesexpressionAttributeValues提供的。

FilterExpression看起来像#category.#uid = :categoryuid,其中我的属性名称映射看起来像{"#category":"category","#uid":"uid"},而属性值映射看起来像{":categoryuid":{"s":"1d5e9cea-3c4d-4a73-8e1e-aeaa868b9d89"}}

这对我来说很好,为了更好地组织代码,我写下了包装器类

public class FilterExpression {

    private List<Filter> filters;

    private String filterExpression;

    private Map<String, AttributeValue> attributeValues;

    private Map<String, String> attributeNames;

    public FilterExpression(List<Filter> filters) {
        this.filters = filters;
        populateFilterExpression();
    }

    private void populateFilterExpression() {
        StringBuilder filterExpressionBuilder = new StringBuilder();
        attributeNames = new HashMap<>();
        attributeValues = new HashMap<>();

        for (Filter filter: filters) {
            if (filterExpressionBuilder.length() > 0) {
                filterExpressionBuilder.append(" AND ");
            }
            String attributeName = filter.getAttributeName();
            String[] attributes = attributeName.split("\\.");

            StringBuilder expNestedAttributes = new StringBuilder();
            for (String attributeInPath: attributes) {
                attributeNames.put("#"+attributeInPath, attributeInPath);
                if(expNestedAttributes.length() > 0) {
                    expNestedAttributes.append(".");
                }
                expNestedAttributes.append("#" + attributeInPath);
            }

            String attributeValueKey = ":" + String.join("", attributes);

            AttributeValue attributeValue;
            switch (filter.getAttributeType()) {
                case STRING:
                    attributeValue = new AttributeValue().withS(filter.getAttributeValue());
                    break;

                case NUMBER:
                    attributeValue = new AttributeValue().withN(filter.getAttributeValue());
                    break;

                default:
                    throw new UnsupportedOperationException("The attribute type is not supported");
            }
            attributeValues.put(attributeValueKey, attributeValue);

            switch (filter.getOperation()) {
                case EQ:
                    filterExpressionBuilder.append(expNestedAttributes);
                    filterExpressionBuilder.append(" = ");
                    filterExpressionBuilder.append(attributeValueKey);
                    break;

                case GE:
                    filterExpressionBuilder.append(expNestedAttributes);
                    filterExpressionBuilder.append(" >= ");
                    filterExpressionBuilder.append(attributeValueKey);
                    break;

                case LE:
                    filterExpressionBuilder.append(expNestedAttributes);
                    filterExpressionBuilder.append(" <= ");
                    filterExpressionBuilder.append(attributeValueKey);
                    break;

                case GT:
                    filterExpressionBuilder.append(expNestedAttributes);
                    filterExpressionBuilder.append(" > ");
                    filterExpressionBuilder.append(attributeValueKey);
                    break;

                case LT:
                    filterExpressionBuilder.append(expNestedAttributes);
                    filterExpressionBuilder.append(" < ");
                    filterExpressionBuilder.append(attributeValueKey);
                    break;

                case STARTS_WITH:
                    filterExpressionBuilder.append("begins_with (");
                    filterExpressionBuilder.append(expNestedAttributes);
                    filterExpressionBuilder.append(", ");
                    filterExpressionBuilder.append(attributeValueKey);
                    filterExpressionBuilder.append(")");
                    break;

                case CONTAINS:
                    filterExpressionBuilder.append("contains (");
                    filterExpressionBuilder.append(expNestedAttributes);
                    filterExpressionBuilder.append(", ");
                    filterExpressionBuilder.append(attributeValueKey);
                    filterExpressionBuilder.append(")");
                    break;

                default:
                    throw new UnsupportedOperationException("filter is not supported");
            }
        }

        filterExpression = filterExpressionBuilder.toString();
    }

    public String getFilterExpression() {
        return filterExpression;
    }

    public Map<String, AttributeValue> getAttributeValues() {
        return attributeValues;
    }

    public Map<String, String> getAttributeNames() {
        return attributeNames;
    }

    @Override
    public String toString() {
        return filterExpression;
    }
}

和我的Filter如下所示:

public class Filter {

    private String attributeName;

    private String attributeValue;

    private Operation operation;

    private AttributeType attributeType;

    public enum Operation {
        EQ, GE, LE, GT, LT, CONTAINS, STARTS_WITH
    }

    public enum AttributeType {
        STRING, NUMBER
    }
}
DAO中的

片段看起来像

public List<ProductDO> get(List<Filter> filters, String previousPageLastKey, int count) {

    FilterExpression filterExpression = new FilterExpression(filters);
    DynamoDBScanExpression scanExpression = new DynamoDBScanExpression()
            .withLimit(count)
            .withFilterExpression(filterExpression.getFilterExpression())
            .withExpressionAttributeNames(filterExpression.getAttributeNames())
            .withExpressionAttributeValues(filterExpression.getAttributeValues());

    dynamoDBMapper.scan(ProductDO.class, scanExpression)
}

将其发布,因为我没有找到与此相关的解决方案。