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); } }
@Override public void closeStorage() { try { dynamoDBConnection.getDynamoClient().describeTable(getTableName()); dynamoDBConnection.getDynamoClient().deleteTable(getTableName()); } catch(ResourceNotFoundException e) { } dynamoDBConnection.getDynamoDB().createTable(getTableName(), Collections.singletonList( new KeySchemaElement("_id", KeyType.HASH)), Collections.singletonList( new AttributeDefinition("_id", ScalarAttributeType.S)), new ProvisionedThroughput(1L, 1L)); }
protected Table createHashAndSortTable(String pk, String sort) throws InterruptedException { ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>(); ScalarAttributeType type = ScalarAttributeType.S; attributeDefinitions.add(new AttributeDefinition() .withAttributeName(pk).withAttributeType(type)); attributeDefinitions .add(new AttributeDefinition().withAttributeName(sort).withAttributeType(type)); ArrayList<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement().withAttributeName(pk).withKeyType(KeyType.HASH)); keySchema.add(new KeySchemaElement().withAttributeName(sort).withKeyType(KeyType.RANGE)); CreateTableRequest request = new CreateTableRequest() .withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions); return createTable(request); }
public void deploy() { final AttributeDefinition idAttr = new AttributeDefinition().withAttributeName("id") .withAttributeType(ScalarAttributeType.S); final ProvisionedThroughput throughput = new ProvisionedThroughput().withReadCapacityUnits(5L) .withWriteCapacityUnits(5L); final KeySchemaElement idKey = new KeySchemaElement().withAttributeName("id").withKeyType(KeyType.HASH); final CreateTableRequest createTableRequest = new CreateTableRequest().withTableName("TranslateSlack") .withAttributeDefinitions(idAttr) .withKeySchema(idKey) .withProvisionedThroughput(throughput); ; ; ddb.createTable(createTableRequest); }
@Override public CreateTableRequest getTableSchema() { return super.getTableSchema() .withAttributeDefinitions( new AttributeDefinition() .withAttributeName(Constants.JANUSGRAPH_HASH_KEY) .withAttributeType(ScalarAttributeType.S), new AttributeDefinition() .withAttributeName(Constants.JANUSGRAPH_RANGE_KEY) .withAttributeType(ScalarAttributeType.S)) .withKeySchema( new KeySchemaElement() .withAttributeName(Constants.JANUSGRAPH_HASH_KEY) .withKeyType(KeyType.HASH), new KeySchemaElement() .withAttributeName(Constants.JANUSGRAPH_RANGE_KEY) .withKeyType(KeyType.RANGE)); }
private CreateTableResult createTable() throws Exception { List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); AttributeDefinition attributeDefinition = new AttributeDefinition() .withAttributeName(TEST_ATTRIBUTE) .withAttributeType(ScalarAttributeType.S); attributeDefinitions.add(attributeDefinition); String tableName = TEST_TABLE_NAME; List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); KeySchemaElement keySchemaElement = new KeySchemaElement() .withAttributeName(TEST_ATTRIBUTE) .withKeyType(KeyType.HASH); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(UNITS) .withWriteCapacityUnits(UNITS); CreateTableResult result = dynamoDb.createTable(attributeDefinitions, tableName, keySchema, provisionedThroughput); return result; }
/** * Creates a table in AWS DynamoDB. * @param appid name of the {@link com.erudika.para.core.App} * @param readCapacity read capacity * @param writeCapacity write capacity * @return true if created */ public static boolean createTable(String appid, long readCapacity, long writeCapacity) { if (StringUtils.isBlank(appid)) { return false; } else if (StringUtils.containsWhitespace(appid)) { logger.warn("DynamoDB table name contains whitespace. The name '{}' is invalid.", appid); return false; } else if (existsTable(appid)) { logger.warn("DynamoDB table '{}' already exists.", appid); return false; } try { String table = getTableNameForAppid(appid); getClient().createTable(new CreateTableRequest().withTableName(table). withKeySchema(new KeySchemaElement(Config._KEY, KeyType.HASH)). withAttributeDefinitions(new AttributeDefinition(Config._KEY, ScalarAttributeType.S)). withProvisionedThroughput(new ProvisionedThroughput(readCapacity, writeCapacity))); logger.info("Created DynamoDB table '{}'.", table); } catch (Exception e) { logger.error(null, e); return false; } return true; }
@Override public void createStoreIfAbsent(String storeName, boolean bBinaryValues) { String tableName = storeToTableName(storeName); if (!Tables.doesTableExist(m_ddbClient, tableName)) { // Create a table with a primary hash key named '_key', which holds a string m_logger.info("Creating table: {}", tableName); CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement() .withAttributeName(ROW_KEY_ATTR_NAME) .withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition() .withAttributeName(ROW_KEY_ATTR_NAME) .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(READ_CAPACITY_UNITS) .withWriteCapacityUnits(WRITE_CAPACITY_UNITS)); m_ddbClient.createTable(createTableRequest).getTableDescription(); try { Tables.awaitTableToBecomeActive(m_ddbClient, tableName); } 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); } }
private static CreateTableResult createTable(AmazonDynamoDB ddb, String tableName, String hashKeyName) { List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition(hashKeyName, ScalarAttributeType.S)); List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>(); ks.add(new KeySchemaElement(hashKeyName, KeyType.HASH)); ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(1000L, 1000L); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(ks) .withProvisionedThroughput(provisionedthroughput); return ddb.createTable(request); }
private void initializeHashAndRangeTable(String name, String hashName, String rangeName) { String tableName = quartzPrefix + name; if (!tableExists(tableName)) { log.info(String.format("Creating table '%s' with hash and range index.", tableName)); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema( new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(hashName), new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(rangeName)) .withAttributeDefinitions( new AttributeDefinition(hashName, ScalarAttributeType.S), new AttributeDefinition(rangeName, ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); client.createTable(request); waitForTable(tableName); } else { log.info(String.format("Table '%s' already exists.", tableName)); } }
private void initializeHashTable(String name, String hashName) { String tableName = quartzPrefix + name; if (!tableExists(tableName)) { log.info(String.format("Creating table '%s' with hash index.", tableName)); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema( new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(hashName)) .withAttributeDefinitions( new AttributeDefinition(hashName, ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); client.createTable(request); waitForTable(tableName); } else { log.info(String.format("Table '%s' already exists.", tableName)); } }
public static void createSessionTable(AmazonDynamoDBClient dynamo, String tableName, long readCapacityUnits, long writeCapacityUnits) { CreateTableRequest request = new CreateTableRequest().withTableName(tableName); request.withKeySchema(new KeySchemaElement().withAttributeName(DynamoSessionItem.SESSION_ID_ATTRIBUTE_NAME) .withKeyType(KeyType.HASH)); request.withAttributeDefinitions( new AttributeDefinition().withAttributeName(DynamoSessionItem.SESSION_ID_ATTRIBUTE_NAME) .withAttributeType(ScalarAttributeType.S)); request.setProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits)); dynamo.createTable(request); }
private CreateTableResult createTable() { final List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions.add(new AttributeDefinition(RESOURCE_NAME_ATT, ScalarAttributeType.S)); attributeDefinitions.add(new AttributeDefinition(RDF_TRIPLE_ATT, ScalarAttributeType.S)); attributeDefinitions.add(new AttributeDefinition(RDF_PREDICATE_ATT, ScalarAttributeType.S)); attributeDefinitions.add(new AttributeDefinition(RDF_OBJECT_ATT, ScalarAttributeType.S)); final List<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement(RESOURCE_NAME_ATT, KeyType.HASH)); keySchema.add(new KeySchemaElement(RDF_TRIPLE_ATT, KeyType.RANGE)); final ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(10L, 10L); final LocalSecondaryIndex predicateIndex = new LocalSecondaryIndex() .withIndexName(PREDICATE_INDEX_NAME) .withKeySchema(new KeySchemaElement(RESOURCE_NAME_ATT, KeyType.HASH)) .withKeySchema(new KeySchemaElement(RDF_PREDICATE_ATT, KeyType.RANGE)) .withProjection(new Projection().withNonKeyAttributes(RDF_SUBJECT_ATT, RDF_OBJECT_ATT) .withProjectionType(ProjectionType.INCLUDE)); final GlobalSecondaryIndex objectIndex = new GlobalSecondaryIndex() .withIndexName(OBJECT_INDEX_NAME) .withKeySchema(new KeySchemaElement(RDF_OBJECT_ATT, KeyType.HASH)) .withKeySchema(new KeySchemaElement(RDF_PREDICATE_ATT, KeyType.RANGE)) .withProjection(new Projection().withNonKeyAttributes(RDF_SUBJECT_ATT) .withProjectionType(ProjectionType.INCLUDE)) .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L)); final CreateTableRequest request = new CreateTableRequest() .withTableName(TABLE_NAME) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(keySchema) .withProvisionedThroughput(provisionedthroughput) .withLocalSecondaryIndexes(predicateIndex) .withGlobalSecondaryIndexes(objectIndex); return dynamodbClient.createTable(request); }
/** * 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 void createAppsTable(String tableName) { final AttributeDefinition appKey = new AttributeDefinition().withAttributeName(HASH_KEY).withAttributeType( ScalarAttributeType.S); final ArrayList<AttributeDefinition> tableAttributeDefinitions = Lists.newArrayList(appKey); final ArrayList<KeySchemaElement> tableKeySchema = Lists.newArrayList(); tableKeySchema.add( new KeySchemaElement().withAttributeName(HASH_KEY).withKeyType(KeyType.HASH)); final ProvisionedThroughput tableProvisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(10L) .withWriteCapacityUnits(10L); final CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(tableName) .withKeySchema(tableKeySchema) .withAttributeDefinitions(tableAttributeDefinitions) .withProvisionedThroughput(tableProvisionedThroughput); final TableDescription tableDescription = amazonDynamoDB.createTable(createTableRequest).getTableDescription(); logger.info("created_table {}", tableDescription); final DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); final TableDescription description = amazonDynamoDB.describeTable(describeTableRequest).getTable(); logger.info("table_description: " + description); }
private static CreateTableRequest createCreateTableRequest(final String tableName) { final List<KeySchemaElement> keySchema = new ArrayList<>(); final List<AttributeDefinition> tableAttributes = new ArrayList<>(); keySchema.add(new KeySchemaElement(FIELD_NAME_PRIMARY_ID, KeyType.HASH)); tableAttributes.add(new AttributeDefinition(FIELD_NAME_PRIMARY_ID, ScalarAttributeType.S)); return new CreateTableRequest() .withTableName(tableName) .withKeySchema(keySchema) .withAttributeDefinitions(tableAttributes) .withProvisionedThroughput(new ProvisionedThroughput(1L, 1L)); }
private TableDescription getHashRangeTable() { TableDescription description = new TableDescription().withKeySchema(Arrays.asList(new KeySchemaElement().withAttributeName("hashKey"), new KeySchemaElement().withAttributeName ("rangeKey"))).withAttributeDefinitions(Arrays.asList(new AttributeDefinition("hashKey", ScalarAttributeType.S), new AttributeDefinition("rangeKey", ScalarAttributeType.N))); return description; }
private static ScalarAttributeType toScalarAttributeType(AttrType attrType) { switch ( attrType ) { case BIN: return ScalarAttributeType.B; case STR: return ScalarAttributeType.S; case NUM: return ScalarAttributeType.N; } throw new IllegalArgumentException("Unsupported AttrType="+attrType+" MUST be BIN, STR, or NUM!"); }
/** * * @{inheritDoc */ @Override public void createTable(String tableName) { try { if (!hasTable(tableName)) { logger.info("Creating table: " + tableName); HierarchicalConfiguration resultsProviderConfig = config.getVmManagerConfig() .getResultsProviderConfig(); long readCapacity = getCapacity(resultsProviderConfig, "read-capacity", 10L); long writeCapacity = getCapacity(resultsProviderConfig, "write-capacity", 50L); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName( DatabaseKeys.JOB_ID_KEY.getShortKey()).withAttributeType(ScalarAttributeType.S)); attributeDefinitions.add(new AttributeDefinition().withAttributeName( DatabaseKeys.REQUEST_NAME_KEY.getShortKey()).withAttributeType(ScalarAttributeType.S)); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits( readCapacity).withWriteCapacityUnits(writeCapacity); KeySchemaElement hashKeyElement = new KeySchemaElement().withAttributeName( DatabaseKeys.JOB_ID_KEY.getShortKey()).withKeyType(KeyType.HASH); KeySchemaElement rangeKeyElement = new KeySchemaElement().withAttributeName( DatabaseKeys.REQUEST_NAME_KEY.getShortKey()).withKeyType(KeyType.RANGE); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(hashKeyElement, rangeKeyElement) .withAttributeDefinitions(attributeDefinitions) .withProvisionedThroughput(provisionedThroughput); CreateTableResult result = dynamoDb.createTable(request); waitForStatus(tableName, TableStatus.ACTIVE); logger.info("Created table: " + result.getTableDescription().getTableName()); } } catch (Exception t) { logger.error(t, t); throw new RuntimeException(t); } }
@Override public CreateTableRequest getTableSchema() { return super.getTableSchema() .withAttributeDefinitions( new AttributeDefinition() .withAttributeName(Constants.JANUSGRAPH_HASH_KEY) .withAttributeType(ScalarAttributeType.S)) .withKeySchema( new KeySchemaElement() .withAttributeName(Constants.JANUSGRAPH_HASH_KEY) .withKeyType(KeyType.HASH)); }
@Test public void waiterMethodsShouldWorkWithWrapper() throws InterruptedException { DynamoDBClientWithStubbedWaiter subject = new DynamoDBClientWithStubbedWaiter( dynamoDbRule.getClient()); DynamoDB db = new DynamoDB(subject); AttributeDefinition id = new AttributeDefinition("id", ScalarAttributeType.S); List<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add( new KeySchemaElement() .withAttributeName(id.getAttributeName()) .withKeyType(KeyType.HASH) ); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(); provisionedThroughput.setReadCapacityUnits(10L); provisionedThroughput.setWriteCapacityUnits(10L); CreateTableRequest request = new CreateTableRequest() .withTableName("test_table") .withKeySchema(keySchema) .withAttributeDefinitions(singleton(id)) .withProvisionedThroughput(provisionedThroughput); Table result = db.createTable(request); TableDescription description = result.waitForActive(); assertThat(description.getTableName(), is("test_table")); }
public static void main(String[] args) { dynamodb.createTable(new CreateTableRequest() .withTableName(PLAYER_STATS_TABLE_NAME) .withKeySchema(new KeySchemaElement(PLAYER_NAME, KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition(PLAYER_NAME, ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput(15L, 15L)) ); System.out.println("Table created: " + PLAYER_STATS_TABLE_NAME); }
public static void main(String[] args) { dynamodb.createTable(new CreateTableRequest() .withTableName(SCORE_TABLE_NAME) .withKeySchema(new KeySchemaElement(SCORE_ID, KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition(SCORE_ID, ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput(20L, 20L)) .withStreamSpecification(new StreamSpecification() .withStreamEnabled(true) .withStreamViewType(StreamViewType.NEW_IMAGE)) ); }
public static void main(String[] args) { dynamodb.createTable(new CreateTableRequest() .withTableName(HIGH_SCORES_BY_DATE_TABLE_NAME) .withKeySchema(new KeySchemaElement(PLAYER_NAME, KeyType.HASH), new KeySchemaElement(DATE, KeyType.RANGE)) .withAttributeDefinitions( new AttributeDefinition(PLAYER_NAME, ScalarAttributeType.S), new AttributeDefinition(DATE, ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput(15L, 15L)) ); System.out.println("Table created: " + HIGH_SCORES_BY_DATE_TABLE_NAME); }
public static void main(String[] args) { dynamodb.createTable(new CreateTableRequest() .withTableName(FUNCTION_TRACKER_TABLE_NAME) .withKeySchema(new KeySchemaElement(SEGMENT, KeyType.HASH)) .withAttributeDefinitions( new AttributeDefinition(SEGMENT, ScalarAttributeType.N)) .withProvisionedThroughput(new ProvisionedThroughput(15L, 15L)) ); System.out.println("Table created: " + FUNCTION_TRACKER_TABLE_NAME); }
@Override public boolean createTable(String tableName) { List<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName(AttributeKey.UUID) .withAttributeType(ScalarAttributeType.S)); List<KeySchemaElement> keySchemaElement = new ArrayList<KeySchemaElement>(); keySchemaElement.add(new KeySchemaElement().withAttributeName(AttributeKey.UUID) .withKeyType(KeyType.HASH)); // Provide the initial provisioned throughput values as Java long data types ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(10L) .withWriteCapacityUnits(10L); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(tableName) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(keySchemaElement) .withProvisionedThroughput(provisionedThroughput); try { CreateTableResult createdTableDescription = dynamoDBClient.createTable(createTableRequest); LOG.info("Creating table description: " + createdTableDescription); // Wait for it to become active waitForTableAvailable(tableName); if (describeTable(tableName) != null) { return true; } } catch (ResourceInUseException rie) { LOG.warn("Table " + tableName + " already exists"); } catch (AmazonServiceException ase) { LOG.error(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.error(ace.getMessage(), ace); } return false; }
public static ScalarAttributeType getAttributeType(final Class<?> propertyClass) { if (propertyClass == ByteBuffer.class || propertyClass == byte[].class) { return ScalarAttributeType.B; } else if (char.class.isAssignableFrom(propertyClass)) { return ScalarAttributeType.S; } else if (propertyClass.isPrimitive() || Number.class.isAssignableFrom(propertyClass) || Boolean.class.isAssignableFrom(propertyClass)) { return ScalarAttributeType.N; } else { return ScalarAttributeType.S; } }
/** * Creates a DynamoDB Table with the correct properties to be used with a ProviderStore. */ public static CreateTableResult createTable(final AmazonDynamoDB ddb, final String tableName, final ProvisionedThroughput provisionedThroughput) { return ddb.createTable(Arrays.asList(new AttributeDefinition(DEFAULT_HASH_KEY, ScalarAttributeType.S), new AttributeDefinition(DEFAULT_RANGE_KEY, ScalarAttributeType.N)), tableName, Arrays.asList(new KeySchemaElement( DEFAULT_HASH_KEY, KeyType.HASH), new KeySchemaElement(DEFAULT_RANGE_KEY, KeyType.RANGE)), provisionedThroughput); }
@Before public void setUp() { client = DynamoDBEmbedded.create(); ArrayList<AttributeDefinition> attrDef = new ArrayList<AttributeDefinition>(); attrDef.add(new AttributeDefinition().withAttributeName("hashKey").withAttributeType(ScalarAttributeType.N)); attrDef.add(new AttributeDefinition().withAttributeName("rangeKey").withAttributeType(ScalarAttributeType.N)); ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName("hashKey").withKeyType(KeyType.HASH)); keySchema.add(new KeySchemaElement().withAttributeName("rangeKey").withKeyType(KeyType.RANGE)); client.createTable(new CreateTableRequest().withTableName("TableName") .withAttributeDefinitions(attrDef) .withKeySchema(keySchema) .withProvisionedThroughput(new ProvisionedThroughput(100L, 100L))); attrDef = new ArrayList<AttributeDefinition>(); attrDef.add(new AttributeDefinition().withAttributeName("hashKey").withAttributeType(ScalarAttributeType.S)); keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName("hashKey").withKeyType(KeyType.HASH)); client.createTable(new CreateTableRequest().withTableName("HashKeyOnly") .withAttributeDefinitions(attrDef) .withKeySchema(keySchema) .withProvisionedThroughput(new ProvisionedThroughput(100L, 100L))); attrDef = new ArrayList<AttributeDefinition>(); attrDef.add(new AttributeDefinition().withAttributeName("hashKey").withAttributeType(ScalarAttributeType.B)); attrDef.add(new AttributeDefinition().withAttributeName("rangeKey").withAttributeType(ScalarAttributeType.N)); keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName("hashKey").withKeyType(KeyType.HASH)); keySchema.add(new KeySchemaElement().withAttributeName("rangeKey").withKeyType(KeyType.RANGE)); client.createTable(new CreateTableRequest().withTableName("DeterministicTable") .withAttributeDefinitions(attrDef) .withKeySchema(keySchema) .withProvisionedThroughput(new ProvisionedThroughput(100L, 100L))); }
/** * Creates a table in AWS DynamoDB which will be shared between apps. * @param readCapacity read capacity * @param writeCapacity write capacity * @return true if created */ public static boolean createSharedTable(long readCapacity, long writeCapacity) { if (StringUtils.isBlank(SHARED_TABLE) || StringUtils.containsWhitespace(SHARED_TABLE) || existsTable(SHARED_TABLE)) { return false; } try { GlobalSecondaryIndex secIndex = new GlobalSecondaryIndex(). withIndexName(getSharedIndexName()). withProvisionedThroughput(new ProvisionedThroughput(). withReadCapacityUnits(1L). withWriteCapacityUnits(1L)). withProjection(new Projection().withProjectionType(ProjectionType.ALL)). withKeySchema(new KeySchemaElement().withAttributeName(Config._APPID).withKeyType(KeyType.HASH), new KeySchemaElement().withAttributeName(Config._ID).withKeyType(KeyType.RANGE)); getClient().createTable(new CreateTableRequest().withTableName(getTableNameForAppid(SHARED_TABLE)). withKeySchema(new KeySchemaElement(Config._KEY, KeyType.HASH)). withAttributeDefinitions(new AttributeDefinition(Config._KEY, ScalarAttributeType.S), new AttributeDefinition(Config._APPID, ScalarAttributeType.S), new AttributeDefinition(Config._ID, ScalarAttributeType.S)). withGlobalSecondaryIndexes(secIndex). withProvisionedThroughput(new ProvisionedThroughput(readCapacity, writeCapacity))); } catch (Exception e) { logger.error(null, e); return false; } return true; }
@Override public void createNamespace() { String table = getTenant().getName(); if(Tables.doesTableExist(m_client, table)) return; m_logger.info("Creating table: {}", table); CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(table) .withKeySchema( new KeySchemaElement() .withAttributeName("key") .withKeyType(KeyType.HASH), new KeySchemaElement() .withAttributeName("column") .withKeyType(KeyType.RANGE)) .withAttributeDefinitions( new AttributeDefinition() .withAttributeName("key") .withAttributeType(ScalarAttributeType.S), new AttributeDefinition() .withAttributeName("column") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(m_readCapacityUnits) .withWriteCapacityUnits(m_writeCapacityUnits)); m_client.createTable(createTableRequest); try { Tables.awaitTableToBecomeActive(m_client, table); } catch (InterruptedException e) { throw new RuntimeException(e); } }
public static void createHashTable(AmazonDynamoDBClient client, String tableName, String hashColumnName) { CreateTableRequest accessTableRequest = new CreateTableRequest() // .withTableName(tableName) // .withKeySchema(new KeySchemaElement(hashColumnName, KeyType.HASH)) // .withAttributeDefinitions(new AttributeDefinition(hashColumnName, ScalarAttributeType.S) // ) // .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) // ; CreateTableResult accessTableresponse = client.createTable(accessTableRequest); }
@Test public void testCreateTableTableAlreadyExistsCorrectKeySchema() { final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH)); final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); final DescribeTableResult result = new DescribeTableResult().withTable(description); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result); final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); PowerMock.replayAll(); assertEquals(description, DynamoDBManager.createTable(dynamoDB, request)); PowerMock.verifyAll(); }
@Test(expected = IllegalStateException.class) public void testCreateTableTableAlreadyExistsIncorrectKeySchema() { final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH)); final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); final DescribeTableResult result = new DescribeTableResult().withTable(description); EasyMock.expect(dynamoDB.describeTable(tableName)).andReturn(result); final Collection<AttributeDefinition> ads2 = Arrays.asList(new AttributeDefinition("Hash2", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses2 = Arrays.asList(new KeySchemaElement("Hash2", KeyType.HASH)); final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads2).withKeySchema(kses2) .withTableName(tableName); PowerMock.replayAll(); DynamoDBManager.createTable(dynamoDB, request); }
@Test public void testCreateTableTableDoesNotExist() { final Collection<AttributeDefinition> ads = Arrays.asList(new AttributeDefinition("Hash", ScalarAttributeType.S)); final Collection<KeySchemaElement> kses = Arrays.asList(new KeySchemaElement("Hash", KeyType.HASH)); final TableDescription description = new TableDescription().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); final CreateTableResult cTR = new CreateTableResult().withTableDescription(description); EasyMock.expect(dynamoDB.describeTable(tableName)).andThrow(new ResourceNotFoundException(null)); final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(ads).withKeySchema(kses) .withTableName(tableName); EasyMock.expect(dynamoDB.createTable(request)).andReturn(cTR); PowerMock.replayAll(); assertEquals(description, DynamoDBManager.createTable(dynamoDB, request)); PowerMock.verifyAll(); }
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(); }
/** * Get region with a table. * @param table Table name * @return Region * @throws Exception If fails */ public Region get(final String table) throws Exception { final Region region = new Region.Simple( new Credentials.Direct(Credentials.TEST, this.prt) ); final MadeTable mocker = new MadeTable( region, new CreateTableRequest() .withTableName(table) .withProvisionedThroughput( new ProvisionedThroughput() .withReadCapacityUnits(1L) .withWriteCapacityUnits(1L) ) .withAttributeDefinitions( new AttributeDefinition() .withAttributeName(this.ahash) .withAttributeType(ScalarAttributeType.S), new AttributeDefinition() .withAttributeName(this.arange) .withAttributeType(ScalarAttributeType.N) ) .withKeySchema( new KeySchemaElement() .withAttributeName(this.ahash) .withKeyType(KeyType.HASH), new KeySchemaElement() .withAttributeName(this.arange) .withKeyType(KeyType.RANGE) ) ); mocker.create(); mocker.createIfAbsent(); return new ReRegion(region); }