0

I am using DynamoDBMapper to scan a a table, specifically a column named "title". I should be returned a list of blogs that contains a string passed into the scanExpression. This is code I wrote about a year and a half ago which I remember working. Maybe something has been updated since?

Thanks!

public List<BlogDetailsEntity> searchBlogs(String query) {
          DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
          scanExpression.addFilterCondition("title", new Condition()
                  .withComparisonOperator(ComparisonOperator.CONTAINS)
                  .withAttributeValueList(new AttributeValue().withS(query.toLowerCase())));
          return dynamoDBMapper.scan(BlogDetailsEntity.class, scanExpression);
      }
Clancinio
  • 734
  • 6
  • 21
  • 40

1 Answers1

2

Your code is not best practice anymore. Java V1 and this mapper should be replaced with AWS SDK for Java V2.

To get the latest code for AWS, always refer to the new AWS Code Library here.

Code examples for DynamoDB using AWS SDKs

To filter a column using the V2 enhanced client (a replacement for DynamoDBMapper), you can use DynamoDbEnhancedClient. For example, assume you want to scan a table to get all Closed items on a column named archive.

enter image description here

You can use code like this.

 // Get Open items from the DynamoDB table.
    public List<WorkItem> getOpenItems() {

        // Create a DynamoDbEnhancedClient.
        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder()
            .dynamoDbClient(getClient())
            .build();

        try{
            // Create a DynamoDbTable object.
            DynamoDbTable<Work> table = enhancedClient.table("Work", TableSchema.fromBean(Work.class));
            AttributeValue attr = AttributeValue.builder()
                .s("Open")
                .build();

            Map<String, AttributeValue> myMap = new HashMap<>();
            myMap.put(":val1",attr);

            Map<String, String> myExMap = new HashMap<>();
            myExMap.put("#archive", "archive");

            // Set the Expression so only Closed items are queried from the Work table.
            Expression expression = Expression.builder()
                .expressionValues(myMap)
                .expressionNames(myExMap)
                .expression("#archive = :val1")
                .build();

            ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder()
                .filterExpression(expression)
                .limit(15)
                .build();

            // Scan items.
            Iterator<Work> results = table.scan(enhancedRequest).items().iterator();
            WorkItem workItem ;
            ArrayList<WorkItem> itemList = new ArrayList<>();

            while (results.hasNext()) {
                workItem = new WorkItem();
                Work work = results.next();
                workItem.setName(work.getName());
                workItem.setGuide(work.getGuide());
                workItem.setDescription(work.getDescription());
                workItem.setStatus(work.getStatus());
                workItem.setDate(work.getDate());
                workItem.setId(work.getId());

                // Push the workItem to the list.
                itemList.add(workItem);
            }
            return itemList;

        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return null;
    } 

You can find a complete AWS end to end developer tutorial that teaches you how to use this code to display Amazon DynamoDB items in a React client app. For example, this illustration shows Closed items.

enter image description here

This complete doc can be found in the code lib here:

Create an Amazon Relational Database Service item tracker

smac2020
  • 9,637
  • 4
  • 24
  • 38