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 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; }
/*** * Create Local Secondary Indexes * @param localSecondaryIndexes * @return localIndexList */ private List<LocalSecondaryIndex> createLocalSecondaryIndexes(List<io.microgenie.aws.config.DynamoDbConfig.LocalSecondaryIndex> localSecondaryIndexes) { List<LocalSecondaryIndex> indexes = null; if(localSecondaryIndexes!=null){ indexes = new ArrayList<LocalSecondaryIndex>(); for(DynamoDbConfig.LocalSecondaryIndex configIndex : localSecondaryIndexes){ LocalSecondaryIndex index = new LocalSecondaryIndex() .withIndexName(configIndex.getName()) .withProjection(this.createProjection(configIndex.getProjection())) .withKeySchema(this.createKeySchemaElements(configIndex.getKeys())); indexes.add(index); } } return indexes; }
/** * <p> * Construct a create table request object based on GeoDataManagerConfiguration. The users can update any aspect of * the request and call it. * </p> * Example: * * <pre> * AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider()); * Region usWest2 = Region.getRegion(Regions.US_WEST_2); * ddb.setRegion(usWest2); * * CreateTableRequest createTableRequest = GeoTableUtil.getCreateTableRequest(config); * CreateTableResult createTableResult = ddb.createTable(createTableRequest); * </pre> * * @return Generated create table request. */ public static CreateTableRequest getCreateTableRequest(GeoDataManagerConfiguration config) { CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(config.getTableName()) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(5L)) .withKeySchema( new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName( config.getHashKeyAttributeName()), new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName( config.getRangeKeyAttributeName())) .withAttributeDefinitions( new AttributeDefinition().withAttributeType(ScalarAttributeType.N).withAttributeName( config.getHashKeyAttributeName()), new AttributeDefinition().withAttributeType(ScalarAttributeType.S).withAttributeName( config.getRangeKeyAttributeName()), new AttributeDefinition().withAttributeType(ScalarAttributeType.N).withAttributeName( config.getGeohashAttributeName())) .withLocalSecondaryIndexes( new LocalSecondaryIndex() .withIndexName(config.getGeohashIndexName()) .withKeySchema( new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName( config.getHashKeyAttributeName()), new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName( config.getGeohashAttributeName())) .withProjection(new Projection().withProjectionType(ProjectionType.ALL))); return createTableRequest; }
public void createTable() { final AttributeDefinition nsKey = new AttributeDefinition().withAttributeName(HASH_KEY).withAttributeType( ScalarAttributeType.S); final AttributeDefinition featureKey = new AttributeDefinition().withAttributeName(RANGE_KEY) .withAttributeType(ScalarAttributeType.S); final AttributeDefinition id = new AttributeDefinition().withAttributeName(ATTR_ID) .withAttributeType(ScalarAttributeType.S); final ArrayList<AttributeDefinition> tableAttributeDefinitions = Lists.newArrayList(nsKey, featureKey, id); final ArrayList<KeySchemaElement> tableKeySchema = Lists.newArrayList(); tableKeySchema.add( new KeySchemaElement().withAttributeName(HASH_KEY).withKeyType(KeyType.HASH)); tableKeySchema.add( new KeySchemaElement().withAttributeName(RANGE_KEY).withKeyType(KeyType.RANGE)); final ProvisionedThroughput tableProvisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(10L) .withWriteCapacityUnits(10L); final ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<>(); indexKeySchema.add( new KeySchemaElement().withAttributeName(HASH_KEY).withKeyType(KeyType.HASH)); indexKeySchema.add( new KeySchemaElement().withAttributeName(ATTR_ID).withKeyType(KeyType.RANGE)); final Projection projection = new Projection().withProjectionType(ProjectionType.INCLUDE); final ArrayList<String> indexColumns = new ArrayList<>(); indexColumns.add("json"); indexColumns.add("v"); projection.setNonKeyAttributes(indexColumns); final LocalSecondaryIndex localSecondaryIndex = new LocalSecondaryIndex() .withIndexName("feature_by_ns_key_and_id_lsi_idx") .withKeySchema(indexKeySchema) .withProjection(projection); final ArrayList<LocalSecondaryIndex> secondaryIndices = new ArrayList<>(); secondaryIndices.add(localSecondaryIndex); final CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(tableName) .withKeySchema(tableKeySchema) .withAttributeDefinitions(tableAttributeDefinitions) .withProvisionedThroughput(tableProvisionedThroughput) .withLocalSecondaryIndexes(secondaryIndices); 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); }
@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; } } } }
private static void createTable( String tableName, long readCapacityUnits, long writeCapacityUnits, String hashKeyName, String hashKeyType, String rangeKeyName, String rangeKeyType) { try { 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 (rangeKeyName != null) { keySchema.add(new KeySchemaElement() .withAttributeName(rangeKeyName) .withKeyType(KeyType.RANGE)); attributeDefinitions.add(new AttributeDefinition() .withAttributeName(rangeKeyName) .withAttributeType(rangeKeyType)); } 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(hashKeyName).withKeyType(KeyType.HASH), new KeySchemaElement() .withAttributeName("PostedBy") .withKeyType(KeyType.RANGE)) .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()); } }