Skip to content
Advertisement

Query DynamoDb Global Secondary Index

i am trying out dynamodb locally and got the following table:

"Table": {
    "AttributeDefinitions": [
        {
            "AttributeName": "hashKey",
            "AttributeType": "S"
        },
        {
            "AttributeName": "sortKey",
            "AttributeType": "S"
        },
        {
            "AttributeName": "full_json",
            "AttributeType": "S"
        }
    ],
    "TableName": "local",
    "KeySchema": [
        {
            "AttributeName": "hashKey",
            "KeyType": "HASH"
        },
        {
            "AttributeName": "sortKey",
            "KeyType": "RANGE"
        }
    ],
    "TableStatus": "ACTIVE",
    "CreationDateTime": "2021-10-01T15:18:04.413000+02:00",
    "ProvisionedThroughput": {
        "LastIncreaseDateTime": "1970-01-01T01:00:00+01:00",
        "LastDecreaseDateTime": "1970-01-01T01:00:00+01:00",
        "NumberOfDecreasesToday": 0,
        "ReadCapacityUnits": 5,
        "WriteCapacityUnits": 1
    },
    "TableSizeBytes": 1066813,
    "ItemCount": 23,
    "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/local",
    "GlobalSecondaryIndexes": [
        {
            "IndexName": "sortKeyIndex",
            "KeySchema": [
                {
                    "AttributeName": "sortKey",
                    "KeyType": "HASH"
                }
            ],
            "Projection": {
                "ProjectionType": "ALL"
            },
            "IndexStatus": "ACTIVE",
            "ProvisionedThroughput": {
                "ReadCapacityUnits": 10,
                "WriteCapacityUnits": 1
            },
            "IndexSizeBytes": 1066813,
            "ItemCount": 23,
            "IndexArn": "arn:aws:dynamodb:ddblocal:000000000000:table/local/index/sortKeyIndex"
        }
    ]
}

I want to query it with Java like this:

Index index = table.getIndex("sortKeyIndex");
ItemCollection<QueryOutcome> items2 = null;
QuerySpec querySpec = new QuerySpec();
querySpec.withKeyConditionExpression("sortKey > :end_date")
                .withValueMap(new ValueMap().withString(":end_date","2021-06-30T07:49:22.000Z"));
items2 = index.query(querySpec);

But it throws a Exception with “QUery Key Condition not supported”. I dont understand this, because in the docs, the “<” operator is described as regular operation. Can anybody help me

Advertisement

Answer

DDB Query() requires a key condition that includes an equality check on the hash/partition key.

You must provide the name of the partition key attribute and a single value for that attribute. Query returns all items with that partition key value. Optionally, you can provide a sort key attribute and use a comparison operator to refine the search results.

In other words, the only time you can really use Query() is when you have a composite primary key (hash + sort).

Without a sort key specified as part of the key for the table/GSI, Query() acts just like GetItem() returning a single record with the given hash key.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement