Search in sources :

Example 6 with KeySchemaElement

use of com.amazonaws.services.dynamodbv2.model.KeySchemaElement in project aws-doc-sdk-examples by awsdocs.

the class StreamsAdapterDemoHelper method createTable.

/**
 * @return StreamArn
 */
public static String createTable(AmazonDynamoDB client, String tableName) {
    java.util.List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N"));
    java.util.List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
    // Partition
    keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH));
    // key
    ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(2L).withWriteCapacityUnits(2L);
    StreamSpecification streamSpecification = new StreamSpecification();
    streamSpecification.setStreamEnabled(true);
    streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE);
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName).withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema).withProvisionedThroughput(provisionedThroughput).withStreamSpecification(streamSpecification);
    try {
        System.out.println("Creating table " + tableName);
        CreateTableResult result = client.createTable(createTableRequest);
        return result.getTableDescription().getLatestStreamArn();
    } catch (ResourceInUseException e) {
        System.out.println("Table already exists.");
        return describeTable(client, tableName).getTable().getLatestStreamArn();
    }
}
Also used : StreamSpecification(com.amazonaws.services.dynamodbv2.model.StreamSpecification) ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) ArrayList(java.util.ArrayList) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) CreateTableResult(com.amazonaws.services.dynamodbv2.model.CreateTableResult)

Example 7 with KeySchemaElement

use of com.amazonaws.services.dynamodbv2.model.KeySchemaElement in project aws-doc-sdk-examples by awsdocs.

the class DocumentAPILocalSecondaryIndexExample method createTable.

public static void createTable() {
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits((long) 1).withWriteCapacityUnits((long) 1));
    // Attribute definitions for table partition and sort keys
    ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("CustomerId").withAttributeType("S"));
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("OrderId").withAttributeType("N"));
    // Attribute definition for index primary key attributes
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("OrderCreationDate").withAttributeType("N"));
    attributeDefinitions.add(new AttributeDefinition().withAttributeName("IsOpen").withAttributeType("N"));
    createTableRequest.setAttributeDefinitions(attributeDefinitions);
    // Key schema for table
    ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>();
    // Partition
    tableKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH));
    // key
    // Sort
    tableKeySchema.add(new KeySchemaElement().withAttributeName("OrderId").withKeyType(KeyType.RANGE));
    // key
    createTableRequest.setKeySchema(tableKeySchema);
    ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<LocalSecondaryIndex>();
    // OrderCreationDateIndex
    LocalSecondaryIndex orderCreationDateIndex = new LocalSecondaryIndex().withIndexName("OrderCreationDateIndex");
    // Key schema for OrderCreationDateIndex
    ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>();
    // Partition
    indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH));
    // key
    // Sort
    indexKeySchema.add(new KeySchemaElement().withAttributeName("OrderCreationDate").withKeyType(KeyType.RANGE));
    // key
    orderCreationDateIndex.setKeySchema(indexKeySchema);
    // Projection (with list of projected attributes) for
    // OrderCreationDateIndex
    Projection projection = new Projection().withProjectionType(ProjectionType.INCLUDE);
    ArrayList<String> nonKeyAttributes = new ArrayList<String>();
    nonKeyAttributes.add("ProductCategory");
    nonKeyAttributes.add("ProductName");
    projection.setNonKeyAttributes(nonKeyAttributes);
    orderCreationDateIndex.setProjection(projection);
    localSecondaryIndexes.add(orderCreationDateIndex);
    // IsOpenIndex
    LocalSecondaryIndex isOpenIndex = new LocalSecondaryIndex().withIndexName("IsOpenIndex");
    // Key schema for IsOpenIndex
    indexKeySchema = new ArrayList<KeySchemaElement>();
    // Partition
    indexKeySchema.add(new KeySchemaElement().withAttributeName("CustomerId").withKeyType(KeyType.HASH));
    // key
    // Sort
    indexKeySchema.add(new KeySchemaElement().withAttributeName("IsOpen").withKeyType(KeyType.RANGE));
    // key
    // Projection (all attributes) for IsOpenIndex
    projection = new Projection().withProjectionType(ProjectionType.ALL);
    isOpenIndex.setKeySchema(indexKeySchema);
    isOpenIndex.setProjection(projection);
    localSecondaryIndexes.add(isOpenIndex);
    // Add index definitions to CreateTable request
    createTableRequest.setLocalSecondaryIndexes(localSecondaryIndexes);
    System.out.println("Creating table " + tableName + "...");
    System.out.println(dynamoDB.createTable(createTableRequest));
    // Wait for table to become active
    System.out.println("Waiting for " + tableName + " to become ACTIVE...");
    try {
        Table table = dynamoDB.getTable(tableName);
        table.waitForActive();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : LocalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex) Table(com.amazonaws.services.dynamodbv2.document.Table) ArrayList(java.util.ArrayList) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) Projection(com.amazonaws.services.dynamodbv2.model.Projection) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 8 with KeySchemaElement

use of com.amazonaws.services.dynamodbv2.model.KeySchemaElement in project aws-doc-sdk-examples by awsdocs.

the class MoviesCreateTable method main.

public static void main(String[] args) throws Exception {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
    DynamoDB dynamoDB = new DynamoDB(client);
    String tableName = "Movies";
    try {
        System.out.println("Attempting to create table; please wait...");
        Table table = dynamoDB.createTable(tableName, // Partition
        Arrays.asList(// Partition
        new KeySchemaElement("year", KeyType.HASH), // key
        new KeySchemaElement("title", // Sort key
        KeyType.RANGE)), 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());
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 9 with KeySchemaElement

use of com.amazonaws.services.dynamodbv2.model.KeySchemaElement in project aws-doc-sdk-examples by awsdocs.

the class TryDaxHelper method createTable.

void createTable(String tableName, DynamoDB client) {
    Table table = client.getTable(tableName);
    try {
        System.out.println("Attempting to create table; please wait...");
        table = client.createTable(tableName, Arrays.asList(// Partition key
        new KeySchemaElement("pk", KeyType.HASH), // Sort key
        new KeySchemaElement("sk", KeyType.RANGE)), Arrays.asList(new AttributeDefinition("pk", ScalarAttributeType.N), new AttributeDefinition("sk", ScalarAttributeType.N)), new ProvisionedThroughput(10L, 10L));
        table.waitForActive();
        System.out.println("Successfully created table.  Table status: " + table.getDescription().getTableStatus());
    } catch (Exception e) {
        System.err.println("Unable to create table: ");
        e.printStackTrace();
    }
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 10 with KeySchemaElement

use of com.amazonaws.services.dynamodbv2.model.KeySchemaElement in project aws-doc-sdk-examples by awsdocs.

the class CreateTable method main.

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);
    }
    /* 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 AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        CreateTableResult result = ddb.createTable(request);
        System.out.println(result.getTableDescription().getTableName());
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
    System.out.println("Done!");
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) CreateTableResult(com.amazonaws.services.dynamodbv2.model.CreateTableResult)

Aggregations

KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)30 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)23 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)22 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)21 ArrayList (java.util.ArrayList)16 Table (com.amazonaws.services.dynamodbv2.document.Table)9 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)8 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)6 CreateTableResult (com.amazonaws.services.dynamodbv2.model.CreateTableResult)5 DescribeTableResult (com.amazonaws.services.dynamodbv2.model.DescribeTableResult)5 Projection (com.amazonaws.services.dynamodbv2.model.Projection)5 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4 Test (org.junit.Test)4 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)3 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)3 LocalSecondaryIndex (com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex)3 StreamSpecification (com.amazonaws.services.dynamodbv2.model.StreamSpecification)3 AmazonClientException (com.amazonaws.AmazonClientException)2 ConsumedCapacity (com.amazonaws.services.dynamodbv2.model.ConsumedCapacity)2