Reputation: 942
So, I have this scanExpression that returns a list of Blogs depending on the query passed in.
When I pass in the query string "Java", for example, it will return the Blog "Starting out with Java".
But, if I pass the query string "Java tutorials", nothing is returned. So basically, I want blogs to be returns that contain the words "Java" or "Tutorial".
public List<BlogDetailsEntity> searchBlogs(String query) {
try {
DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
scanExpression.addFilterCondition("title", new Condition()
.withComparisonOperator(ComparisonOperator.CONTAINS)
.withAttributeValueList(new AttributeValue().withS(query.toLowerCase())));
return dynamoDBMapper.scan(BlogDetailsEntity.class, scanExpression);
} catch (Exception ex) {
log.error("failed to get blogs > " + query);
}
return null;
}
Upvotes: 2
Views: 2571
Reputation: 10704
To query DynamoDB data, you should look at using the AWS SDK for Java V2 API and the Enhanced Client. Your code is the older version 1 and the Amazon SDK team recommends V2.
The AWS SDK for Java 2.x is a major rewrite of the version 1.x code base. It’s built on top of Java 8+ and adds several frequently requested features. These include support for non-blocking I/O and the ability to plug in a different HTTP implementation at run time.
More about the Enhanced Client here:
Using the Enhanced Client, you can use Expression objects to get a result set that you want. To see different ways to retrieve data by using V2 and the Enhanced Client, see this code example.
If you are not familiar with the AWS Java V2 API, see this doc topic in the Java V2 Developer Guide:
Get started with the AWS SDK for Java 2.x
Using DynamoDB V2 API, you can perform your use case. Given this table.
I can pull up records where the word issue is in title using this Java code:
AttributeValue attVal = AttributeValue.builder()
.s("issue")
.build();
Map<String, AttributeValue> myMap = new HashMap<>();
myMap.put(":val1", attVal);
Map<String, String> myExMap = new HashMap<>();
myExMap.put("#title", "title");
Expression expression = Expression.builder()
.expressionValues(myMap)
.expressionNames(myExMap)
.expression("contains(#title, :val1)")
.build();
ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder()
.filterExpression(expression)
.limit(15)
.build();
// Get items in the Issues table.
Iterator<Issues> results = table.scan(enhancedRequest).items().iterator();
while (results.hasNext()) {
Issues issue = results.next();
System.out.println("The record description is " + issue.getDescription());
System.out.println("The record title is " + issue.getTitle());
}
Also - I noticed that you tagged Spring BOOT. To learn how to write a Spring BOOT web app that uses the DynamoDB API V2, see:
Creating the DynamoDB web application item tracker
Upvotes: 4