Search in sources :

Example 1 with LocalSecondaryIndex

use of com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex 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 2 with LocalSecondaryIndex

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

the class CreateTablesLoadData method createTable.

private static void createTable(String tableName, long readCapacityUnits, long writeCapacityUnits, String partitionKeyName, String partitionKeyType, String sortKeyName, String sortKeyType) {
    try {
        ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>();
        // Partition
        keySchema.add(new KeySchemaElement().withAttributeName(partitionKeyName).withKeyType(KeyType.HASH));
        // key
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        attributeDefinitions.add(new AttributeDefinition().withAttributeName(partitionKeyName).withAttributeType(partitionKeyType));
        if (sortKeyName != null) {
            // Sort
            keySchema.add(new KeySchemaElement().withAttributeName(sortKeyName).withKeyType(KeyType.RANGE));
            // key
            attributeDefinitions.add(new AttributeDefinition().withAttributeName(sortKeyName).withAttributeType(sortKeyType));
        }
        CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacityUnits).withWriteCapacityUnits(writeCapacityUnits));
        // If this is the Reply table, define a local secondary index
        if (replyTableName.equals(tableName)) {
            attributeDefinitions.add(new AttributeDefinition().withAttributeName("PostedBy").withAttributeType("S"));
            ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<LocalSecondaryIndex>();
            localSecondaryIndexes.add(new LocalSecondaryIndex().withIndexName("PostedBy-Index").withKeySchema(// Partition
            new KeySchemaElement().withAttributeName(partitionKeyName).withKeyType(KeyType.HASH), // key
            new KeySchemaElement().withAttributeName("PostedBy").withKeyType(// Sort
            KeyType.RANGE)).withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY)));
            request.setLocalSecondaryIndexes(localSecondaryIndexes);
        }
        request.setAttributeDefinitions(attributeDefinitions);
        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();
    } catch (Exception e) {
        System.err.println("CreateTable request failed for " + tableName);
        System.err.println(e.getMessage());
    }
}
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 3 with LocalSecondaryIndex

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

the class LowLevelLocalSecondaryIndexExample 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 key and sort key
    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 sort 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(client.createTable(createTableRequest));
    waitForTableToBecomeAvailable(tableName);
}
Also used : LocalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex) 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)

Aggregations

AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)3 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)3 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)3 LocalSecondaryIndex (com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex)3 Projection (com.amazonaws.services.dynamodbv2.model.Projection)3 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)3 ArrayList (java.util.ArrayList)3 Table (com.amazonaws.services.dynamodbv2.document.Table)2