public void createTable(Class<? extends IDomain> domain){ CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(domain); tableRequest = tableRequest.withProvisionedThroughput(new ProvisionedThroughput(5L,5L)); //check whether or not we need to add a provisioning throughput value for GSI for (Method method : domain.getMethods()) { if(method.isAnnotationPresent(DynamoDBIndexHashKey.class)){ String tempGSI = method.getAnnotation(DynamoDBIndexHashKey.class).globalSecondaryIndexName(); for (GlobalSecondaryIndex globalSecondaryIndex : tableRequest.getGlobalSecondaryIndexes()) { if(globalSecondaryIndex.getIndexName().equals(tempGSI)){ globalSecondaryIndex.setProvisionedThroughput(new ProvisionedThroughput(5L,5L)); } } } } amazonDynamoDBClient.createTable(tableRequest); }
/*** * Create The list of Global Secondary Indexes if they exist * @param globalSecondaryIndexes * @return globalSecondaryIndexList */ private List<GlobalSecondaryIndex> createGlobalSecondaryIndexes(final List<io.microgenie.aws.config.DynamoDbConfig.GlobalSecondaryIndex> globalSecondaryIndexes) { List<GlobalSecondaryIndex> indexes = null; if(globalSecondaryIndexes!=null){ indexes = new ArrayList<GlobalSecondaryIndex>(); for(DynamoDbConfig.GlobalSecondaryIndex configGlobalIndex : globalSecondaryIndexes){ final GlobalSecondaryIndex gi = new GlobalSecondaryIndex(); gi.withIndexName(configGlobalIndex.getName()); gi.withProjection(this.createProjection(configGlobalIndex.getProjection())); gi.withProvisionedThroughput(this.createProvisionedThroughput(configGlobalIndex.getReadCapacityUnits(), configGlobalIndex.getWriteCapacityUnits())); gi.withKeySchema(this.createKeySchemaElements(configGlobalIndex.getKeys())); indexes.add(gi); } } return indexes; }
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); }
/** * 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; }
/*** * Create the Table Request * @param table * @return createTableRequest */ private CreateTableRequest createTableRequest(final Table table) { final ProvisionedThroughput throughput = this.createProvisionedThroughput(table.getReadCapacityUnits(), table.getWriteCapacityUnits()); final List<KeySchemaElement> keys = this.createKeySchemaElements(table.getKeys()); final CreateTableRequest tableRequest = new CreateTableRequest(table.getName(), keys) .withProvisionedThroughput(throughput); /*** * Set Indexes */ final List<LocalSecondaryIndex> localSecondaryIndexes = this.createLocalSecondaryIndexes(table.getLocalSecondaryIndexes()); final List<GlobalSecondaryIndex> globalSecondaryIndexes = this.createGlobalSecondaryIndexes(table.getGlobalSecondaryIndexes()); /** Local Secondary Indexes **/ if(localSecondaryIndexes!=null){ tableRequest.withLocalSecondaryIndexes(localSecondaryIndexes); } /** Global Secondary Indexes **/ if(globalSecondaryIndexes!=null){ tableRequest.withGlobalSecondaryIndexes(globalSecondaryIndexes); } /** Set Attribute Definitions **/ final List<AttributeDefinition> attributeDefinitions = this.createAttributeDefinitions(table.getAttributeDefinitions()); tableRequest.withAttributeDefinitions(attributeDefinitions); return tableRequest; }
/** * Creates the table that stores images. * * @param dynamoDB * {@link AmazonDynamoDB} used to create the image table * @param tableName * name of the table to create * @param tableProvisionedThroughput * initial provisioned throughput for the table * @param timeGSIProvisionedThroughput * initial provisioned throughput for the time-based global secondary index * @param voteGSIProvisionedThroughput * initial provisioned throughput for the vote-based global secondary index * @see <a href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html">Global Secondary * Indexes</a> ======= initial provisioned throughput for the time-based global secondary index * @see <a * href="http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/ProvisionedThroughputIntro.html">Provisioned * Throughput in Amazon DynamoDB</a> */ public static void createImageTable(final AmazonDynamoDB dynamoDB, final String tableName, final ProvisionedThroughput tableProvisionedThroughput, final ProvisionedThroughput timeGSIProvisionedThroughput, final ProvisionedThroughput voteGSIProvisionedThroughput) { // Set up time GSI final GlobalSecondaryIndex timeGSI = new GlobalSecondaryIndex(); timeGSI.setIndexName(IMAGE_TABLE_TIME_GSI_NAME); timeGSI.setKeySchema(Arrays.asList(IMAGE_TABLE_TIME_GSI_HASH_KSE, IMAGE_TABLE_TIME_GSI_RANGE_KSE)); timeGSI.setProjection(IMAGE_TABLE_TIME_GSI_PROJECTION); timeGSI.setProvisionedThroughput(timeGSIProvisionedThroughput); // Set up vote GSI final GlobalSecondaryIndex voteGSI = new GlobalSecondaryIndex(); voteGSI.setIndexName(IMAGE_TABLE_VOTE_GSI_NAME); voteGSI.setKeySchema(Arrays.asList(IMAGE_TABLE_VOTE_GSI_HASH_KSE, IMAGE_TABLE_VOTE_GSI_RANGE_KSE)); voteGSI.setProjection(IMAGE_TABLE_VOTE_GSI_PROJECTION); voteGSI.setProvisionedThroughput(voteGSIProvisionedThroughput); // Create table final CreateTableRequest request = new CreateTableRequest(); request.setAttributeDefinitions(IMAGE_TABLE_ATTRIBUTE_DEFINITIONS); request.setKeySchema(IMAGE_TABLE_KEY_SCHEMA); request.setGlobalSecondaryIndexes(Arrays.asList(timeGSI, voteGSI)); request.setProvisionedThroughput(tableProvisionedThroughput); request.setTableName(tableName); LOGGER.info("Creating image table: " + request); final TableDescription result = DynamoDBManager.createTable(dynamoDB, request); LOGGER.info("Image table successfully created: " + result); }
@Test public void testCreateImageTable() { final AmazonDynamoDB dynamoDB = PowerMock.createMock(AmazonDynamoDB.class); PowerMock.mockStatic(DynamoDBManager.class); final CreateTableRequest request = new CreateTableRequest(); request.setAttributeDefinitions(MarsDynamoDBManager.IMAGE_TABLE_ATTRIBUTE_DEFINITIONS); request.setKeySchema(MarsDynamoDBManager.IMAGE_TABLE_KEY_SCHEMA); final GlobalSecondaryIndex timeGSI = new GlobalSecondaryIndex(); timeGSI.setIndexName(MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_NAME); timeGSI.setKeySchema(Arrays.asList(MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_HASH_KSE, MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_RANGE_KSE)); timeGSI.setProjection(MarsDynamoDBManager.IMAGE_TABLE_TIME_GSI_PROJECTION); timeGSI.setProvisionedThroughput(PROVISIONED_THROUGHPUT); final GlobalSecondaryIndex voteGSI = new GlobalSecondaryIndex(); voteGSI.setIndexName(MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_NAME); voteGSI.setKeySchema(Arrays.asList(MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_HASH_KSE, MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_RANGE_KSE)); voteGSI.setProjection(MarsDynamoDBManager.IMAGE_TABLE_VOTE_GSI_PROJECTION); voteGSI.setProvisionedThroughput(PROVISIONED_THROUGHPUT); request.setGlobalSecondaryIndexes(Arrays.asList(timeGSI, voteGSI)); request.setProvisionedThroughput(PROVISIONED_THROUGHPUT); request.setTableName(TABLE_NAME); DynamoDBManager.createTable(dynamoDB, request); PowerMock.expectLastCall().andReturn(null); PowerMock.replayAll(); MarsDynamoDBManager.createImageTable(dynamoDB, TABLE_NAME, PROVISIONED_THROUGHPUT, PROVISIONED_THROUGHPUT, PROVISIONED_THROUGHPUT); PowerMock.verifyAll(); }
private void createTable() { try { final ArrayList<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement() .withAttributeName("id") .withKeyType(KeyType.HASH)); final ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions.add(new AttributeDefinition() .withAttributeName("id") .withAttributeType("S")); final ArrayList<GlobalSecondaryIndex> globalSecondaryIndices = new ArrayList<>(); if (secondaryIndexNames != null) { for (final String indexName : secondaryIndexNames) { attributeDefinitions.add(new AttributeDefinition() .withAttributeName(indexName) .withAttributeType("S")); globalSecondaryIndices.add(new GlobalSecondaryIndex() .withIndexName(indexName + "-index") .withKeySchema(new KeySchemaElement().withAttributeName(indexName).withKeyType(KeyType.HASH)) .withProjection(new Projection().withProjectionType(ProjectionType.ALL)) .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits))); } } final CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(keySchema) .withProvisionedThroughput(new ProvisionedThroughput() .withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits)); if (!globalSecondaryIndices.isEmpty()) { request.withGlobalSecondaryIndexes(globalSecondaryIndices); } request.setAttributeDefinitions(attributeDefinitions); logger.debug("Issuing CreateTable request for " + tableName); final Table table = DynamoCommons.getInstance().getDb().createTable(request); logger.debug("Waiting for table to be created..."); table.waitForActive(); } catch (final Exception e) { logger.error("CreateTable request failed: " + e.getMessage()); } }
@Override protected void createTable(TableDescription table) { List<GlobalSecondaryIndex> gsis = new ArrayList<>(); List<LocalSecondaryIndex> lsis = new ArrayList<>(); ProvisionedThroughput mainThroughtput = null; List<KeySchemaElement> mainKeySchema = null; Map<String, AttrType> attrTypes = new HashMap<>(); for ( IndexDescription index : table.getIndexes() ) { addAttrType(table.getTableName(), index.getIndexName(), attrTypes, index.getHashKey()); addAttrType(table.getTableName(), index.getIndexName(), attrTypes, index.getRangeKey()); ProvisionedThroughput throughput = new ProvisionedThroughput() .withReadCapacityUnits(index.getReadCapacity()) .withWriteCapacityUnits(index.getWriteCapacity()); switch ( index.getIndexType() ) { case MAIN_INDEX: mainThroughtput = throughput; mainKeySchema = toKeySchema(table.getTableName(), index); break; case LOCAL_SECONDARY_INDEX: lsis.add( new LocalSecondaryIndex() .withProjection( new Projection().withProjectionType(ProjectionType.ALL)) .withIndexName(index.getIndexName()) .withKeySchema(toKeySchema(table.getTableName(), index))); break; case GLOBAL_SECONDARY_INDEX: gsis.add( new GlobalSecondaryIndex() .withIndexName(index.getIndexName()) .withKeySchema(toKeySchema(table.getTableName(), index)) .withProjection( new Projection().withProjectionType(ProjectionType.ALL)) .withProvisionedThroughput(throughput)); break; default: throw new UnsupportedOperationException( "Unsupported indexType="+index.getIndexType()+" for table name "+ table.getTableName()+" index="+index.getIndexName()); } } String tableName = getTableName(table.getTableName()); for ( int retry=0;; retry++ ) { try { _dynamodb.createTable( new CreateTableRequest() .withKeySchema(mainKeySchema) .withProvisionedThroughput(mainThroughtput) .withAttributeDefinitions(toAttributeDefinitions(attrTypes)) .withLocalSecondaryIndexes(lsis.size() == 0 ? null : lsis) .withGlobalSecondaryIndexes(gsis.size() == 0 ? null : gsis) .withTableName(tableName)); break; } catch ( LimitExceededException ex ) { long secs = (retry < 6) ? (long)Math.pow(2, retry) : 60L; LOG.info("Waiting {} seconds to create {} due to: {}", secs, tableName, ex.getMessage()); try { Thread.sleep(1000*secs); } catch ( InterruptedException interruptedEx ) { Thread.currentThread().interrupt(); return; } } } }
/** * Creates a table in Dynamo DB with the requested read and write capacity, * attributes, key schema and GSI's. This method will block until the table * is Active in Dynamo DB. * * @param dynamoClient * Dynamo DB Client to use for connection to Dynamo DB. * @param dynamoTable * The table name to create in Dynamo DB. * @param readCapacity * The requested amount of read IOPS to be provisioned. * @param writeCapacity * The requested amount of write IOPS to be provisioned. * @param attributes * Attribute Names which must be indicated to create the key * schema and/or GSI's. * @param keySchema * The keys used for the primary key of the table. * @param gsi * List of Global Secondary Indexes to be created on the table * @throws Exception */ public static void initTable(final AmazonDynamoDB dynamoClient, final String dynamoTable, final long readCapacity, final long writeCapacity, List<AttributeDefinition> attributes, List<KeySchemaElement> keySchema, final Collection<GlobalSecondaryIndex> gsi) throws Exception { try { DescribeTableResult res = safeDescribeTable(dynamoClient, dynamoTable); if (!res.getTable().getTableStatus().equals("ACTIVE")) { waitForTableActive(dynamoClient, dynamoTable); } } catch (ResourceInUseException r) { waitForTableActive(dynamoClient, dynamoTable); } catch (ResourceNotFoundException e) { LOG.info(String .format("Table %s Not Found - Creating with %s Reads/sec & %s Writes/sec", dynamoTable, readCapacity, writeCapacity)); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(dynamoTable) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits( readCapacity).withWriteCapacityUnits( writeCapacity)).withKeySchema(keySchema) .withAttributeDefinitions(attributes); if (gsi != null) createTableRequest.withGlobalSecondaryIndexes(gsi); // create the table try { safeCreateTable(dynamoClient, createTableRequest); } catch (Exception ex) { LOG.error(ex); throw e; } // wait for it to go to active state waitForTableActive(dynamoClient, dynamoTable); } }
public void initAggTable(final String keyColumn, final String dateColumnName, final long readCapacity, final long writeCapacity) throws Exception { final String setDateColumn = dateColumnName == null ? StreamAggregator.DEFAULT_DATE_VALUE : dateColumnName; long setReadCapacity = readCapacity == -1 ? DEFAULT_READ_CAPACITY : readCapacity; long setWriteCapacity = writeCapacity == -1 ? DEFAULT_WRITE_CAPACITY : writeCapacity; // we have to add this attribute list so that we can project the key // into the GSI List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>() { { add(new AttributeDefinition().withAttributeName(keyColumn).withAttributeType("S")); add(new AttributeDefinition().withAttributeName(setDateColumn).withAttributeType( "S")); } }; Collection<GlobalSecondaryIndex> gsi = new ArrayList<>(); // Global Secondary Index for accessing the table by date item gsi.add(new GlobalSecondaryIndex().withIndexName( StreamAggregatorUtils.getDateDimensionIndexName(tableName, setDateColumn)).withKeySchema( new KeySchemaElement().withAttributeName(SCATTER_PREFIX_ATTRIBUTE).withKeyType( KeyType.HASH), new KeySchemaElement().withAttributeName(setDateColumn).withKeyType(KeyType.RANGE)).withProjection( new Projection().withProjectionType(ProjectionType.KEYS_ONLY)).withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(setReadCapacity).withWriteCapacityUnits( setWriteCapacity))); attributes.add(new AttributeDefinition().withAttributeName(SCATTER_PREFIX_ATTRIBUTE).withAttributeType( "N")); // table is hash/range on value and date List<KeySchemaElement> key = new ArrayList<KeySchemaElement>() { { add(new KeySchemaElement().withAttributeName(keyColumn).withKeyType(KeyType.HASH)); add(new KeySchemaElement().withAttributeName(setDateColumn).withKeyType( KeyType.RANGE)); } }; // initialise the table DynamoUtils.initTable(this.dynamoClient, this.tableName, setReadCapacity, setWriteCapacity, attributes, key, gsi); }
public static void createTokenTables(AmazonDynamoDBClient client, DynamoDBTokenSchema schema) { GlobalSecondaryIndex gsiAuthenticationIdToken = new GlobalSecondaryIndex() // .withIndexName(schema.getAccessIndexAuthenticationId()) // .withKeySchema(new KeySchemaElement(schema.getAccessColumnAuthenticationId(), KeyType.HASH)) // .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) // .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY)); GlobalSecondaryIndex gsiRefreshToken = new GlobalSecondaryIndex() // .withIndexName(schema.getAccessIndexRefreshToken()) // .withKeySchema(new KeySchemaElement(schema.getAccessColumnRefreshToken(), KeyType.HASH)) // .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) // .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY)); GlobalSecondaryIndex gsiClientIdAndUserName = new GlobalSecondaryIndex() // .withIndexName(schema.getAccessIndexClientIdAndUserName()) // .withKeySchema( // new KeySchemaElement(schema.getAccessColumnClientId(), KeyType.HASH), // new KeySchemaElement(schema.getAccessColumnUserName(), KeyType.RANGE) // ) // .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) // .withProjection(new Projection().withProjectionType(ProjectionType.KEYS_ONLY)); CreateTableRequest accessTableRequest = new CreateTableRequest() // .withTableName(schema.getAccessTableName()) // .withKeySchema(new KeySchemaElement(schema.getAccessColumnTokenId(), KeyType.HASH)) // .withGlobalSecondaryIndexes(gsiAuthenticationIdToken, gsiRefreshToken, gsiClientIdAndUserName) // .withAttributeDefinitions(new AttributeDefinition(schema.getAccessColumnTokenId(), ScalarAttributeType.S), // new AttributeDefinition(schema.getAccessColumnAuthenticationId(), ScalarAttributeType.S), // new AttributeDefinition(schema.getAccessColumnRefreshToken(), ScalarAttributeType.S), // new AttributeDefinition(schema.getAccessColumnClientId(), ScalarAttributeType.S), // new AttributeDefinition(schema.getAccessColumnUserName(), ScalarAttributeType.S) // ) // .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) // ; CreateTableResult accessTableresponse = client.createTable(accessTableRequest); CreateTableRequest refreshTableRequest = new CreateTableRequest() // .withTableName(schema.getRefreshTableName()) // .withKeySchema(new KeySchemaElement(schema.getRefreshColumnTokenId(), KeyType.HASH)) // .withAttributeDefinitions(new AttributeDefinition(schema.getRefreshColumnTokenId(), ScalarAttributeType.S) // ) // .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT) // ; CreateTableResult refreshTableresponse = client.createTable(refreshTableRequest); }
public static void createTable() { // Attribute definitions ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition() .withAttributeName("IssueId").withAttributeType("S")); attributeDefinitions.add(new AttributeDefinition() .withAttributeName("Title").withAttributeType("S")); attributeDefinitions.add(new AttributeDefinition() .withAttributeName("CreateDate").withAttributeType("S")); attributeDefinitions.add(new AttributeDefinition() .withAttributeName("DueDate").withAttributeType("S")); // Key schema for table ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>(); tableKeySchema.add(new KeySchemaElement() .withAttributeName("IssueId").withKeyType(KeyType.HASH)); tableKeySchema.add(new KeySchemaElement() .withAttributeName("Title").withKeyType(KeyType.RANGE)); // Initial provisioned throughput settings for the indexes ProvisionedThroughput ptIndex = new ProvisionedThroughput() .withReadCapacityUnits(1L).withWriteCapacityUnits(1L); // CreateDateIndex GlobalSecondaryIndex createDateIndex = new GlobalSecondaryIndex() .withIndexName("CreateDateIndex") .withProvisionedThroughput(ptIndex) .withKeySchema( new KeySchemaElement() .withAttributeName("CreateDate").withKeyType(KeyType.HASH), new KeySchemaElement() .withAttributeName("IssueId") .withKeyType(KeyType.RANGE)) .withProjection(new Projection() .withProjectionType("INCLUDE") .withNonKeyAttributes("Description", "Status")); // TitleIndex GlobalSecondaryIndex titleIndex = new GlobalSecondaryIndex() .withIndexName("TitleIndex") .withProvisionedThroughput(ptIndex) .withKeySchema( new KeySchemaElement() .withAttributeName("Title") .withKeyType(KeyType.HASH), new KeySchemaElement() .withAttributeName("IssueId") .withKeyType(KeyType.RANGE)) .withProjection(new Projection() .withProjectionType("KEYS_ONLY")); // DueDateIndex GlobalSecondaryIndex dueDateIndex = new GlobalSecondaryIndex() .withIndexName("DueDateIndex") .withProvisionedThroughput(ptIndex) .withKeySchema( new KeySchemaElement() .withAttributeName("DueDate") .withKeyType(KeyType.HASH)) .withProjection(new Projection() .withProjectionType("ALL")); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(tableName) .withProvisionedThroughput( new ProvisionedThroughput() .withReadCapacityUnits( (long) 1) .withWriteCapacityUnits( (long) 1)) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(tableKeySchema) .withGlobalSecondaryIndexes(createDateIndex, titleIndex, dueDateIndex); System.out.println("Creating table " + tableName + "..."); System.out.println(client.createTable(createTableRequest)); waitForTableToBecomeAvailable(tableName); }
@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(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; }
private void createTables(long readCapacity, long writeCapacity) throws Exception { // ID | createdAt | lat | long | screen name | text | // Primary index is by ID // Global Secondary index is by screen name + createdAt try { CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(TABLE_NAME) .withKeySchema( new KeySchemaElement().withAttributeName(COL_ID) .withKeyType(KeyType.HASH)) .withAttributeDefinitions( new AttributeDefinition().withAttributeName(COL_ID) .withAttributeType(ScalarAttributeType.N), new AttributeDefinition().withAttributeName( COL_CREATEDAT).withAttributeType( ScalarAttributeType.N), new AttributeDefinition().withAttributeName( COL_SCREENNAME).withAttributeType( ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits( readCapacity).withWriteCapacityUnits( writeCapacity)) .withGlobalSecondaryIndexes( new GlobalSecondaryIndex() .withIndexName(INDEX_SCREENNAME) .withProvisionedThroughput( new ProvisionedThroughput() .withReadCapacityUnits( (long) 10) .withWriteCapacityUnits( (long) 1)) .withProjection( new Projection() .withProjectionType("ALL")) .withKeySchema( new KeySchemaElement() .withAttributeName( COL_SCREENNAME) .withKeyType(KeyType.HASH), new KeySchemaElement() .withAttributeName( COL_CREATEDAT) .withKeyType(KeyType.RANGE))); TableDescription createdTableDescription = dynamoDB.createTable( createTableRequest).getTableDescription(); LOG.info("Created Table: " + createdTableDescription); } catch (Exception e) { handleException(e); } }