Example usage for com.amazonaws.services.dynamodbv2.document Table waitForActive

List of usage examples for com.amazonaws.services.dynamodbv2.document Table waitForActive

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document Table waitForActive.

Prototype

public TableDescription waitForActive() throws InterruptedException 

Source Link

Document

A convenient blocking call that can be used, typically during table creation, to wait for the table to become active.

Usage

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 a  v  a 2  s  .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: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  . jav  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: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 {//www  .j  ava  2 s .  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 {//from w  w  w . j  ava 2s .  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: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  ww  w.j a v a 2 s.  c  o m*/

    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.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
 *///from   www  . j a  v a  2  s  .  c  o  m
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.intuit.tank.persistence.databases.AmazonDynamoDatabaseDocApi.java

License:Open Source License

private void addItemsToTable(String tableName, final BatchWriteItemRequest request) {

    boolean shouldRetry;
    int retries = 0;

    do {//from ww  w.  j  a va 2  s .c  o  m
        shouldRetry = false;
        try {
            BatchWriteItemResult result = dynamoDb.batchWriteItem(request);
            if (result != null) {
                try {
                    List<ConsumedCapacity> consumedCapacity = result.getConsumedCapacity();
                    for (ConsumedCapacity cap : consumedCapacity) {
                        logger.info(cap.getCapacityUnits());
                    }
                } catch (Exception e) {
                    // ignore this
                }
            }
        } catch (AmazonServiceException e) {
            if (e instanceof ProvisionedThroughputExceededException) {
                try {
                    DynamoDB db = new DynamoDB(dynamoDb);
                    Table table = db.getTable(tableName);
                    ProvisionedThroughputDescription oldThroughput = table.getDescription()
                            .getProvisionedThroughput();
                    logger.info("ProvisionedThroughputExceeded throughput = " + oldThroughput);
                    ProvisionedThroughput newThroughput = new ProvisionedThroughput()
                            .withReadCapacityUnits(
                                    table.getDescription().getProvisionedThroughput().getReadCapacityUnits())
                            .withWriteCapacityUnits(getIncreasedThroughput(
                                    table.getDescription().getProvisionedThroughput().getReadCapacityUnits()));
                    if (!oldThroughput.equals(newThroughput)) {
                        logger.info("Updating throughput to " + newThroughput);
                        table.updateTable(newThroughput);
                        table.waitForActive();
                    }
                } catch (Exception e1) {
                    logger.error("Error increasing capacity: " + e, e);
                }
            }
            int status = e.getStatusCode();
            if (status == HttpStatus.SC_INTERNAL_SERVER_ERROR || status == HttpStatus.SC_SERVICE_UNAVAILABLE) {
                shouldRetry = true;
                long delay = (long) (Math.random() * (Math.pow(4, retries++) * 100L));
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException iex) {
                    logger.error("Caught InterruptedException exception", iex);
                }
            } else {
                logger.error("Error writing to DB: " + e.getMessage());
                throw new RuntimeException(e);
            }
        }
    } while (shouldRetry && retries < MAX_NUMBER_OF_RETRIES);

}

From source file:com.telefonica.iot.cygnus.backends.dynamo.DynamoDBBackendImpl.java

License:Open Source License

@Override
public void createTable(String tableName, String primaryKey) throws Exception {
    try {//from  w  w w . j  av  a 2 s.  c  om
        // Create the key schema for the given primary key
        ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
        keySchema.add(new KeySchemaElement().withAttributeName(primaryKey).withKeyType(KeyType.HASH));

        // Create the attribute definitions
        ArrayList<AttributeDefinition> attrDefs = new ArrayList<AttributeDefinition>();
        attrDefs.add(new AttributeDefinition().withAttributeName(primaryKey).withAttributeType("N"));

        // Create the table request given the table name, the key schema and the attribute definitios
        CreateTableRequest tableRequest = new CreateTableRequest().withTableName(tableName)
                .withKeySchema(keySchema).withAttributeDefinitions(attrDefs).withProvisionedThroughput(
                        new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(5L));

        // Create the table
        LOGGER.debug("Creating DynamoDB table " + tableName);
        Table table = dynamoDB.createTable(tableRequest);

        // Wait until the table is active
        LOGGER.debug("Waiting until the DynamoDB table " + tableName + " becomes active");
        table.waitForActive();
    } catch (Exception e) {
        LOGGER.error("Error while creating the DynamoDB table " + tableName + ". Details=" + e.getMessage());
    } // try catch
}

From source file:dynamodb.CrudOperationsOnDynamoDBTable.java

License:Open Source License

static void createExampleTable() {

    try {/* w  w  w .  ja v a 2s  .c  o m*/
        System.out.println("Creating table ...");
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));

        ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
        keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); //Partition key
        CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema)
                .withAttributeDefinitions(attributeDefinitions).withProvisionedThroughput(
                        new ProvisionedThroughput().withReadCapacityUnits(5L).withWriteCapacityUnits(6L));

        System.out.println("Issuing CreateTable request for " + tableName);
        Table table = dynamoDB.createTable(request);

        System.out.println("Waiting for " + tableName + " to be created...this may take a while...");
        table.waitForActive();
        System.out.println(tableName + " table activated successfully ...");
        getTableInformation();

    } catch (Exception e) {
        System.err.println("CreateTable request failed for " + tableName);
        System.err.println(e.getMessage());
    }

}

From source file:dynamodb.CrudOperationsOnDynamoDBTable.java

License:Open Source License

static void updateExampleTable() {

    Table table = dynamoDB.getTable(tableName);
    System.out.println("Modifying provisioned throughput for " + tableName);

    try {/*from  w  ww .  jav a2 s . com*/
        table.updateTable(new ProvisionedThroughput().withReadCapacityUnits(6L).withWriteCapacityUnits(7L));

        table.waitForActive();
    } catch (final Exception e) {
        System.err.println("UpdateTable request failed for " + tableName);
        System.err.println(e.getMessage());
    }
}