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

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

Introduction

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

Prototype

public KeySchemaElement(String attributeName, KeyType keyType) 

Source Link

Document

Constructs a new KeySchemaElement 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  w w .  j a v  a 2s .  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  a  va 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: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 {/* 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  .  jav a 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: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 ww  w .java 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: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);//from  ww  w . java2  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);/*w w  w . j  a va2s.co  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:com.amazon.services.awsrum.utils.DynamoDBUtils.java

License:Open Source License

public static void createTable(AmazonDynamoDBClient client, String tableName, String key, String rangeKey,
        long readCapacityUnits, long writeCapacityUnits) {
    if (tableExists(client, tableName)) {
        if (tableHasCorrectSchema(client, tableName, key, rangeKey)) {
            waitForActive(client, tableName);
            return;
        } else {// ww w  .  j  a  v  a 2s.  c o  m
            LOG.error("Table already exists and schema does not match");
        }

    }
    CreateTableRequest createTableRequest = new CreateTableRequest();
    createTableRequest.setTableName(tableName);
    createTableRequest.setKeySchema(Arrays.asList(new KeySchemaElement(key, KeyType.HASH),
            new KeySchemaElement(rangeKey, KeyType.RANGE)));
    createTableRequest
            .setProvisionedThroughput(new ProvisionedThroughput(readCapacityUnits, writeCapacityUnits));
    createTableRequest
            .setAttributeDefinitions(Arrays.asList(new AttributeDefinition(key, ScalarAttributeType.S),
                    new AttributeDefinition(rangeKey, ScalarAttributeType.N)));
    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:com.clicktravel.infrastructure.persistence.aws.dynamodb.integration.DynamoDbDataGenerator.java

License:Apache License

void createStubItemTable() throws Exception {
    final String tableName = unitTestSchemaName + "." + stubItemTableName;
    boolean tableCreated = false;
    try {/*  w w  w.j  av  a 2s. com*/
        final DescribeTableResult result = amazonDynamoDbClient.describeTable(tableName);
        if (isTableCreated(tableName, result)) {
            tableCreated = true;
        }
    } catch (final ResourceNotFoundException e) {
        tableCreated = false;
    }
    if (!tableCreated) {
        final Collection<AttributeDefinition> attributeDefinitions = new ArrayList<>();
        attributeDefinitions.add(new AttributeDefinition("id", ScalarAttributeType.S));
        final Collection<KeySchemaElement> keySchema = new ArrayList<>();
        keySchema.add(new KeySchemaElement("id", "S").withKeyType(KeyType.HASH));
        final CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
                .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema)
                .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L));
        amazonDynamoDbClient.createTable(createTableRequest);

        final long startTime = System.currentTimeMillis();
        do {
            Thread.sleep(1000);
            final DescribeTableResult describeTableResult = amazonDynamoDbClient.describeTable(tableName);
            tableCreated = isTableCreated(tableName, describeTableResult);
        } while (!tableCreated && System.currentTimeMillis() - startTime < 60000);
    }
}

From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.integration.DynamoDbDataGenerator.java

License:Apache License

void createStubItemWithRangeTable() throws Exception {
    final String tableName = unitTestSchemaName + "." + stubItemWithRangeTableName;
    boolean tableCreated = false;
    try {//from w ww.j  a v a2s.c om
        final DescribeTableResult result = amazonDynamoDbClient.describeTable(tableName);
        if (isTableCreated(tableName, result)) {
            tableCreated = true;
        }
    } catch (final ResourceNotFoundException e) {
        tableCreated = false;
    }
    if (!tableCreated) {
        final Collection<AttributeDefinition> attributeDefinitions = new ArrayList<>();
        attributeDefinitions.add(new AttributeDefinition("id", ScalarAttributeType.S));
        attributeDefinitions.add(new AttributeDefinition("supportingId", ScalarAttributeType.S));
        final Collection<KeySchemaElement> keySchema = new ArrayList<>();
        keySchema.add(new KeySchemaElement("id", "S").withKeyType(KeyType.HASH));
        keySchema.add(new KeySchemaElement("supportingId", "S").withKeyType(KeyType.RANGE));
        final CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName)
                .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema)
                .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L));
        amazonDynamoDbClient.createTable(createTableRequest);

        final long startTime = System.currentTimeMillis();
        do {
            Thread.sleep(1000);
            final DescribeTableResult describeTableResult = amazonDynamoDbClient.describeTable(tableName);
            tableCreated = isTableCreated(tableName, describeTableResult);
        } while (!tableCreated && System.currentTimeMillis() - startTime < 60000);
    }
}