使用dynamodb的动态过滤搜索

时间:2018-08-01 12:26:34

标签: java amazon-dynamodb

我是DynamoDB的新手,所以我只是想在特定的表中进行简单的搜索/查询。

这些搜索是变化的,并且可以是动态的,即,我可能在查询中有几个要搜索的属性,因此我想知道是否有任何方法可以动态地构建此扫描查询。例如,我应该能够根据属性的数量进行查询:

    Map<String, String> attributeNames = new HashMap<String, String>();
    attributeNames.put("#parametro", "ID_PARAMETRO");
    attributeNames.put("#startDate", "FC_INICIO");
    attributeNames.put("#endDate", "FC_FIN");

    Map<String, AttributeValue> attributeValues = new HashMap<String, AttributeValue>();
    attributeValues.put(":idParameter", new AttributeValue().withS(idParameter!=null?idParameter:"none"));
    attributeValues.put(":startDate", new AttributeValue().withS(startDate!=null?sdf.format(startDate):"none"));
    attributeValues.put(":endDate", new AttributeValue().withS(endDate!=null?sdf.format(endDate):"none"));

    ScanRequest scanRequest = new ScanRequest()
            .withTableName(TABLE_NAME)
            .withFilterExpression("#parametro = :idParameter and #startDate = :startDate and #endDate = :endDate")
            .withExpressionAttributeNames(attributeNames)
            .withExpressionAttributeValues(attributeValues);

因此,除了添加“ if-else”语句之外,还有其他方法可以通过DynamoDB API中的某些方法来实现吗?

0 个答案:

没有答案