1
votes

I am writing an app using which people can request information about specific events. Because multiple users can request information about the same event, the key for my Request table is username+event (where both of those are strings).

However, in the Java code which supplies that information, I don't want to download data I don't need so, ideally, I'd query on the event (as I don't know the usernames of all the people making the query).

I thought that if I gave the Request table a secondary index, called "event", which the users also filled in (so the hash key (called "key") is username+event, and the event field is just the event string), I'd able to query on the secondary index, and get all the requests for that event.

Here's the code:

Map<String, Condition> keyConditions = new HashMap();
keyConditions.put("event", new Condition()
                    .withComparisonOperator(EQ)
                    .withAttributeValueList(new AttributeValue().withS(event)));
QueryRequest one = new QueryRequest()
    .withTableName(REQ_TABLE)
    .withIndexName("event")
    .withKeyConditions(keyConditions);
QueryResult queryResult = dynamoDB.query(one);

All I get, however, is this error message: com.amazonaws.AmazonServiceException: Query condition missed key schema element: key

Removing the call to withIndexName("event") makes no difference.

I haven't used NoSQL before this week, which may be part of the problem.

1
can you also post the full stack trace?rpmartz

1 Answers

2
votes

Are you sure that "event" is the name of the attribute that is indexed and the name of the index?

You are using it as the attribute name in the query:

keyConditions.put("event", new Condition()
                .withComparisonOperator(EQ)
                .withAttributeValueList(new AttributeValue().withS(event)));

And you're using it as the index name:

 .withIndexName("event")

In the DynamoDB console of your AWS dashboard, check the Indexes tab to see what the name of the index is. It's been a little while since I created an index but I believe by default AWS usually postfixes the index name with _index.