private void createRecipientTable() { CreateTableRequest request = new CreateTableRequest() .withTableName(TABLE_NAME) .withAttributeDefinitions( new AttributeDefinition("_id", ScalarAttributeType.S) ) .withKeySchema( new KeySchemaElement("_id", KeyType.HASH) ) .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L)); ddb.createTable(request); try { TableUtils.waitUntilActive(ddb, TABLE_NAME); } catch (InterruptedException e) { throw new RuntimeException(e); } }
public static void main(String[] args) throws Exception { AmazonDynamoDBClient client = new AmazonDynamoDBClient(); client.setEndpoint("http://localhost:8000"); DynamoDB dynamoDB = new DynamoDB(client); String tableName = "Movies"; Table table = dynamoDB.createTable(tableName, Arrays.asList( new KeySchemaElement("year", KeyType.HASH), new KeySchemaElement("title", KeyType.RANGE)), Arrays.asList( new AttributeDefinition("year", ScalarAttributeType.N), new AttributeDefinition("title", ScalarAttributeType.S)), new ProvisionedThroughput(10L, 10L)); try { TableUtils.waitUntilActive(client, tableName); System.out.println("Table status: " + table.getDescription().getTableStatus()); } catch (AmazonClientException e) { e.printStackTrace(); System.exit(1); } }
/** * Create ticket tables. * * @param deleteTables the delete tables */ public void createTicketTables(final boolean deleteTables) { final Collection<TicketDefinition> metadata = this.ticketCatalog.findAll(); metadata.forEach(Unchecked.consumer(r -> { final CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions(new AttributeDefinition(ColumnNames.ID.getName(), ScalarAttributeType.S)) .withKeySchema(new KeySchemaElement(ColumnNames.ID.getName(), KeyType.HASH)) .withProvisionedThroughput(new ProvisionedThroughput(dynamoDbProperties.getReadCapacity(), dynamoDbProperties.getWriteCapacity())) .withTableName(r.getProperties().getStorageName()); if (deleteTables) { final DeleteTableRequest delete = new DeleteTableRequest(r.getProperties().getStorageName()); LOGGER.debug("Sending delete request [{}] to remove table if necessary", delete); TableUtils.deleteTableIfExists(amazonDynamoDBClient, delete); } LOGGER.debug("Sending delete request [{}] to create table", request); TableUtils.createTableIfNotExists(amazonDynamoDBClient, request); LOGGER.debug("Waiting until table [{}] becomes active...", request.getTableName()); TableUtils.waitUntilActive(amazonDynamoDBClient, request.getTableName()); final DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(request.getTableName()); LOGGER.debug("Sending request [{}] to obtain table description...", describeTableRequest); final TableDescription tableDescription = amazonDynamoDBClient.describeTable(describeTableRequest).getTable(); LOGGER.debug("Located newly created table with description: [{}]", tableDescription); })); }
/** * Create tables. * * @param deleteTables the delete tables */ public void createServicesTable(final boolean deleteTables) { try { final CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions(new AttributeDefinition(ColumnNames.ID.getName(), ScalarAttributeType.S)) .withKeySchema(new KeySchemaElement(ColumnNames.ID.getName(), KeyType.HASH)) .withProvisionedThroughput(new ProvisionedThroughput(dynamoDbProperties.getReadCapacity(), dynamoDbProperties.getWriteCapacity())) .withTableName(TABLE_NAME); if (deleteTables) { final DeleteTableRequest delete = new DeleteTableRequest(request.getTableName()); LOGGER.debug("Sending delete request [{}] to remove table if necessary", delete); TableUtils.deleteTableIfExists(amazonDynamoDBClient, delete); } LOGGER.debug("Sending delete request [{}] to create table", request); TableUtils.createTableIfNotExists(amazonDynamoDBClient, request); LOGGER.debug("Waiting until table [{}] becomes active...", request.getTableName()); TableUtils.waitUntilActive(amazonDynamoDBClient, request.getTableName()); final DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(request.getTableName()); LOGGER.debug("Sending request [{}] to obtain table description...", describeTableRequest); final TableDescription tableDescription = amazonDynamoDBClient.describeTable(describeTableRequest).getTable(); LOGGER.debug("Located newly created table with description: [{}]", tableDescription); } catch (final Exception e) { throw Throwables.propagate(e); } }
private static void createSettingsTable(final AmazonDynamoDBClient amazonDynamoDBClient, final boolean deleteTables) { try { final CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions(new AttributeDefinition(ColumnNames.ID.getName(), ScalarAttributeType.S)) .withKeySchema(new KeySchemaElement(ColumnNames.ID.getName(), KeyType.HASH)) .withProvisionedThroughput(new ProvisionedThroughput(PROVISIONED_THROUGHPUT, PROVISIONED_THROUGHPUT)) .withTableName(TABLE_NAME); if (deleteTables) { final DeleteTableRequest delete = new DeleteTableRequest(request.getTableName()); LOGGER.debug("Sending delete request [{}] to remove table if necessary", delete); TableUtils.deleteTableIfExists(amazonDynamoDBClient, delete); } LOGGER.debug("Sending delete request [{}] to create table", request); TableUtils.createTableIfNotExists(amazonDynamoDBClient, request); LOGGER.debug("Waiting until table [{}] becomes active...", request.getTableName()); TableUtils.waitUntilActive(amazonDynamoDBClient, request.getTableName()); final DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(request.getTableName()); LOGGER.debug("Sending request [{}] to obtain table description...", describeTableRequest); final TableDescription tableDescription = amazonDynamoDBClient.describeTable(describeTableRequest).getTable(); LOGGER.debug("Located newly created table with description: [{}]", tableDescription); } catch (final Exception e) { throw Throwables.propagate(e); } }
private static void createDDBTable(String uuid, Properties props, String tableName, String tableKey, String tblPropName, String keyPropName) { CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName + uuid) .withKeySchema(new KeySchemaElement().withAttributeName(tableKey).withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName(tableKey) .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L)); TableUtils.createTableIfNotExists(getDynamoDB(), createTableRequest); props.setProperty(tblPropName, tableName + uuid); props.setProperty(keyPropName, tableKey); }
@AfterClass public static void deleteTable() { final DeleteTableRequest deleteTableRequest = new DeleteTableRequest(tableName); // credentials need to be set in local environment // see http://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html TableUtils.deleteTableIfExists(new AmazonDynamoDBClient(), deleteTableRequest); }
private void ensureTableExists() throws AlexaStateException { // given custom table is always assumed as existing so you can have this option to bypass existance checks // for reason of least privileges on used AWS credentials or better performance if (!tableExistenceApproved || !tableExists()) { final ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>(); // describe keys (both will be Strings) attributeDefinitions.add(new AttributeDefinition() .withAttributeName(pkUser) .withAttributeType("S")); attributeDefinitions.add(new AttributeDefinition() .withAttributeName(pkModel) .withAttributeType("S")); // define keys with name and type final ArrayList<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement() .withAttributeName(pkUser) .withKeyType(KeyType.HASH)); keySchema.add(new KeySchemaElement() .withAttributeName(pkModel) .withKeyType(KeyType.RANGE)); // prepare table creation request final CreateTableRequest awsRequest = new CreateTableRequest() .withTableName(tableName) .withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions) .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits)); // create on not existing table if (TableUtils.createTableIfNotExists(awsClient, awsRequest)) { log.info(String.format("Table '%1$s' is created in DynamoDB. Now standing by for up to ten minutes for this table to be in active state.", tableName)); // wait for table to be in ACTIVE state in order to proceed with read or write // this could take up to possible ten minutes so be sure to run this code once before publishing your skill ;) try { TableUtils.waitUntilActive(awsClient, awsRequest.getTableName()); } catch (final InterruptedException e) { final String message = String.format("Could not create DynamoDb-Table '%1$s' before writing state", tableName); log.error(message, e); throw AlexaStateException.create(message).withCause(e).withHandler(this).build(); } } } }