List of usage examples for com.amazonaws.services.dynamodbv2.document Table waitForActive
public TableDescription waitForActive() throws InterruptedException
From source file:DynamoDB.sample.CreateTablesLoadData.java
License:Open Source License
private static void createTable(String tableName, long readCapacityUnits, long writeCapacityUnits, String partitionKeyName, String partitionKeyType, String sortKeyName, String sortKeyType) { try {// w ww. j a v a 2 s .com ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName(partitionKeyName).withKeyType(KeyType.HASH)); //Partition key ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName(partitionKeyName) .withAttributeType(partitionKeyType)); if (sortKeyName != null) { keySchema.add(new KeySchemaElement().withAttributeName(sortKeyName).withKeyType(KeyType.RANGE)); //Sort 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( new KeySchemaElement().withAttributeName(partitionKeyName) .withKeyType(KeyType.HASH), //Partition key new KeySchemaElement().withAttributeName("PostedBy").withKeyType(KeyType.RANGE)) //Sort key .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()); } }
From source file:org.diksha.common.dyutils.SchedulerDynamoTable.java
License:Apache License
public void createDynamoTable(DynamoDB dynamoDB) { try {//from w ww .jav a 2 s . c om System.out.println(toString()); ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName(hashKeyName).withKeyType(KeyType.HASH)); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions .add(new AttributeDefinition().withAttributeName(hashKeyName).withAttributeType(hashKeyType)); if (rangeKeyType != null) { if (!rangeKeyType.isEmpty()) { attributeDefinitions.add(new AttributeDefinition().withAttributeName(rangeKeyName) .withAttributeType(rangeKeyType)); } } if (additionalAttributes.size() > 0) { for (int cnt = 0; cnt < additionalAttributes.size(); cnt++) { String[] args = (additionalAttributes.get(cnt).toString()).split(":"); if (args.length == 2) { attributeDefinitions.add( new AttributeDefinition().withAttributeName(args[0]).withAttributeType(args[1])); } } } CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema) .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits)); ArrayList<LocalSecondaryIndex> localSecondaryIndexes = new ArrayList<LocalSecondaryIndex>(); ArrayList<GlobalSecondaryIndex> globalSecondaryIndexes = new ArrayList<GlobalSecondaryIndex>(); if (rangeKeyType != null) { if (!rangeKeyType.isEmpty()) { if (rangeKeyTypeGlobalOrLocal.equals("L")) { localSecondaryIndexes .add(new LocalSecondaryIndex().withIndexName(rangeKeyName + "-Index") .withKeySchema( new KeySchemaElement().withAttributeName(hashKeyName) .withKeyType(KeyType.HASH), new KeySchemaElement().withAttributeName(rangeKeyName) .withKeyType(KeyType.RANGE)) .withProjection( new Projection().withProjectionType(ProjectionType.KEYS_ONLY))); } else { globalSecondaryIndexes .add(new GlobalSecondaryIndex().withIndexName(rangeKeyName + "-Index") .withKeySchema( new KeySchemaElement().withAttributeName(hashKeyName) .withKeyType(KeyType.HASH), new KeySchemaElement().withAttributeName(rangeKeyName) .withKeyType(KeyType.RANGE)) .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(indexReadCapacityUnits) .withWriteCapacityUnits(indexWriteCapacityUnits)) .withProjection( new Projection().withProjectionType(ProjectionType.KEYS_ONLY))); } } } if (indices.size() > 0) { for (int cnt = 0; cnt < indices.size(); cnt++) { SchedulerDynamoTableIndex schedulerDynamoTableIndex = indices.get(cnt); if (schedulerDynamoTableIndex.isGlobal()) { // System.out.println("IS GLOBAL"); globalSecondaryIndexes.add(schedulerDynamoTableIndex.getGlobalSecondaryIndex()); // System.out.println("size = " + globalSecondaryIndexes.size()); } } } if (localSecondaryIndexes.size() > 0) request.setLocalSecondaryIndexes(localSecondaryIndexes); if (globalSecondaryIndexes.size() > 0) request.setGlobalSecondaryIndexes(globalSecondaryIndexes); 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()); } }
From source file:org.wildfly.camel.test.common.aws.DynamoDBUtils.java
License:Apache License
public static TableDescription createTable(AmazonDynamoDB client, String tableName) throws InterruptedException { CreateTableRequest tableReq = new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement("Id", KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition("Id", ScalarAttributeType.N)) .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L)) .withStreamSpecification(new StreamSpecification().withStreamEnabled(true) .withStreamViewType(StreamViewType.NEW_AND_OLD_IMAGES)); DynamoDB dynamoDB = new DynamoDB(client); Table table = dynamoDB.createTable(tableReq); return table.waitForActive(); }