Example usage for com.amazonaws.services.dynamodbv2.model GlobalSecondaryIndex withKeySchema

List of usage examples for com.amazonaws.services.dynamodbv2.model GlobalSecondaryIndex withKeySchema

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model GlobalSecondaryIndex withKeySchema.

Prototype


public GlobalSecondaryIndex withKeySchema(java.util.Collection<KeySchemaElement> keySchema) 

Source Link

Document

The complete key schema for a global secondary index, which consists of one or more pairs of attribute names and key types:

  • HASH - partition key

  • RANGE - sort key

The partition key of an item is also known as its hash attribute.

Usage

From source file:jp.classmethod.aws.dynamodb.DynamoDbRepository.java

License:Open Source License

/**
 * Creates a GSI configuration object/*  w w  w . ja  v  a 2s  .c o  m*/
 *
 * @param name             name of the index object to create
 * @param hashKey          hash key of the index
 * @param rangeKey         range key of the index
 * @param nonKeyAttributes determines the projection type and top level projected attributes.
 *                         if null, ALL attributes are projected. if an empty list, KEYS_ONLY are projected.
 *                         if the list has elements, only the elements INCLUDED in the list are projected
 * @return a description of a global secondary index that can be used to create a table.
 */
protected static GlobalSecondaryIndex createGlobalSecondaryIndex(String name, String hashKey, String rangeKey,
        List<String> nonKeyAttributes) {
    Preconditions.checkArgument(false == Strings.isNullOrEmpty(hashKey));
    final KeySchemaElement hks = new KeySchemaElement(hashKey, KeyType.HASH);

    final Projection projection;
    if (nonKeyAttributes == null) {
        projection = new Projection().withProjectionType(ProjectionType.ALL);
    } else if (nonKeyAttributes.isEmpty()) {
        projection = new Projection().withProjectionType(ProjectionType.KEYS_ONLY);
    } else {
        projection = new Projection().withProjectionType(ProjectionType.INCLUDE)
                .withNonKeyAttributes(nonKeyAttributes);
    }

    final GlobalSecondaryIndex result = new GlobalSecondaryIndex().withIndexName(name)
            .withProjection(projection);
    if (Strings.isNullOrEmpty(rangeKey)) {
        result.withKeySchema(hks);
    } else {
        result.withKeySchema(hks, new KeySchemaElement(rangeKey, KeyType.RANGE));
    }
    return result;
}