Example usage for com.amazonaws.services.dynamodbv2.model AttributeDefinition AttributeDefinition

List of usage examples for com.amazonaws.services.dynamodbv2.model AttributeDefinition AttributeDefinition

Introduction

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

Prototype

public AttributeDefinition(String attributeName, ScalarAttributeType attributeType) 

Source Link

Document

Constructs a new AttributeDefinition object.

Usage

From source file:com.envirover.spl.stream.DynamoDBOutputStream.java

License:Open Source License

@Override
public void open() throws IOException {
    AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient();

    if (TableUtils.createTableIfNotExists(dynamoDBClient,
            new CreateTableRequest().withTableName(tableName)
                    .withKeySchema(new KeySchemaElement(ATTR_DEVICE_ID, KeyType.HASH),
                            new KeySchemaElement(ATTR_TIME, KeyType.RANGE))
                    .withAttributeDefinitions(new AttributeDefinition(ATTR_DEVICE_ID, ScalarAttributeType.S),
                            new AttributeDefinition(ATTR_TIME, ScalarAttributeType.N))
                    .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(READ_CAPACITY)
                            .withWriteCapacityUnits(WRITE_CAPACITY)))) {

        try {//from   w  w  w . ja va 2 s.  c o  m
            TableUtils.waitUntilActive(dynamoDBClient, tableName);
        } catch (TableNeverTransitionedToStateException e) {
            throw new IOException(e);
        } catch (InterruptedException e) {
            throw new IOException(e);
        }

        logger.info(MessageFormat.format("DynamoDB table ''{0}'' created.", tableName));
    }

    dynamoDB = new DynamoDB(dynamoDBClient);
}

From source file:com.erudika.para.persistence.AWSDynamoUtils.java

License:Apache License

/**
 * Creates a table in AWS DynamoDB.//from   w  w w .  jav  a  2 s.c  om
 * @param appid name of the {@link com.erudika.para.core.App}
 * @param readCapacity read capacity
 * @param writeCapacity write capacity
 * @return true if created
 */
public static boolean createTable(String appid, long readCapacity, long writeCapacity) {
    if (StringUtils.isBlank(appid)) {
        return false;
    } else if (StringUtils.containsWhitespace(appid)) {
        logger.warn("DynamoDB table name contains whitespace. The name '{}' is invalid.", appid);
        return false;
    } else if (existsTable(appid)) {
        logger.warn("DynamoDB table '{}' already exists.", appid);
        return false;
    }
    try {
        getClient().createTable(new CreateTableRequest().withTableName(getTableNameForAppid(appid))
                .withKeySchema(new KeySchemaElement(Config._KEY, KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition(Config._KEY, ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput(readCapacity, writeCapacity)));
    } catch (Exception e) {
        logger.error(null, e);
        return false;
    }
    return true;
}

From source file:com.erudika.para.persistence.AWSDynamoUtils.java

License:Apache License

/**
 * Creates a table in AWS DynamoDB which will be shared between apps.
 * @param readCapacity read capacity//from w  w w.j a v  a2 s. c  o m
 * @param writeCapacity write capacity
 * @return true if created
 */
public static boolean createSharedTable(long readCapacity, long writeCapacity) {
    if (StringUtils.isBlank(SHARED_TABLE) || StringUtils.containsWhitespace(SHARED_TABLE)
            || existsTable(SHARED_TABLE)) {
        return false;
    }
    try {
        GlobalSecondaryIndex secIndex = new GlobalSecondaryIndex().withIndexName(getSharedIndexName())
                .withProvisionedThroughput(
                        new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L))
                .withProjection(new Projection().withProjectionType(ProjectionType.ALL)).withKeySchema(
                        new KeySchemaElement().withAttributeName(Config._APPID).withKeyType(KeyType.HASH),
                        new KeySchemaElement().withAttributeName(Config._TIMESTAMP).withKeyType(KeyType.RANGE));

        getClient().createTable(new CreateTableRequest().withTableName(getTableNameForAppid(SHARED_TABLE))
                .withKeySchema(new KeySchemaElement(Config._KEY, KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition(Config._KEY, ScalarAttributeType.S),
                        new AttributeDefinition(Config._APPID, ScalarAttributeType.S),
                        new AttributeDefinition(Config._TIMESTAMP, ScalarAttributeType.S))
                .withGlobalSecondaryIndexes(secIndex)
                .withProvisionedThroughput(new ProvisionedThroughput(readCapacity, writeCapacity)));
    } catch (Exception e) {
        logger.error(null, e);
        return false;
    }
    return true;
}

From source file:com.euclidespaim.dynamodb.CreateTableFunction.java

License:Open Source License

public static void main(String[] args) throws Exception {

    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(
            new AwsClientBuilder.EndpointConfiguration("https://dynamodb.us-east-1.amazonaws.com", "us-east-1"))
            .build();/*from   w w  w .  j a va 2  s  .  com*/

    DynamoDB dynamoDB = new DynamoDB(client);

    String tableName = "Movies";

    try {
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName, Arrays.asList(new KeySchemaElement("year", KeyType.HASH), // Partition
                // key
                new KeySchemaElement("title", KeyType.RANGE)), // Sort key
                Arrays.asList(new AttributeDefinition("year", ScalarAttributeType.N),
                        new AttributeDefinition("title", ScalarAttributeType.S)),
                new ProvisionedThroughput(10L, 10L));
        table.waitForActive();
        System.out.println("Success.  Table status: " + table.getDescription().getTableStatus());

    } catch (Exception e) {
        System.err.println("Unable to create table: ");
        System.err.println(e.getMessage());
    }

}

From source file:com.exorath.service.lastserver.dynamodb.DynamoDBService.java

License:Apache License

public DynamoDBService(DynamoDBProvider provider) {
    try {//  w ww  .j av a2s. c  om
        table = provider.getDB()
                .createTable(new CreateTableRequest().withTableName(TABLE_NAME)
                        .withKeySchema(new KeySchemaElement(PRIM_KEY, KeyType.HASH))
                        .withAttributeDefinitions(new AttributeDefinition(PRIM_KEY, ScalarAttributeType.S))
                        .withProvisionedThroughput(new ProvisionedThroughput(1L, 1L)));
        logger.info("Created DynamoDB table " + TABLE_NAME
                + " with 1r/1w provisioning. Waiting for it to activate.");
    } catch (ResourceInUseException ex) {
        table = provider.getDB().getTable(TABLE_NAME);
        logger.info("DynamoDB table " + TABLE_NAME + " already existed. Waiting for it to activate.");
    }

    try {
        table.waitForActive();
    } catch (InterruptedException ex) {
        logger.error("DynamoDB table " + TABLE_NAME + " could not activate!\n" + ex.getMessage());
        System.exit(1);
    }
    logger.info("DynamoDB table " + TABLE_NAME + " active.");
}

From source file:com.exorath.service.lobbymsg.impl.DynamoDBService.java

License:Apache License

private void setupTable() throws InterruptedException {
    try {//w  w  w.  j a  va 2  s . com
        Table table = db.createTable(new CreateTableRequest().withTableName(tableName)
                .withKeySchema(new KeySchemaElement(ID_FIELD, KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition(ID_FIELD, ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput(1l, 1l)));
        LOG.info(
                "Created dynamodb table " + tableName + " with 1r/1w provisioning. Waiting for it to activate");
        waitForTableToActivate();
    } catch (ResourceInUseException e) {//table exists, let's make sure it's active
        waitForTableToActivate();
    }
}

From source file:com.exorath.service.party.service.DynamoDatabaseProvider.java

License:Apache License

/**
 * @param primKey Primary partition key for the table
 * @param gsi     String array for global secondary index's, allow for searching on more than primary key
 * @return Table containing party information
 *///w w  w. ja v  a2s  . c om
private Table setupTable(String primKey, String... gsi) {
    Table table;
    try {
        ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(1L, 1L);
        ArrayList<GlobalSecondaryIndex> gsiArr = new ArrayList<>();
        ArrayList<AttributeDefinition> attDefs = new ArrayList<>();
        for (String g : gsi) {
            GlobalSecondaryIndex gsiIndex = new GlobalSecondaryIndex().withIndexName(g)
                    .withProvisionedThroughput(provisionedThroughput)
                    .withKeySchema(new KeySchemaElement().withAttributeName(g).withKeyType(KeyType.HASH))
                    .withProjection(new Projection().withProjectionType("ALL"));
            gsiArr.add(gsiIndex);
            attDefs.add(new AttributeDefinition(g, ScalarAttributeType.S));
        }
        attDefs.add(new AttributeDefinition(primKey, ScalarAttributeType.S));
        table = database.createTable(new CreateTableRequest().withTableName(tableName)
                .withKeySchema(new KeySchemaElement(primKey, KeyType.HASH)).withGlobalSecondaryIndexes(gsiArr)
                .withAttributeDefinitions(attDefs).withProvisionedThroughput(provisionedThroughput));
        logger.info("Created DynamoDB table " + tableName
                + " with 1r/1w provisioning. Waiting for it to activate.");
    } catch (ResourceInUseException ex) {
        table = database.getTable(tableName);
        logger.info("DynamoDB table " + tableName + " already existed. Waiting for it to activate.");
    }

    try {
        table.waitForActive();
    } catch (InterruptedException ex) {
        logger.error("DynamoDB table " + tableName + " could not activate!\n" + ex.getMessage());
        System.exit(1);
    }
    logger.info("DynamoDB table " + tableName + " active.");
    return table;
}

From source file:com.rss.common.AWSDetails.java

License:Apache License

public static void main(String[] args) {
    String queueUrl = SQS.createQueue(new CreateQueueRequest(SQS_QUEUE_NAME)).getQueueUrl();
    System.out.println("Using Amazon SQS Queue: " + queueUrl);

    String publisherQueueUrl = SQS.createQueue(new CreateQueueRequest(SQS_PUBLISHER_QUEUE)).getQueueUrl();
    System.out.println("Using Amazon SQS Queue: " + publisherQueueUrl);

    String webserviceRequestQueueUrl = SQS.createQueue(new CreateQueueRequest(SQS_WEBSERVICE_REQUEST_QUEUE))
            .getQueueUrl();//from   w  w  w  . j  a va 2 s  . c om
    System.out.println("Using Amazon SQS Queue: " + webserviceRequestQueueUrl);

    if (!Tables.doesTableExist(DYNAMODB, JOBREQUEST_TABLE_NAME)) {
        System.out.println("Creating job request table...");
        DYNAMODB.createTable(new CreateTableRequest().withTableName(JOBREQUEST_TABLE_NAME)
                .withKeySchema(new KeySchemaElement("id", KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition("id", ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput(50l, 50l)));
    }
    if (!Tables.doesTableExist(DYNAMODB, FEEDURL_REQUEST_TABLE_NAME)) {
        System.out.println("Creating feed url request table...");
        DYNAMODB.createTable(new CreateTableRequest().withTableName(FEEDURL_REQUEST_TABLE_NAME)
                .withKeySchema(new KeySchemaElement("id", KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition("id", ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput(50l, 50l)));
    }
    if (!Tables.doesTableExist(DYNAMODB, SUBSCRIBER_TABLE_NAME)) {
        System.out.println("Creating subscriber table...");
        DYNAMODB.createTable(new CreateTableRequest().withTableName(SUBSCRIBER_TABLE_NAME)
                .withKeySchema(new KeySchemaElement("id", KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition("id", ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput(50l, 50l)));
    }
    if (!Tables.doesTableExist(DYNAMODB, CHANNEL_TABLE_NAME)) {
        System.out.println("Creating channel table....");
        DYNAMODB.createTable(new CreateTableRequest().withTableName(CHANNEL_TABLE_NAME)
                .withKeySchema(new KeySchemaElement("id", KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition("id", ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput(50l, 50l)));
    }
    if (!Tables.doesTableExist(DYNAMODB, FEED_URL_TABLE_NAME)) {
        System.out.println("Creating feedurl table....");
        DYNAMODB.createTable(new CreateTableRequest().withTableName(FEED_URL_TABLE_NAME)
                .withKeySchema(new KeySchemaElement("id", KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition("id", ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput(50l, 50l)));
    }
    if (!Tables.doesTableExist(DYNAMODB, ARTICLE_TABLE_NAME)) {
        System.out.println("Creating article table....");
        DYNAMODB.createTable(new CreateTableRequest().withTableName(ARTICLE_TABLE_NAME)
                .withKeySchema(new KeySchemaElement("id", KeyType.HASH))
                .withAttributeDefinitions(new AttributeDefinition("id", ScalarAttributeType.S))
                .withProvisionedThroughput(new ProvisionedThroughput(50l, 50l)));
    }
    Tables.waitForTableToBecomeActive(DYNAMODB, JOBREQUEST_TABLE_NAME);
    Tables.waitForTableToBecomeActive(DYNAMODB, FEEDURL_REQUEST_TABLE_NAME);
    Tables.waitForTableToBecomeActive(DYNAMODB, SUBSCRIBER_TABLE_NAME);
    Tables.waitForTableToBecomeActive(DYNAMODB, CHANNEL_TABLE_NAME);
    Tables.waitForTableToBecomeActive(DYNAMODB, FEED_URL_TABLE_NAME);
    Tables.waitForTableToBecomeActive(DYNAMODB, ARTICLE_TABLE_NAME);
    System.out.println("Using AWS DynamoDB Table: " + JOBREQUEST_TABLE_NAME + "," + SUBSCRIBER_TABLE_NAME + ","
            + CHANNEL_TABLE_NAME);
}

From source file:com.vivastream.security.oauth2.common.util.DynamoDBInitializationHelper.java

License:Apache License

public static void createTokenTables(AmazonDynamoDBClient client, DynamoDBTokenSchema schema) {
    GlobalSecondaryIndex gsiAuthenticationIdToken = new GlobalSecondaryIndex() //
            .withIndexName(schema.getAccessIndexAuthenticationId()) //
            .withKeySchema(new KeySchemaElement(schema.getAccessColumnAuthenticationId(), KeyType.HASH)) //
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
            .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY));

    GlobalSecondaryIndex gsiRefreshToken = new GlobalSecondaryIndex() //
            .withIndexName(schema.getAccessIndexRefreshToken()) //
            .withKeySchema(new KeySchemaElement(schema.getAccessColumnRefreshToken(), KeyType.HASH)) //
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
            .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY));

    GlobalSecondaryIndex gsiClientIdAndUserName = new GlobalSecondaryIndex() //
            .withIndexName(schema.getAccessIndexClientIdAndUserName()) //
            .withKeySchema( //
                    new KeySchemaElement(schema.getAccessColumnClientId(), KeyType.HASH), //
                    new KeySchemaElement(schema.getAccessColumnUserName(), KeyType.RANGE) //
            ) ////from   www .jav a  2  s .  c  o m
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
            .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY));

    CreateTableRequest accessTableRequest = new CreateTableRequest() //
            .withTableName(schema.getAccessTableName()) //
            .withKeySchema(new KeySchemaElement(schema.getAccessColumnTokenId(), KeyType.HASH)) //
            .withGlobalSecondaryIndexes(gsiAuthenticationIdToken, gsiRefreshToken, gsiClientIdAndUserName) //
            .withAttributeDefinitions(
                    new AttributeDefinition(schema.getAccessColumnTokenId(), ScalarAttributeType.S), //
                    new AttributeDefinition(schema.getAccessColumnAuthenticationId(), ScalarAttributeType.S), //
                    new AttributeDefinition(schema.getAccessColumnRefreshToken(), ScalarAttributeType.S), //
                    new AttributeDefinition(schema.getAccessColumnClientId(), ScalarAttributeType.S), //
                    new AttributeDefinition(schema.getAccessColumnUserName(), ScalarAttributeType.S) //
            ) //
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
    ;

    CreateTableResult accessTableresponse = client.createTable(accessTableRequest);

    CreateTableRequest refreshTableRequest = new CreateTableRequest() //
            .withTableName(schema.getRefreshTableName()) //
            .withKeySchema(new KeySchemaElement(schema.getRefreshColumnTokenId(), KeyType.HASH)) //
            .withAttributeDefinitions(
                    new AttributeDefinition(schema.getRefreshColumnTokenId(), ScalarAttributeType.S) //
            ) //
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
    ;

    CreateTableResult refreshTableresponse = client.createTable(refreshTableRequest);
}

From source file:com.vivastream.security.oauth2.common.util.DynamoDBInitializationHelper.java

License:Apache License

public static void createHashTable(AmazonDynamoDBClient client, String tableName, String hashColumnName) {

    CreateTableRequest accessTableRequest = new CreateTableRequest() //
            .withTableName(tableName) //
            .withKeySchema(new KeySchemaElement(hashColumnName, KeyType.HASH)) //
            .withAttributeDefinitions(new AttributeDefinition(hashColumnName, ScalarAttributeType.S) //
            ) ////from  www.  j  av  a 2s  .co  m
            .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) //
    ;

    CreateTableResult accessTableresponse = client.createTable(accessTableRequest);
}