Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

Ho to use contains function in DynamoDB with DynamoDBMapper Java?

I need to check list of data with contains function is DynamoDB. Following is my Java Code,

List<String> dataList = new ArrayList<>();

I am inserting the above list inside a JSON called Data.

Map<String, AttributeValue> valueMap = new HashMap<>();
valueMap.put(":v", new AttributeValue().withS(String.valueOf(dataList)));

Map<String, String> nameMap = new HashMap<>();
nameMap.put("#list", "Data.list");

DynamoDBQueryExpression<> queryExpression = new DynamoDBQueryExpression<>()
                .withFilterExpression("contains(#list, :v)")
                .withExpressionAttributeNames(nameMap)
                .withExpressionAttributeValues(valueMap)
                .withConsistentRead(false);

I used the above approach but it is not working. What I need is I need to filter data if any of the list values contains in the dynamo db inserted list values. The list data is inside Json. So what I am doing wrong here? can anybody help me? Thanks in advance.

question from:https://stackoverflow.com/questions/65901948/ho-to-use-contains-function-in-dynamodb-with-dynamodbmapper-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Your query doesn't has the withKeyConditionExpression as part of building DynamoDBQueryExpression.
  2. FilterExpression is not improving your RCUs consumption. It adds very little value. I prefer having clean query objects ( ddbMapper.query(Object) ) and filtering post getting response.

From the docs -

A filter expression is applied after a Query finishes, but before the results are returned. Therefore, a Query consumes the same amount of read capacity, regardless of whether a filter expression is present.

Note: If still looking to do it in DynamoDB query, you need to ensure that AttributeValue type provided is correct.

From the docs -

An attribute of type String Set. For example:
"SS": ["Giraffe", "Hippo" ,"Zebra"]
Type: Array of strings

So, here change from new AttributeValue().withS to new AttributeValue().withSS to apply over list. Note that, you will still need to bring your object down to AttributeValue granularity which DDB understands.

This doc has more advanced samples.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...