List of usage examples for com.amazonaws.services.dynamodbv2.model CreateTableRequest setTableName
public void setTableName(String tableName)
The name of the table to create.
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 . c o 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: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 {/*from w w w . ja v a 2 s . 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:org.apache.gora.dynamodb.store.DynamoDBUtils.java
License:Apache License
/** * Builds the necessary requests to create tables * /*ww w .j a va 2 s. c o m*/ * @param tableName * @param keySchema * @param proThrou * @param attrs * @return */ public static CreateTableRequest buildCreateTableRequest(String tableName, ArrayList<KeySchemaElement> keySchema, ProvisionedThroughput proThrou, Map<String, String> attrs) { CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.setTableName(tableName); createTableRequest.setKeySchema(keySchema); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); for (KeySchemaElement kEle : keySchema) { AttributeDefinition attrDef = new AttributeDefinition(); attrDef.setAttributeName(kEle.getAttributeName()); attrDef.setAttributeType(attrs.get(kEle.getAttributeName())); attributeDefinitions.add(attrDef); } createTableRequest.setAttributeDefinitions(attributeDefinitions); createTableRequest.setProvisionedThroughput(proThrou); return createTableRequest; }
From source file:org.apache.metamodel.dynamodb.DynamoDbTableCreationBuilder.java
License:Apache License
@Override public Table execute() throws MetaModelException { final MutableTable table = getTable(); final String tableName = table.getName(); final Collection<AttributeDefinition> attributes = new ArrayList<>(); final Collection<KeySchemaElement> keySchema = new ArrayList<>(); final Collection<GlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>(); final long readCapacity = Long .parseLong(System.getProperty(DynamoDbDataContext.SYSTEM_PROPERTY_THROUGHPUT_READ_CAPACITY, "5")); final long writeCapacity = Long .parseLong(System.getProperty(DynamoDbDataContext.SYSTEM_PROPERTY_THROUGHPUT_WRITE_CAPACITY, "5")); final ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(readCapacity, writeCapacity); for (Column column : table.getColumns()) { if (column.isPrimaryKey()) { final KeyType keyType = getKeyType(column.getRemarks()); keySchema.add(new KeySchemaElement(column.getName(), keyType)); attributes.add(// w w w . j av a 2s . c o m new AttributeDefinition(column.getName(), DynamoDbUtils.toAttributeType(column.getType()))); } } final CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.setTableName(tableName); createTableRequest.setAttributeDefinitions(attributes); createTableRequest.setGlobalSecondaryIndexes(globalSecondaryIndices); createTableRequest.setKeySchema(keySchema); createTableRequest.setProvisionedThroughput(provisionedThroughput); final AmazonDynamoDB client = getUpdateCallback().getDataContext().getDynamoDb(); final CreateTableResult createTableResult = client.createTable(createTableRequest); // await the table creation to be "done". { String tableStatus = createTableResult.getTableDescription().getTableStatus(); while (TableStatus.CREATING.name().equals(tableStatus)) { logger.debug("Waiting for table status to be ACTIVE. Currently: {}", tableStatus); try { Thread.sleep(300); } catch (InterruptedException e) { getUpdateCallback().setInterrupted(true); } tableStatus = client.describeTable(tableName).getTable().getTableStatus(); } } return table; }