How to fetch a list of items (like a SQL IN clause)

0 votes

I have a table of items where each item belongs to an organization. Each item has a name and an auto-generated UUID. The name of the item is meant to be unique within an organization. Thus, I have defined my table as such:

{
  "AttributeDefinitions": [
    { "AttributeName": "EmpId",            "AttributeType": "ABC" },
    { "AttributeName": "name",             "AttributeType": "ABC" },
    { "AttributeName": "id",               "AttributeType": "ABC" }
  ],
  "TableName": "symbols",
  "KeySchema": [
    { "AttributeName": "EmpId", "KeyType": "HASH" },
    { "AttributeName": "name",  "KeyType": "RANGE" }
  ],
  "LocalSecondaryIndexes": [
    {
      "IndexName": "IdIndex",
      "KeySchema": [
        { "AttributeName": "EmpId", "KeyType": "HASH" },
        { "AttributeName": "id",    "KeyType": "RANGE" }
      ],
      "Projection": {
        "ProjectionType": "KEYS_ONLY"
      }
    }
  ],
  "ProvisionedThroughput": {
    "ReadCapacityUnits": 10,
    "WriteCapacityUnits": 5
  },
  "SSESpecification": {
    "Enabled": true
  }
}

I am trying to retrieve the set of items that match a specified list of ids. My java code looks like this:

List<AttributeValue> attributeValueList = new ArrayList<>();
String[] idList = {"XXXXXX5-4ec2-95a9-XXXXXXX", "YYYYYYYY-9a55-4c2c-b329-YYYYYYYYYYY"};
for (String id : idList) {
    attributeValueList.add(new AttributeValue().withS(id));
}

HashMap<String, Condition> keyConditions = new HashMap<>();

keyConditions.put("orgId", new Condition()
        .withComparisonOperator(ComparisonOperator.EQ)
        .withAttributeValueList(new AttributeValue("111")));

keyConditions.put("id", new Condition()
        .withComparisonOperator(ComparisonOperator.IN)
        .withAttributeValueList(attributeValueList));

QueryRequest queryRequest = new QueryRequest()
        .withTableName(Symbol.TABLE_SYMBOL)
        .withKeyConditions(keyConditions)
        .withIndexName("IdIndex")
        .withSelect("ALL_ATTRIBUTES");

QueryResult queryResult = dynamoDB.query(queryRequest);

When I run the query, it gives me the exception:

Attempted conditional constraint is not an indexable operation

Why this doesn't work?
Do I need to use a scan instead?
I want a query if possible.

Oct 26, 2018 in AWS by findingbugs
• 3,140 points
185 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

You can use this as this worked well for me.

    List<AttributeValue> attributeValueList = new ArrayList<>();
    for (String id : objectIds) {
        attributeValueList.add(new AttributeValue().withS(id));
    }

    TextFragment partitionKey = new TextFragment();
    partitionKey.setOrgId(user.getOrgId());

    Condition rangeKeyCondition = new Condition();
    rangeKeyCondition.withComparisonOperator(ComparisonOperator.IN)
            .withAttributeValueList(attributeValueList);

    DynamoDBQueryExpression<TextFragment> queryExpression = new DynamoDBQueryExpression<TextFragment>()
            .withHashKeyValues(partitionKey)
            .withQueryFilterEntry("id", rangeKeyCondition);

    List<TextFragment> itemList = mapper.query(TextFragment.class, queryExpression);
answered Oct 26, 2018 by Priyaj
• 56,100 points

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer

How to upload a lib for Tomcat in Amazon EC2?

You need to set the proper privileges ...READ MORE

answered Aug 20, 2018 in AWS by Archana
• 3,770 points
26 views
0 votes
1 answer

How to list the contents of Amazon S3 by modified date?

One easy solution would be probably to ...READ MORE

answered Aug 21, 2018 in AWS by datageek
• 2,390 points
1,795 views
+1 vote
1 answer

Error: Save Batch Record throws @DynamoDBTyped or @DynamoDBTypeConverted

There is simpler way by use annotation @DynamoDBDocument For ...READ MORE

answered Sep 26, 2018 in AWS by Priyaj
• 56,100 points
151 views
0 votes
1 answer
0 votes
1 answer

In Amazon Data Pipeline, how to make sure only once instance of a pipeline is running at any time?

On the CopyTablesActivity, you could set a lateAfterTimeout attribute ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 56,100 points
134 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.