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,260 points
10,137 views

1 answer to this question.

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
• 58,100 points

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer

How to view a list of branch names in AWS CodeCommit?

Hi@akhtar, You can get information about one or more ...READ MORE

answered Nov 24, 2020 in AWS by MD
• 95,460 points
795 views
0 votes
1 answer

How to view a list of repositories in AWS CodeCommit?

Hi@akhtar, You can lists all AWS CodeCommit repositories ...READ MORE

answered Nov 24, 2020 in AWS by MD
• 95,460 points
1,972 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
• 58,100 points
4,214 views
0 votes
1 answer
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
• 58,100 points
2,093 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP