1

I have requirement to fetch the item from dynamodb based on the order id(not the partition key).

If the order id is 123456 as mentioned the in the above example I should get this item.

Can somebody please let me know if it is possible to achieve this. If yes, how?

DynamoDB Table

@DynamoDBTable(tableName="SomeTable")
public class SomeTable {
private String id;
private String name;
private List<Order> orders;

 @DynamoDBHashKey(attributeName="id")
    public String getId() {
        return id;
    }


}

Class 2

@DynamoDBDocument
public class Order {
private String id;
private String name;
}

DynamoDB Table Data

 "Items": [
     {
         "orders": {
             "L": [
                 {
                     "M": {
                         "name": {
                             "S": "xyz"
                         },
                         "id": {
                             "S": "123456"
                         }
                     }
                 }
             ]
         },
         "id": {
             "S": "789"
         },
         "name": {
             "S": "abcd"
         }
     }

1 Answer 1

0

Do a Scan not a Query https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Scan.html

When you do the scan your AttributeValue needs to be a String Set, not just a String.

In your FilterExpression use the condition CONTAINS with your AttributeValue https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Condition.html

Sign up to request clarification or add additional context in comments.

2 Comments

Map<String, AttributeValue> map = new HashMap<>(); map.put("name", new AttributeValue().withS("xyz")); map.put("id", new AttributeValue().withS("123456")); Map<String, AttributeValue> eav2 = new HashMap<>(); eav2.put(":orders", new AttributeValue().withM(map)); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression("contains (orders, :orders)") .withExpressionAttributeValues(eav2 ); List<SomeTable > list= mapper.parallelScan(SomeTable .class, scanExpression, 1);
This above code works but I have to pass whole map but in my case I can't pass whole map, I will have only id and not the name. Is there any way I can achieve this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.