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

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

Introduction

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

Prototype


public java.util.List<KeySchemaElement> getKeySchema() 

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

/**
 * Create instance.//from   w ww  .  j a  va2  s. c  o m
 *
 * @param prefix the table prefix
 * @param tableNameSuffix the suffix for the table name
 * @param amazonDynamoDB dynamodb client
 * @param provisionedThroughputMap map of provisioned througput
 * @param objectMapper mapper to use for back/forth to json
 * @param clazz class reference of E
 * @param attributeDefinitions types of keys on base table and GSI
 * @param baseTableKeyNames names of base table keys
 * @param gsiList list of GSI definitions
 * @param versionString
 * @since #version#
 */
protected DynamoDbRepository(String prefix, String tableNameSuffix, AmazonDynamoDB amazonDynamoDB,
        Map<String, ProvisionedThroughput> provisionedThroughputMap, ObjectMapper objectMapper, Class<E> clazz,
        Map<String, ScalarAttributeType> attributeDefinitions, List<String> baseTableKeyNames,
        Map<String, GlobalSecondaryIndex> gsiList, String versionString) {
    Preconditions.checkNotNull(amazonDynamoDB);
    Preconditions.checkArgument(false == Strings.isNullOrEmpty(tableNameSuffix));
    Preconditions.checkNotNull(provisionedThroughputMap);
    Preconditions.checkNotNull(attributeDefinitions);
    this.dynamoDB = amazonDynamoDB;
    this.tableNameSuffix = tableNameSuffix;
    final String tableName = Strings.isNullOrEmpty(prefix) ? tableNameSuffix
            : String.format(Locale.ENGLISH, "%s_%s", prefix, tableNameSuffix);
    this.table = new Table(this.dynamoDB, tableName);
    this.ptMap = provisionedThroughputMap;
    this.gsis = gsiList != null ? new HashMap<>() : null;
    this.definitions = new HashMap<>(attributeDefinitions);
    this.lookupKeyConditions = new HashMap<>();
    this.objectMapper = objectMapper;
    this.clazz = clazz;
    this.gsiHashKeys = new HashMap<>();
    this.gsiRangeKeys = new HashMap<>();
    this.versionProperty = Strings.isNullOrEmpty(versionString) ? null : versionString;
    Optional.ofNullable(gsiList).orElse(new HashMap<>()).values().forEach(gsi -> {
        final String indexName = gsi.getIndexName();
        //make a copy
        final GlobalSecondaryIndex copy = new GlobalSecondaryIndex().withIndexName(gsi.getIndexName())
                .withKeySchema(gsi.getKeySchema()).withProjection(gsi.getProjection())
                .withProvisionedThroughput(ptMap.get(indexName));
        final String hk = copy.getKeySchema().get(0).getAttributeName();
        final String rk = copy.getKeySchema().size() == 2 ? copy.getKeySchema().get(1).getAttributeName()
                : null;
        this.gsis.put(indexName, copy);
        this.gsiHashKeys.put(indexName, hk);
        if (rk != null) {
            lookupKeyConditions.put(indexName,
                    String.format(Locale.ENGLISH, "%s = :%s and %s = :%s", hk, hk, rk, rk));
            this.gsiRangeKeys.put(indexName, rk);
        } else {
            lookupKeyConditions.put(indexName, String.format(Locale.ENGLISH, "%s = :%s", hk, hk));
        }
    });

    Preconditions.checkNotNull(baseTableKeyNames);
    Preconditions.checkArgument(false == baseTableKeyNames.isEmpty(), "need at least one key");
    Preconditions.checkArgument(baseTableKeyNames.size() <= 2,
            "cant have more than two keys (one partition and one sort key)");

    //add the attribute definitions for the base table key schema
    baseTableKeyNames.stream().forEach(name -> Preconditions.checkArgument(this.definitions.containsKey(name)));
    this.schemata = Lists.newArrayList(new KeySchemaElement(baseTableKeyNames.get(0), KeyType.HASH));
    if (baseTableKeyNames.size() == 2) {
        schemata.add(new KeySchemaElement(baseTableKeyNames.get(1), KeyType.RANGE));
    }
    hashKeyName = schemata.get(0).getAttributeName();
    rangeKeyName = schemata.size() == 2 ? schemata.get(1).getAttributeName() : null;
    conditionalCreateCondition = rangeKeyName != null
            ? String.format(Locale.ENGLISH, "attribute_not_exists(%s) and attribute_not_exists(%s)",
                    hashKeyName, rangeKeyName)
            : String.format(Locale.ENGLISH, "attribute_not_exists(%s)", hashKeyName);
    conditionalDeleteCondition = rangeKeyName != null
            ? String.format(Locale.ENGLISH, "attribute_exists(%s) and attribute_exists(%s)", hashKeyName,
                    rangeKeyName)
            : String.format(Locale.ENGLISH, "attribute_exists(%s)", hashKeyName);
}