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();
}
}
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();
}
}
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());
}
}
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();
}
}
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!");
}
Aggregations