You can use the AWS SDK for Java Document API to create an Amazon DynamoDB table with one or more global secondary indexes, describe the indexes on the table, and perform queries using the indexes.
The following are the common steps for table operations.
Create an instance of theDynamoDB
class.
Provide the required and optional parameters for the operation by creating the corresponding request objects.
Call the appropriate method provided by the client that you created in the preceding step.
You can create global secondary indexes at the same time that you create a table. To do this, useCreateTable
and provide your specifications for one or more global secondary indexes. The following Java code example creates a table to hold information about weather data. The partition key isLocation
and the sort key isDate
. A global secondary index namedPrecipIndex
allows fast access to precipitation data for various locations.
The following are the steps to create a table with a global secondary index, using the DynamoDB document API.
Create an instance of theDynamoDB
class.
Create an instance of theCreateTableRequest
class to provide the request information.
You must provide the table name, its primary key, and the provisioned throughput values. For the global secondary index, you must provide the index name, its provisioned throughput settings, the attribute definitions for the index sort key, the key schema for the index, and the attribute projection.
Call thecreateTable
method by providing the request object as a parameter.
The following Java code example demonstrates the preceding steps. The code creates a table (WeatherData
) with a global secondary index (PrecipIndex
). The index partition key isDate
and its sort key isPrecipitation
. All of the table attributes are projected into the index. Users can query this index to obtain weather data for a particular date, optionally sorting the data by precipitation amount.
BecausePrecipitation
is not a key attribute for the table, it is not required. However,WeatherData
items withoutPrecipitation
do not appear inPrecipIndex
.
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();DynamoDB dynamoDB = new DynamoDB(client);// Attribute definitionsArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();attributeDefinitions.add(new AttributeDefinition() .withAttributeName("Location") .withAttributeType("S"));attributeDefinitions.add(new AttributeDefinition() .withAttributeName("Date") .withAttributeType("S"));attributeDefinitions.add(new AttributeDefinition() .withAttributeName("Precipitation") .withAttributeType("N"));// Table key schemaArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();tableKeySchema.add(new KeySchemaElement() .withAttributeName("Location") .withKeyType(KeyType.HASH)); //Partition keytableKeySchema.add(new KeySchemaElement() .withAttributeName("Date") .withKeyType(KeyType.RANGE)); //Sort key// PrecipIndexGlobalSecondaryIndex precipIndex = new GlobalSecondaryIndex() .withIndexName("PrecipIndex") .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits((long) 10) .withWriteCapacityUnits((long) 1)) .withProjection(new Projection().withProjectionType(ProjectionType.ALL));ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();indexKeySchema.add(new KeySchemaElement() .withAttributeName("Date") .withKeyType(KeyType.HASH)); //Partition keyindexKeySchema.add(new KeySchemaElement() .withAttributeName("Precipitation") .withKeyType(KeyType.RANGE)); //Sort keyprecipIndex.setKeySchema(indexKeySchema);CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName("WeatherData") .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits((long) 5) .withWriteCapacityUnits((long) 1)) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(tableKeySchema) .withGlobalSecondaryIndexes(precipIndex);Table table = dynamoDB.createTable(createTableRequest);System.out.println(table.getDescription());
You must wait until DynamoDB creates the table and sets the table status toACTIVE
. After that, you can begin putting data items into the table.
To get information about global secondary indexes on a table, useDescribeTable
. For each index, you can access its name, key schema, and projected attributes.
The following are the steps to access global secondary index information a table.
Create an instance of theDynamoDB
class.
Create an instance of theTable
class to represent the index you want to work with.
Call thedescribe
method on theTable
object.
The following Java code example demonstrates the preceding steps.
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();DynamoDB dynamoDB = new DynamoDB(client);Table table = dynamoDB.getTable("WeatherData");TableDescription tableDesc = table.describe(); Iterator<GlobalSecondaryIndexDescription> gsiIter = tableDesc.getGlobalSecondaryIndexes().iterator();while (gsiIter.hasNext()){ GlobalSecondaryIndexDescription gsiDesc = gsiIter.next(); System.out.println("Info for index " + gsiDesc.getIndexName() + ":"); Iterator<KeySchemaElement> kseIter = gsiDesc.getKeySchema().iterator(); while (kseIter.hasNext()){ KeySchemaElement kse = kseIter.next(); System.out.printf("\t%s: %s\n", kse.getAttributeName(), kse.getKeyType()); } Projection projection = gsiDesc.getProjection(); System.out.println("\tThe projection type is: " + projection.getProjectionType()); if (projection.getProjectionType().toString().equals("INCLUDE")){ System.out.println("\t\tThe non-key projected attributes are: " + projection.getNonKeyAttributes()); }}
You can useQuery
on a global secondary index, in much the same way youQuery
a table. You need to specify the index name, the query criteria for the index partition key and sort key (if present), and the attributes that you want to return. In this example, the index isPrecipIndex
, which has a partition key ofDate
and a sort key ofPrecipitation
. The index query returns all of the weather data for a particular date, where the precipitation is greater than zero.
The following are the steps to query a global secondary index using the AWS SDK for Java Document API.
Create an instance of theDynamoDB
class.
Create an instance of theTable
class to represent the index you want to work with.
Create an instance of theIndex
class for the index you want to query.
Call thequery
method on theIndex
object.
The attribute nameDate
is a DynamoDB reserved word. Therefore, you must use an expression attribute name as a placeholder in theKeyConditionExpression
.
The following Java code example demonstrates the preceding steps.
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();DynamoDB dynamoDB = new DynamoDB(client);Table table = dynamoDB.getTable("WeatherData");Index index = table.getIndex("PrecipIndex");QuerySpec spec = new QuerySpec() .withKeyConditionExpression("#d = :v_date and Precipitation = :v_precip") .withNameMap(new NameMap() .with("#d", "Date")) .withValueMap(new ValueMap() .withString(":v_date","2013-08-10") .withNumber(":v_precip",0));ItemCollection<QueryOutcome> items = index.query(spec);Iterator<Item> iter = items.iterator(); while (iter.hasNext()){ System.out.println(iter.next().toJSONPretty());}