Search code examples
javaamazon-dynamodbdynamodb-queries

How to make a complex batch read query using DynamoDB enhanced client?


I'm using the DynamoDB enhanced client for Java.

I have a DynamoDB table where partition key is S: product_id and sort key is S: created_date.

I have a list of IDs List<ProductId>.

I want to get all items from the table where product_id is in List<ProductId> and since created_date > someDate.

So, for example if I have items like

[
  {
    "product_id": "aaa",
    "created_date": "2023-04-01T22:57:12.942Z"
  },
  {
    "product_id": "bbb",
    "created_date": "2023-04-02T22:57:12.942Z"
  },
  {
    "product_id": "aaa",
    "created_date": "2023-01-03T22:57:12.942Z"
  },
  {
    "product_id": "ccc",
    "created_date": "2023-04-03T22:57:12.942Z"
  },
  {
    "product_id": "bbb",
    "created_date": "2023-04-01T22:57:12.942Z"
  }
]

my List<ProductId> is ["aaa", "bbb"], someDate is 2023-04-01T22:57:12.942Z, then I want to get back

[
  {
    "product_id": "aaa",
    "created_date": "2023-04-02T22:57:12.942Z"
  },
  {
    "product_id": "bbb",
    "created_date": "2023-04-02T22:57:12.942Z"
  },
  {
    "product_id": "bbb",
    "created_date": "2023-04-03T22:57:12.942Z"
  }
]

How do I achieve this with the DynamoDB enhanced client?


Solution

  • In DynamoDB you cannot do a BatchGetItem without providing the entire primary key (partition and sort key) for each item. So your use case where you want greater than created_date will not work.

    If you do know the entire key, you can use this code example to help get you started.