Example usage for com.amazonaws.services.dynamodbv2.model KeyType HASH

List of usage examples for com.amazonaws.services.dynamodbv2.model KeyType HASH

Introduction

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

Prototype

KeyType HASH

To view the source code for com.amazonaws.services.dynamodbv2.model KeyType HASH.

Click Source Link

Usage

From source file:com.innoq.hagmans.bachelor.DynamoDBUtils.java

License:Open Source License

/**
 * Creates the table to store our temperatures in with a hash key of
 * "sensor" and a range key of "timestamp" so we can query counts for a
 * given sensor by time. This uses an initial provisioned throughput of 10
 * read capacity units and 5 write capacity units
 * //w  w  w.  j  a  v  a2  s  . c  o m
 * @param tableName
 *            The name of the table to create.
 */
public void createTemperatureTableIfNotExists(String tableName) {
    List<KeySchemaElement> ks = new ArrayList<>();
    ks.add(new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(ATTRIBUTE_NAME_HASH_KEY));
    ks.add(new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(ATTRIBUTE_NAME_RANGE_KEY));

    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName(ATTRIBUTE_NAME_HASH_KEY)
            .withAttributeType(ScalarAttributeType.S));
    // Range key must be a String. DynamoDBMapper translates Dates to
    // ISO8601 strings.
    attributeDefinitions.add(new AttributeDefinition().withAttributeName(ATTRIBUTE_NAME_RANGE_KEY)
            .withAttributeType(ScalarAttributeType.S));

    // Create the table with enough write IOPS to handle 5 distinct
    // resources updated every 1 second:
    // 1 update/second * 5 resources = 5 write IOPS.
    // The provisioned throughput will need to be adjusted if the cadinality
    // of the input data or the interval for
    // updates changes.
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
            .withProvisionedThroughput(new ProvisionedThroughput(10L, 5L)).withKeySchema(ks)
            .withAttributeDefinitions(attributeDefinitions);

    try {
        amazonDynamoDB.createTable(createTableRequest);

        LOG.info(String.format("Created DynamoDB table: %s. Waiting up to 5 minutes for it to become ACTIVE...",
                tableName));
        // Wait 5 minutes for the table to become ACTIVE
        if (!waitUntilTableIsActive(tableName, 10, TimeUnit.MINUTES.toSeconds(5))) {
            throw new IllegalStateException(
                    String.format("Timed out while waiting for DynamoDB table %s to become ready", tableName));
        }
    } catch (ResourceInUseException ex) {
        // Assume table exists and is ready to use
    }
}

From source file:com.intuit.tank.persistence.databases.AmazonDynamoDatabaseDocApi.java

License:Open Source License

/**
 * /* ww w. j  ava  2  s  . c om*/
 * @{inheritDoc
 */
@Override
public void createTable(String tableName) {
    try {
        if (!hasTable(tableName)) {
            logger.info("Creating table: " + tableName);
            HierarchicalConfiguration resultsProviderConfig = config.getVmManagerConfig()
                    .getResultsProviderConfig();
            long readCapacity = getCapacity(resultsProviderConfig, "read-capacity", 10L);
            long writeCapacity = getCapacity(resultsProviderConfig, "write-capacity", 50L);
            ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
            attributeDefinitions
                    .add(new AttributeDefinition().withAttributeName(DatabaseKeys.JOB_ID_KEY.getShortKey())
                            .withAttributeType(ScalarAttributeType.S));
            attributeDefinitions.add(
                    new AttributeDefinition().withAttributeName(DatabaseKeys.REQUEST_NAME_KEY.getShortKey())
                            .withAttributeType(ScalarAttributeType.S));
            ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput()
                    .withReadCapacityUnits(readCapacity).withWriteCapacityUnits(writeCapacity);
            KeySchemaElement hashKeyElement = new KeySchemaElement()
                    .withAttributeName(DatabaseKeys.JOB_ID_KEY.getShortKey()).withKeyType(KeyType.HASH);
            KeySchemaElement rangeKeyElement = new KeySchemaElement()
                    .withAttributeName(DatabaseKeys.REQUEST_NAME_KEY.getShortKey()).withKeyType(KeyType.RANGE);
            CreateTableRequest request = new CreateTableRequest().withTableName(tableName)
                    .withKeySchema(hashKeyElement, rangeKeyElement)
                    .withAttributeDefinitions(attributeDefinitions)
                    .withProvisionedThroughput(provisionedThroughput);

            CreateTableResult result = dynamoDb.createTable(request);
            waitForStatus(tableName, TableStatus.ACTIVE);
            logger.info("Created table: " + result.getTableDescription().getTableName());
        }
    } catch (Exception t) {
        logger.error(t, t);
        throw new RuntimeException(t);
    }
}

From source file:com.kinesis.datavis.utils.DynamoDBUtils.java

License:Open Source License

public void createCountTableIfNotExists(String tableName, String attrHash, String attrRange) {
    List<KeySchemaElement> ks = new ArrayList<>();
    ks.add(new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(attrHash));
    ks.add(new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(attrRange));

    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>();
    attributeDefinitions.add(/*w  w w  . j  av  a 2 s.  c o  m*/
            new AttributeDefinition().withAttributeName(attrHash).withAttributeType(ScalarAttributeType.S));
    // Range key must be a String. DynamoDBMapper translates Dates to ISO8601 strings.
    attributeDefinitions.add(
            new AttributeDefinition().withAttributeName(attrRange).withAttributeType(ScalarAttributeType.S));
    // Create the table with enough write IOPS to handle 5 distinct resources updated every 1 second:
    // 1 update/second * 5 resources = 5 write IOPS.
    // The provisioned throughput will need to be adjusted if the cadinality of the input data or the interval for
    // updates changes.
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
            .withProvisionedThroughput(new ProvisionedThroughput(10L, 5L)).withKeySchema(ks)
            .withAttributeDefinitions(attributeDefinitions);

    try {
        dynamoDB.createTable(createTableRequest);

        LOG.info(String.format("Created DynamoDB table: %s. Waiting up to 5 minutes for it to become ACTIVE...",
                tableName));
        // Wait 5 minutes for the table to become ACTIVE
        if (!waitUntilTableIsActive(tableName, 10, TimeUnit.MINUTES.toSeconds(5))) {
            throw new IllegalStateException(
                    String.format("Timed out while waiting for DynamoDB table %s to become ready", tableName));
        }
    } catch (ResourceInUseException ex) {
        // Assume table exists and is ready to use
    }
}

From source file:com.lvl6.mobsters.dynamo.setup.TransactionExamples.java

License:Open Source License

public void setup() throws Exception {
    print("\n*** setup() ***\n");
    TableHelper tableHelper = new TableHelper(dynamodb);

    // 1. Verify that the transaction table exists, or create it if it doesn't exist
    print("Verifying or creating table " + TX_TABLE_NAME);
    TransactionManager.verifyOrCreateTransactionTable(dynamodb, TX_TABLE_NAME, 1, 1, null);

    // 2. Verify that the transaction item images table exists, or create it otherwise
    print("Verifying or creating table " + TX_IMAGES_TABLE_NAME);
    TransactionManager.verifyOrCreateTransactionImagesTable(dynamodb, TX_IMAGES_TABLE_NAME, 1, 1, null);

    // 3. Create a table to do transactions on
    print("Verifying or creating table " + EXAMPLE_TABLE_NAME);
    List<AttributeDefinition> attributeDefinitions = Arrays.asList(new AttributeDefinition()
            .withAttributeName(EXAMPLE_TABLE_HASH_KEY).withAttributeType(ScalarAttributeType.S));
    List<KeySchemaElement> keySchema = Arrays
            .asList(new KeySchemaElement().withAttributeName(EXAMPLE_TABLE_HASH_KEY).withKeyType(KeyType.HASH));
    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(1L)
            .withWriteCapacityUnits(1L);

    tableHelper.verifyOrCreateTable(EXAMPLE_TABLE_NAME, attributeDefinitions, keySchema, null,
            provisionedThroughput, null);

    // 4. Wait for tables to be created
    print("Waiting for table to become ACTIVE: " + EXAMPLE_TABLE_NAME);
    tableHelper.waitForTableActive(EXAMPLE_TABLE_NAME, 5 * 60L);
    print("Waiting for table to become ACTIVE: " + TX_TABLE_NAME);
    tableHelper.waitForTableActive(TX_TABLE_NAME, 5 * 60L);
    print("Waiting for table to become ACTIVE: " + TX_IMAGES_TABLE_NAME);
    tableHelper.waitForTableActive(TX_IMAGES_TABLE_NAME, 5 * 60L);
}

From source file:com.makariev.dynamodb.data.tables.creators.ReplyCreator.java

License:Apache License

@Override
public CreateTableRequest getCreateTableRequest() {
    final String tableName = Reply.TABLE_NAME;
    final String hashKeyName = "Id";
    final ScalarAttributeType hashKeyType = ScalarAttributeType.S;
    final Long readCapacityUnits = 10L;
    final Long writeCapacityUnits = 10L;

    final String rangeKeyName = "ReplyDateTime";
    final ScalarAttributeType rangeKeyType = ScalarAttributeType.S;

    ////////////////
    final CreateTableRequest request = basicTable(tableName, readCapacityUnits, writeCapacityUnits, hashKeyName,
            hashKeyType);/*from w  ww  . j a  va 2s.  c om*/
    ///////////////////////
    //defining range key 
    request.getKeySchema()
            .add(new KeySchemaElement().withAttributeName(rangeKeyName).withKeyType(KeyType.RANGE));
    //defining range key 
    request.getAttributeDefinitions()
            .add(new AttributeDefinition().withAttributeName(rangeKeyName).withAttributeType(rangeKeyType));
    //////////////////////

    //follows defininig of a local secondary index
    request.getAttributeDefinitions().add(
            new AttributeDefinition().withAttributeName("PostedBy").withAttributeType(ScalarAttributeType.S));

    final List<KeySchemaElement> iks = new ArrayList<>();
    iks.add(new KeySchemaElement().withAttributeName(hashKeyName).withKeyType(KeyType.HASH));
    iks.add(new KeySchemaElement().withAttributeName("PostedBy").withKeyType(KeyType.RANGE));

    final LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex().withIndexName("PostedByIndex")
            .withKeySchema(iks).withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY));

    final List<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<>();
    localSecondaryIndexes.add(localSecondaryIndex);

    request.setLocalSecondaryIndexes(localSecondaryIndexes);

    return request;
}

From source file:com.makariev.dynamodb.data.tables.creators.UserPreferenceCreator.java

License:Apache License

@Override
public CreateTableRequest getCreateTableRequest() {
    final String tableName = UserPreference.TABLE_NAME;
    final String hashKeyName = "userNo";
    final ScalarAttributeType hashKeyType = ScalarAttributeType.N;
    final Long readCapacityUnits = 10L;
    final Long writeCapacityUnits = 10L;

    ////////////////
    final CreateTableRequest request = basicTable(tableName, readCapacityUnits, writeCapacityUnits, hashKeyName,
            hashKeyType);//from   w w  w .  j  a  v a  2  s .  c  o m

    //define GlobalSecondaryIndex
    request.getAttributeDefinitions().add(
            new AttributeDefinition().withAttributeName("firstName").withAttributeType(ScalarAttributeType.S));

    request.getAttributeDefinitions().add(
            new AttributeDefinition().withAttributeName("lastName").withAttributeType(ScalarAttributeType.S));

    // NameIndex
    final GlobalSecondaryIndex nameIndex = new GlobalSecondaryIndex().withIndexName(UserPreference.NAME_INDEX)
            .withProvisionedThroughput(
                    new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(10L))
            .withProjection(new Projection().withProjectionType("ALL"));

    final List<KeySchemaElement> indexKeySchema = new ArrayList<>();

    indexKeySchema.add(new KeySchemaElement().withAttributeName("firstName").withKeyType(KeyType.HASH));
    indexKeySchema.add(new KeySchemaElement().withAttributeName("lastName").withKeyType(KeyType.RANGE));

    nameIndex.setKeySchema(indexKeySchema);

    request.withGlobalSecondaryIndexes(nameIndex);
    return request;
}

From source file:com.makariev.dynamodb.data.tables.TableRequestCreatorHelper.java

License:Apache License

public static CreateTableRequest basicTable(String tableName, Long readCapacityUnits, Long writeCapacityUnits,
        String hashKeyName, ScalarAttributeType hashKeyType) {

    final List<KeySchemaElement> ks = new ArrayList<>();
    ks.add(new KeySchemaElement().withAttributeName(hashKeyName).withKeyType(KeyType.HASH));

    final List<AttributeDefinition> attributeDefinitions = new ArrayList<>();
    attributeDefinitions//w w w .j  a  v  a 2  s .c o m
            .add(new AttributeDefinition().withAttributeName(hashKeyName).withAttributeType(hashKeyType));

    // Provide initial provisioned throughput values as Java long data types
    final ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput()
            .withReadCapacityUnits(readCapacityUnits).withWriteCapacityUnits(writeCapacityUnits);

    final CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withKeySchema(ks)
            .withProvisionedThroughput(provisionedthroughput).withAttributeDefinitions(attributeDefinitions);

    return request;
}

From source file:com.mortardata.pig.storage.DynamoDBStorage.java

License:Apache License

/**
 * HELPERS//from   w w  w  .j av a2 s.co m
 *
 * @throws IOException
 **/

private void checkPigSchemaForDynamo(ResourceSchema schema) throws IOException {
    // extract field names
    Set<String> fieldNames = Sets.newHashSetWithExpectedSize(schema.getFields().length);
    for (ResourceFieldSchema field : schema.getFields()) {
        String fieldName = field.getName();
        if (fieldNames.contains(fieldName)) {
            throw new IOException(
                    "Schema cannot contain duplicated field name. Found duplicated: " + fieldName);
        }
        if (field.getType() == DataType.MAP || field.getType() == DataType.TUPLE
                || field.getType() == DataType.BAG) {
            throw new IOException(
                    "DynamoDBStorage can not store map, tuple, or bag types.  Found one in field name: "
                            + fieldName);
        }
        fieldNames.add(fieldName);
    }

    // ensure that Dynamo table primary keys are found in field names
    DescribeTableResult describe = describeDynamoTable();
    List<KeySchemaElement> keySchemaElements = describe.getTable().getKeySchema();

    for (KeySchemaElement keySchemaElement : keySchemaElements) {
        String expectedFieldName = keySchemaElement.getAttributeName();

        if (KeyType.valueOf(keySchemaElement.getKeyType()) == KeyType.HASH) {
            if (!fieldNames.contains(expectedFieldName)) {
                throw new IOException("Dynamo table " + this.tableName + " hash primary key ["
                        + expectedFieldName + "] not found in " + " pig schema fields: " + fieldNames);
            }
        } else if (KeyType.valueOf(keySchemaElement.getKeyType()) == KeyType.RANGE) {
            if (!fieldNames.contains(expectedFieldName)) {
                throw new IOException("Dynamo table " + this.tableName + " range secondary key ["
                        + expectedFieldName + "] not found in " + " pig schema fields: " + fieldNames);
            }
        }
    }

}

From source file:com.mycompany.rproject.runnableClass.java

private static void createTable() {
    List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("ID").withAttributeType("S"));

    List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
    ks.add(new KeySchemaElement().withAttributeName("ID").withKeyType(KeyType.HASH));

    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(10L)
            .withWriteCapacityUnits(10L);

    CreateTableRequest request = new CreateTableRequest().withTableName(TABLENAME)
            .withAttributeDefinitions(attributeDefinitions).withKeySchema(ks)
            .withProvisionedThroughput(provisionedThroughput);

    try {/*from   w  w  w  . j av  a2  s . c  o  m*/

        CreateTableResult createdTableDescription = dynamoDBClient.createTable(request);

        System.out.println("Created table:" + createdTableDescription);
        // Wait for it to become active
        waitForTableToBecomeAvailable(TABLENAME);

    } catch (ResourceInUseException e) {

        //logger.warn(e);
    }
}

From source file:com.netflix.config.sources.DynamoDbIntegrationTestHelper.java

License:Apache License

static void createTable(AmazonDynamoDB dbClient, String tableName) throws InterruptedException {
    //TODO check to make sure the table isn't being created or deleted.
    KeySchemaElement hashKey = new KeySchemaElement()
            .withAttributeName(DynamoDbConfigurationSource.defaultKeyAttribute).withKeyType(KeyType.HASH);

    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(1L)
            .withWriteCapacityUnits(1L);

    dbClient.createTable(new CreateTableRequest().withTableName(tableName).withKeySchema(hashKey)
            .withProvisionedThroughput(provisionedThroughput));

    while (!dbClient.describeTable(new DescribeTableRequest().withTableName(tableName)).getTable()
            .getTableStatus().equalsIgnoreCase("active")) {
        Thread.sleep(10000);/*w  w w .j a  va  2s .co m*/
    }
}