public Map<String,LocalSecondaryIndex> localSecondaryIndexes() { final Map<String,LocalSecondaryIndex> map = new LinkedHashMap<String,LocalSecondaryIndex>(); for (final DynamoDBMapperFieldModel<T,Object> field : fields.values()) { for (final String indexName : field.localSecondaryIndexNames()) { final LocalSecondaryIndex lsi = new LocalSecondaryIndex().withIndexName(indexName); if (map.put(indexName, lsi) != null) { throw new DynamoDBMappingException( targetType.getSimpleName() + "[" + field.name() + "]; must not duplicate LSI " + indexName ); } lsi.withProjection(new Projection().withProjectionType(KEYS_ONLY)); lsi.withKeySchema(new KeySchemaElement(keys.get(HASH).name(), HASH)); lsi.withKeySchema(new KeySchemaElement(field.name(), RANGE)); } } if (map.isEmpty()) { return Collections.<String,LocalSecondaryIndex>emptyMap(); } return Collections.unmodifiableMap(map); }
public Map<String,GlobalSecondaryIndex> globalSecondaryIndexes() { final Map<String,GlobalSecondaryIndex> map = new LinkedHashMap<String,GlobalSecondaryIndex>(); for (final DynamoDBMapperFieldModel<T,Object> field : fields.values()) { for (final String indexName : field.globalSecondaryIndexNames(HASH)) { final GlobalSecondaryIndex gsi = new GlobalSecondaryIndex().withIndexName(indexName); if (map.put(indexName, gsi) != null) { throw new DynamoDBMappingException( targetType.getSimpleName() + "[" + field.name() + "]; must not duplicate GSI " + indexName ); } gsi.withProjection(new Projection().withProjectionType(KEYS_ONLY)); gsi.withKeySchema(new KeySchemaElement(field.name(), HASH)); } } for (final DynamoDBMapperFieldModel<T,Object> field : fields.values()) { for (final String indexName : field.globalSecondaryIndexNames(RANGE)) { final GlobalSecondaryIndex gsi = map.get(indexName); if (gsi == null) { throw new DynamoDBMappingException( targetType.getSimpleName() + "[" + field.name() + "]; no HASH key for GSI " + indexName ); } gsi.withKeySchema(new KeySchemaElement(field.name(), RANGE)); } } if (map.isEmpty()) { return Collections.<String,GlobalSecondaryIndex>emptyMap(); } return Collections.unmodifiableMap(map); }
/** * Gets the global secondary index. * @param indexName The index name. * @return The global secondary index or null. */ public GlobalSecondaryIndex globalSecondaryIndex(final String indexName) { if (!globalSecondaryIndexes.containsKey(indexName)) { return null; } final GlobalSecondaryIndex gsi = globalSecondaryIndexes.get(indexName); final GlobalSecondaryIndex copy = new GlobalSecondaryIndex().withIndexName(gsi.getIndexName()); copy.withProjection(new Projection().withProjectionType(gsi.getProjection().getProjectionType())); for (final KeySchemaElement key : gsi.getKeySchema()) { copy.withKeySchema(new KeySchemaElement(key.getAttributeName(), key.getKeyType())); } return copy; }
/** * Gets the local secondary index by name. * @param indexNames The index name. * @return The local secondary index, or null. */ public LocalSecondaryIndex localSecondaryIndex(final String indexName) { if (!localSecondaryIndexes.containsKey(indexName)) { return null; } final LocalSecondaryIndex lsi = localSecondaryIndexes.get(indexName); final LocalSecondaryIndex copy = new LocalSecondaryIndex().withIndexName(lsi.getIndexName()); copy.withProjection(new Projection().withProjectionType(lsi.getProjection().getProjectionType())); for (final KeySchemaElement key : lsi.getKeySchema()) { copy.withKeySchema(new KeySchemaElement(key.getAttributeName(), key.getKeyType())); } return copy; }
public KeySchemaElement unmarshall(JsonUnmarshallerContext context) throws Exception { KeySchemaElement keySchemaElement = new KeySchemaElement();
private void runTest(AmazonDynamoDB client) { CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions(new AttributeDefinition( "Name", ScalarAttributeType.S)) .withKeySchema(new KeySchemaElement("Name", KeyType.HASH)) .withProvisionedThroughput(new ProvisionedThroughput( new Long(10), new Long(10))) .withTableName("foo"); client.createTable(request); final TableDescription tableDescription = client.describeTable("foo").getTable(); assertNotNull("the description is not null", tableDescription); assertEquals("the table has the right name", "foo", tableDescription.getTableName()); assertEquals("the name has the right primary key", "Name", tableDescription.getKeySchema().get(0).getAttributeName()); } }
@Override public <T> CreateTableRequest generateCreateTableRequest(Class<T> clazz, DynamoDBMapperConfig config) { config = mergeConfig(config); final DynamoDBMapperTableModel<T> model = getTableModel(clazz, config); final CreateTableRequest request = new CreateTableRequest(); request.setTableName(getTableName(clazz, config)); request.withKeySchema(new KeySchemaElement(model.hashKey().name(), HASH)); if (model.rangeKeyIfExists() != null) { request.withKeySchema(new KeySchemaElement(model.rangeKey().name(), RANGE)); } request.setGlobalSecondaryIndexes(model.globalSecondaryIndexes()); request.setLocalSecondaryIndexes(model.localSecondaryIndexes()); for (final DynamoDBMapperFieldModel<T,Object> field : model.fields()) { if (field.keyType() != null || field.indexed()) { request.withAttributeDefinitions(new AttributeDefinition() .withAttributeType(ScalarAttributeType.valueOf(field.attributeType().name())) .withAttributeName(field.name()) ); } } return request; }
.withIndexName(lsiName) .withKeySchema( new KeySchemaElement(pHashKeyName, KeyType.HASH), new KeySchemaElement(lsiRangeKeyName, KeyType.RANGE)) .withProjection( new Projection()
gsi.withKeySchema(new KeySchemaElement(gsiHashKeyName, KeyType.HASH)); } else { final LinkedList<KeySchemaElement> orderedKeys = new LinkedList<KeySchemaElement>( gsi.getKeySchema()); orderedKeys.addFirst(new KeySchemaElement(gsiHashKeyName, KeyType.HASH)); gsi.setKeySchema(orderedKeys); gsi.withKeySchema(new KeySchemaElement(gsiRangeKeyName, KeyType.RANGE));
final AttributeDefinition pHashAttrDefinition = getKeyAttributeDefinition(pHashKeyGetter, converter); createTableRequest.withKeySchema(new KeySchemaElement(pHashAttrDefinition .getAttributeName(), KeyType.HASH)); if (pRangeKeyGetter != null) { pRangeAttrDefinition = getKeyAttributeDefinition(pRangeKeyGetter, converter); createTableRequest.withKeySchema(new KeySchemaElement(pRangeAttrDefinition .getAttributeName(), KeyType.RANGE));
public KeySchemaElement unmarshall(JsonUnmarshallerContext context) throws Exception { AwsJsonReader reader = context.getReader(); if (!reader.isContainer()) { reader.skipValue(); return null; } KeySchemaElement keySchemaElement = new KeySchemaElement(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("AttributeName")) { keySchemaElement.setAttributeName(StringJsonUnmarshaller.getInstance() .unmarshall(context)); } else if (name.equals("KeyType")) { keySchemaElement.setKeyType(StringJsonUnmarshaller.getInstance() .unmarshall(context)); } else { reader.skipValue(); } } reader.endObject(); return keySchemaElement; }
/** * Returns the key schema for the DynamoDB table. * * @return DynamoDB key schema */ static Collection<KeySchemaElement> keySchema() { return Arrays.asList( new KeySchemaElement(PARENT, KeyType.HASH), new KeySchemaElement(CHILD, KeyType.RANGE)); }
private void autoCreateTableIfNotExists() { if (autoCreateTable) { AttributeDefinition partitionKeyDefinition = new AttributeDefinition() .withAttributeName(partitionKeyName) .withAttributeType(ScalarAttributeType.S); KeySchemaElement partitionKeySchema = new KeySchemaElement() .withAttributeName(partitionKeyName) .withKeyType(KeyType.HASH); ProvisionedThroughput throughput = new ProvisionedThroughput() .withReadCapacityUnits(5L) .withWriteCapacityUnits(5L); try { TableUtils.createTableIfNotExists(dynamoDb, new CreateTableRequest() .withTableName(tableName) .withAttributeDefinitions(partitionKeyDefinition) .withKeySchema(partitionKeySchema) .withProvisionedThroughput(throughput)); } catch (AmazonDynamoDBException e) { throw new PersistenceException("Create table request failed", e); } } }
/** * 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); }
@Override public Collection<KeySchemaElement> getKeySchema() { List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName(LEASE_KEY_KEY).withKeyType(KeyType.HASH)); return keySchema; }
ProvisionedThroughput thorughput = new ProvisionedThroughput().withReadCapacityUnits(10L).withWriteCapacityUnits(5L); KeySchemaElement schemaElement = new KeySchemaElement().withAttributeName("Name").withAttributeType(ScalarAttributeType.S); CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(TOPIC_TABLE).withProvisionedThroughput(thorughput).withKeySchema(new KeySchema(schemaElement)); client.createTable(createTableRequest);
@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)); }
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(); }
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(); }
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); }