title: AWS DynamoDbMapper FilterExpression pagination in Java
body: <p>I have a dynamoDb table, the table has couple of columns,<br>
  My <strong>Primary partition key is - pageId (String)</strong>,<br>
  I also have a <strong>GSI - pageContainer(String)</strong>.<br></p>
  
  <p>I am using DynamoDBMapper to query/scan my table,<br>
  I am trying to achieve <strong>pagination in DynamoDb</strong>,<br>
  I understnd paganation in DynamoDB works on <strong>ExclusiveStartKey and LastEvaluatedKey.</strong><br></p>
  
  <p>ExclusiveStartKey will be null on the 1st page query/scan,
  After querying/scanning the 1st page, DynamoDB will return the LastEvaluatedKey.<br></p>
  
  <p>If the query result is more than 1Mb or if one has put a limit for the query/scan,<br>
  <strong>The LastEvaluatedKey's value, is then used as ExclusiveStartKey to query/scan the 2nd page and so on</strong>.<br></p>
  
  <p>My operation requires a <strong>Filter Expression beginsWith</strong>, on <strong>pageContainer column</strong>,<br>
  as I am trying to get data that beginsWith a certain word, it's like a "Like" condition is compared to relational db.<br>
  <strong>I have 10 values in the table all start with "Test_test" in the pageContainer column.</strong><br></p>
  
  <p><strong>Here's my code -</strong> </p>
  
  <pre><code>AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
                  new AwsClientBuilder.EndpointConfiguration(DYNAMO_DB_ENDPOINT, DYNAMO_DB_REGION))
                  .build();
  DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBClient);
  
  String beginWith = "Test_test";
  HashMap&lt;String, AttributeValue&gt; filterMap = new HashMap&lt;&gt;();
          filterMap.put(":beginWith", new AttributeValue().withS(beginWith));
  
  DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();   
  
  scanExpression.withFilterExpression("begins_with(pageContainer,:beginWith)")
                  .withExpressionAttributeValues(filterMap)
                  .withExclusiveStartKey(null)
                  .withLimit(4);
  
  ScanResultPage&lt;PageModel&gt; scanPage = mapper.scanPage(PageModel.class, scanExpression);
  List&lt;PageModel&gt; pageCmsDomains = scanPage.getResults();
  
  Map&lt;String, AttributeValue&gt; lastEvaluatedKey = scanPage.getLastEvaluatedKey();
  System.out.println("LastEvaluatedKey=" + scanPage.getLastEvaluatedKey());
  </code></pre>
  
  <p><strong>Problem -</strong><br> 
  The scan operation is returning me the LastEvaluatedKey but, 
  nothing is coming in results(empty response).<br></p>
  
  <p><strong>I am referring the following resources -</strong><br>
  1. <a href="https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.QueryScanExample.html" rel="nofollow noreferrer">https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBMapper.QueryScanExample.html</a><br>
  2. <a href="https://stackoverflow.com/questions/32788959/pagination-with-dynamodbmapper-java-aws-sdk">Pagination with DynamoDBMapper Java AWS SDK</a></p>