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

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

Introduction

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

Prototype

public ProvisionedThroughput(Long readCapacityUnits, Long writeCapacityUnits) 

Source Link

Document

Constructs a new ProvisionedThroughput object.

Usage

From source file:DynamoDBUtils.java

License:Open Source License

/**
 * Creates the Amazon DynamoDB table if it does not already exist and have the correct schema. Then it
 * waits for the table to become active.
 *
 * @param client/* w  ww  .  j a  v  a 2 s . co  m*/
 *        The {@link AmazonDynamoDBClient} with Amazon DynamoDB read and write privileges
 * @param tableName
 *        The Amazon DynamoDB table to create
 * @param key
 *        The Amazon DynamoDB table hashkey
 * @param readCapacityUnits
 *        Number of read capacity units for the Amazon DynamoDB table
 * @param writeCapacityUnits
 *        Number of write capacity units for the Amazon DynamoDB table
 * @throws IllegalStateException
 *         Table already exists and schema does not match
 * @throws IllegalStateException
 *         Table is already getting created
 */
public static void createTable(AmazonDynamoDBClient client, String tableName, String key,
        long readCapacityUnits, long writeCapacityUnits) {
    if (tableExists(client, tableName)) {
        if (tableHasCorrectSchema(client, tableName, key)) {
            waitForActive(client, tableName);
            return;
        } else {
            throw new IllegalStateException("Table already exists and schema does not match");
        }

    }
    CreateTableRequest createTableRequest = new CreateTableRequest();
    createTableRequest.setTableName(tableName);
    createTableRequest.setKeySchema(Arrays.asList(new KeySchemaElement(key, KeyType.HASH)));
    createTableRequest
            .setProvisionedThroughput(new ProvisionedThroughput(readCapacityUnits, writeCapacityUnits));
    createTableRequest
            .setAttributeDefinitions(Arrays.asList(new AttributeDefinition(key, ScalarAttributeType.S)));
    try {
        client.createTable(createTableRequest);
    } catch (ResourceInUseException e) {
        throw new IllegalStateException("The table may already be getting created.", e);
    }
    LOG.info("Table " + tableName + " created");
    waitForActive(client, tableName);
}

From source file:CreateUserLoginTable.java

License:Open Source License

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

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    String tableName = "UserLogin";

    try {/*from   w w w .j  ava  2  s .c om*/
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName,
                Arrays.asList(new KeySchemaElement("UserID", KeyType.HASH)), //Partition key
                Arrays.asList(new AttributeDefinition("UserID", ScalarAttributeType.N)),
                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:CreateUserFavoritesTable.java

License:Open Source License

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

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    String tableName = "UserFavorites";

    try {//from w  w w .  ja va 2  s . c om
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName,
                Arrays.asList(new KeySchemaElement("UserID", KeyType.HASH)), //Partition key
                Arrays.asList(new AttributeDefinition("UserID", ScalarAttributeType.N)),
                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:CreateUserInfoTable.java

License:Open Source License

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

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    String tableName = "UserInfo";

    try {/*from   w w  w .j  ava2s.  c o m*/
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName,
                Arrays.asList(new KeySchemaElement("UserID", KeyType.HASH)), //Partition key
                Arrays.asList(new AttributeDefinition("UserID", ScalarAttributeType.N)),
                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:CreateUserRoutesTable.java

License:Open Source License

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

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());

    DynamoDB dynamoDB = new DynamoDB(client);

    String tableName = "UserRoutes";

    try {/* w ww.  j  a  v a  2s . com*/
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName,
                Arrays.asList(new KeySchemaElement("UserID", KeyType.HASH)), //Partition key
                Arrays.asList(new AttributeDefinition("UserID", ScalarAttributeType.N)),
                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:aws.example.dynamodb.CreateTable.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    CreateTable <table>\n\n" + "Where:\n"
            + "    table - the table to create.\n\n" + "Example:\n" + "    CreateTable HelloTable\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);// w  w  w  . ja v  a2  s.c o m
    }

    /* Read the name from command args */
    String table_name = args[0];

    System.out.format("Creating table \"%s\" with a simple primary key: \"Name\".\n", table_name);

    CreateTableRequest request = new CreateTableRequest()
            .withAttributeDefinitions(new AttributeDefinition("Name", ScalarAttributeType.S))
            .withKeySchema(new KeySchemaElement("Name", KeyType.HASH))
            .withProvisionedThroughput(new ProvisionedThroughput(new Long(10), new Long(10)))
            .withTableName(table_name);

    final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        CreateTableResult result = ddb.createTable(request);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

From source file:aws.example.dynamodb.CreateTableCompositeKey.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    CreateTable <table>\n\n" + "Where:\n"
            + "    table - the table to create.\n\n" + "Example:\n" + "    CreateTable GreetingsTable\n";

    if (args.length < 1) {
        System.out.println(USAGE);
        System.exit(1);/*from w w  w  .  j  av  a2 s . c  o m*/
    }

    /* Read the name from command args */
    String table_name = args[0];

    System.out.format("Creating table %s\n with a composite primary key:\n");
    System.out.format("* Language - partition key\n");
    System.out.format("* Greeting - sort key\n");

    CreateTableRequest request = new CreateTableRequest()
            .withAttributeDefinitions(new AttributeDefinition("Language", ScalarAttributeType.S),
                    new AttributeDefinition("Greeting", ScalarAttributeType.S))
            .withKeySchema(new KeySchemaElement("Language", KeyType.HASH),
                    new KeySchemaElement("Greeting", KeyType.RANGE))
            .withProvisionedThroughput(new ProvisionedThroughput(new Long(10), new Long(10)))
            .withTableName(table_name);

    final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        CreateTableResult result = ddb.createTable(request);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

From source file:aws.example.dynamodb.Query.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    Query <table> <read> <write>\n\n" + "Where:\n"
            + "    table - the table to put the item in.\n"
            + "    read  - the new read capacity of the table.\n"
            + "    write - the new write capacity of the table.\n\n" + "Example:\n"
            + "    Query HelloTable 16 10\n";

    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);/*w  ww.  ja v a 2s  .  c  o m*/
    }

    String table_name = args[0];
    Long read_capacity = Long.parseLong(args[1]);
    Long write_capacity = Long.parseLong(args[2]);

    System.out.format("Updating %s with new provisioned throughput values\n", table_name);
    System.out.format("Read capacity : %d\n", read_capacity);
    System.out.format("Write capacity : %d\n", write_capacity);

    ProvisionedThroughput table_throughput = new ProvisionedThroughput(read_capacity, write_capacity);

    final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        ddb.updateTable(table_name, table_throughput);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

From source file:aws.example.dynamodb.UpdateTable.java

License:Open Source License

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    UpdateTable <table> <read> <write>\n\n" + "Where:\n"
            + "    table - the table to put the item in.\n"
            + "    read  - the new read capacity of the table.\n"
            + "    write - the new write capacity of the table.\n\n" + "Example:\n"
            + "    UpdateTable HelloTable 16 10\n";

    if (args.length < 3) {
        System.out.println(USAGE);
        System.exit(1);/*  w  ww .j a v  a  2s .c om*/
    }

    String table_name = args[0];
    Long read_capacity = Long.parseLong(args[1]);
    Long write_capacity = Long.parseLong(args[2]);

    System.out.format("Updating %s with new provisioned throughput values\n", table_name);
    System.out.format("Read capacity : %d\n", read_capacity);
    System.out.format("Write capacity : %d\n", write_capacity);

    ProvisionedThroughput table_throughput = new ProvisionedThroughput(read_capacity, write_capacity);

    final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient();

    try {
        ddb.updateTable(table_name, table_throughput);
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}

From source file:com.alertlogic.aws.analytics.poc.DynamoDBUtils.java

License:Open Source License

/**
 * Creates the table to store our counts in with a hash key of "resource" and a range key of "timestamp" so we can
 * query counts for a given resource by time. This uses an initial provisioned throughput of 10 read capacity units
 * and 5 write capacity units// www.ja v  a 2  s . c  o  m
 *
 * @param tableName The name of the table to create.
 */
public void createCountTableIfNotExists(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 {
        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
    }
}