Reputation: 23
I've been trying to figure out how to determine the data type (in my case String or Number) of indexes and global secondary indexes of an Amazon DynamoDB table.
I'm using the java sdk to interact with DynamoDB. I've been using the TableDescription class which gives me a lot of information but seems to be lacking a way to determine the data type.
Does anyone know a way to get the data type for indexes?
Thanks
Upvotes: 2
Views: 157
Reputation: 47269
You could make a DescribeTable
call, and combine the data from the GlobalSecondaryIndexes[].KeySchema
with the data provided by the top level AttributeDefinitions
. This works because a GSI needs the definition of an attribute, which means that you can access the type from it.
Here is an example using com.amazonaws.services.dynamodbv2.document.Table
for a table I made for another StackOverflow answer, where you have access to the AttributeDefinition
:
final TableDescription description = table.describe();
System.out.println(description.getAttributeDefinitions());
final List<AttributeDefinition>
tableAttributeDefinitions =
description.getAttributeDefinitions();
for (GlobalSecondaryIndexDescription gsi : description
.getGlobalSecondaryIndexes()) {
System.out.println("IndexName: " + gsi.getIndexName());
for (KeySchemaElement keySchemaElement : gsi.getKeySchema()) {
final String attrName = keySchemaElement.getAttributeName();
final AttributeDefinition
attrDef =
tableAttributeDefinitions.stream()
.filter(ad -> ad.getAttributeName().equals(attrName)).findFirst()
.orElseThrow(RuntimeException::new);
System.out.println(
"AttributeName: " + attrDef.getAttributeName() +
", AttributeType: " + attrDef.getAttributeType());
}
}
Output:
[{AttributeName: hashAttributeName,AttributeType: S}, {AttributeName: rangeAttributeName,AttributeType: N}, {AttributeName: otherAttribute,AttributeType: N}] IndexName: myOtherAttributeGsi
AttributeName: otherAttribute, AttributeType: N
Upvotes: 1