Movatterモバイル変換


[0]ホーム

URL:


Working with Global Secondary Indexes: Java - Amazon DynamoDB
DocumentationAmazon DynamoDBDeveloper Guide
Create a table with a Global Secondary IndexDescribe a table with a Global Secondary IndexQuery a Global Secondary Index

Working with Global Secondary Indexes: Java

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 a table with a Global Secondary Index

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.

  1. Create an instance of theDynamoDB class.

  2. 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.

  3. 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.

Describe a table with a Global Secondary Index

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.

The following Java code example demonstrates the preceding steps.

Example
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()); }}

Query a Global Secondary Index

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.

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.

Detecting and correcting index key violations in DynamoDB
Example: Global Secondary Indexes - Java document API

[8]
ページ先頭

©2009-2025 Movatter.jp