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; }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getAttributeName() == null) ? 0 : getAttributeName().hashCode()); hashCode = prime * hashCode + ((getAttributeType() == null) ? 0 : getAttributeType().hashCode()); return hashCode; }
public AttributeDefinition unmarshall(JsonUnmarshallerContext context) throws Exception { AttributeDefinition attributeDefinition = new AttributeDefinition(); if (context.testExpression("AttributeName", targetDepth)) { context.nextToken(); attributeDefinition.setAttributeName(context.getUnmarshaller(String.class).unmarshall(context)); attributeDefinition.setAttributeType(context.getUnmarshaller(String.class).unmarshall(context));
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); } } }
keySchema.add(new KeySchemaElement() .withAttributeName(hashKeyName) .withKeyType(KeyType.HASH)); attributeDefinitions.add(new AttributeDefinition() .withAttributeName(hashKeyName) .withAttributeType(hashKeyType)); .withAttributeName(rangeKeyName) .withKeyType(KeyType.RANGE)); attributeDefinitions.add(new AttributeDefinition() .withAttributeName(rangeKeyName) .withAttributeType(rangeKeyType)); keySchema, attributeDefinitions, new ProvisionedThroughput() .withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits)); System.out.println("Waiting for " + tableName + " to be created...this may take a while...");
@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)); }
attributeDefinitions.add(new AttributeDefinition().withAttributeName("videoid").withAttributeType("S")); attributeDefinitions.add(new AttributeDefinition().withAttributeName("category").withAttributeType("S")); keySchema.add(new KeySchemaElement().withAttributeName("videoid").withKeyType(KeyType.HASH)); keySchema.add(new KeySchemaElement().withAttributeName("category").withKeyType(KeyType.RANGE)); ProvisionedThroughput ptIndex = new ProvisionedThroughput().withReadCapacityUnits(150L) .withWriteCapacityUnits(150L); .withKeySchema(new KeySchemaElement().withAttributeName("category").withKeyType(KeyType.HASH), new KeySchemaElement().withAttributeName("videoid").withKeyType(KeyType.RANGE)) .withProjection(new Projection().withProjectionType(ProjectionType.ALL)); createTableRequest = new CreateTableRequest().withTableName(tableName).withKeySchema(keySchema) .withAttributeDefinitions(attributeDefinitions) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(100L).withWriteCapacityUnits(100L)) .withGlobalSecondaryIndexes(videoCategoryGsi);
keyAttrs.add(new KeySchemaElement(itemTransformer.getHashKeyAttributeName(), KeyType.HASH)); attrs.add(new AttributeDefinition(itemTransformer.getHashKeyAttributeName(), ScalarAttributeType.S)); keyAttrs.add(new KeySchemaElement(rangeKeyName, KeyType.RANGE)); attrs.add(new AttributeDefinition(rangeKeyName, ScalarAttributeType.S)); .withKeySchema(new KeySchemaElement(rangeKeyName, KeyType.HASH)) .withProjection(new Projection().withProjectionType(ProjectionType.ALL)) .withProvisionedThroughput(rwCapacity)); attrs.add(new AttributeDefinition(a, ScalarAttributeType.S)); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(tableName) .withProvisionedThroughput(rwCapacity) .withSSESpecification(new SSESpecification().withEnabled(tableEncryptionAtRest)) .withKeySchema(keyAttrs)
protected void init() throws Exception { List<AttributeDefinition> attributes = new ArrayList<AttributeDefinition>() { { add(new AttributeDefinition().withAttributeName(InventoryModel.AGGREGATOR).withAttributeType( "S")); add(new AttributeDefinition().withAttributeName(InventoryModel.SHARD_ID).withAttributeType( "S")); } }; List<KeySchemaElement> key = new ArrayList<KeySchemaElement>() { { add(new KeySchemaElement().withAttributeName(InventoryModel.AGGREGATOR).withKeyType( KeyType.HASH)); add(new KeySchemaElement().withAttributeName(InventoryModel.SHARD_ID).withKeyType( KeyType.RANGE)); } }; DynamoUtils.initTable(dynamoClient, InventoryModel.TABLE_NAME, InventoryModel.READ_CAPACITY, InventoryModel.WRITE_CAPACITY, attributes, key, null); online = true; }
final long writeCapacity = Long.parseLong(System.getProperty( DynamoDbDataContext.SYSTEM_PROPERTY_THROUGHPUT_WRITE_CAPACITY, "5")); final ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(readCapacity, writeCapacity); 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);
final String name = json.getString("TableName"); final CreateTableRequest request = new CreateTableRequest().withTableName(name); if (json.containsKey("KeySchema")) { final Collection<KeySchemaElement> keys = this.keySchema(json); request.setKeySchema(keys); for (final JsonObject defn : schema.getValuesAs(JsonObject.class)) { attrs.add( new AttributeDefinition( defn.getString("AttributeName"), defn.getString("AttributeType") json.getJsonObject("ProvisionedThroughput"); request.setProvisionedThroughput( new ProvisionedThroughput( this.asLong(throughput, "ReadCapacityUnits"), this.asLong(throughput, "WriteCapacityUnits") .withProjection(this.projection(index)) .withProvisionedThroughput( new ProvisionedThroughput( this.asLong(throughput, "ReadCapacityUnits"), this.asLong(throughput, "WriteCapacityUnits")
ItemConverter converter) { final CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.setTableName(DynamoDBMapper.internalGetTableName(clazz, null, config)); 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 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); } } }
/** * Builds the necessary requests to create tables * * @param tableName * @param keySchema * @param proThrou * @param attrs * @return */ public static CreateTableRequest buildCreateTableRequest(String tableName, ArrayList<KeySchemaElement> keySchema, ProvisionedThroughput proThrou, Map<String, String> attrs) { CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.setTableName(tableName); createTableRequest.setKeySchema(keySchema); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); for (KeySchemaElement kEle : keySchema) { AttributeDefinition attrDef = new AttributeDefinition(); attrDef.setAttributeName(kEle.getAttributeName()); attrDef.setAttributeType(attrs.get(kEle.getAttributeName())); attributeDefinitions.add(attrDef); } createTableRequest.setAttributeDefinitions(attributeDefinitions); createTableRequest.setProvisionedThroughput(proThrou); return createTableRequest; }
/** * 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); }
private static AttributeDefinition getKeyAttributeDefinition( Method keyGetter, ItemConverter converter) { final DynamoDBMapperFieldModel fieldModel = converter.getFieldModel(keyGetter); final String keyAttrName = fieldModel.getDynamoDBAttributeName(); final DynamoDBAttributeType keyType = fieldModel.getDynamoDBAttributeType(); if (keyType == DynamoDBAttributeType.S || keyType == DynamoDBAttributeType.N || keyType == DynamoDBAttributeType.B) { return new AttributeDefinition(keyAttrName, keyType.toString()); } throw new DynamoDBMappingException( "The key attribute must be in a scalar type " + "(String, Number or Binary)."); }
@Override public Collection<AttributeDefinition> getAttributeDefinitions() { List<AttributeDefinition> definitions = new ArrayList<AttributeDefinition>(); definitions.add(new AttributeDefinition().withAttributeName(LEASE_KEY_KEY) .withAttributeType(ScalarAttributeType.S)); return definitions; } }
final String keyName = element.getAttributeName(); for (final AttributeDefinition ad : this.tableDescription.getAttributeDefinitions()) { if (ad.getAttributeName().equals(keyName)) { final KeyDescription kd = new KeyDescription(); kd.setHash("HASH".equalsIgnoreCase(element.getKeyType())); kd.setType(getType(ad.getAttributeType())); if (kd.isHash()) { this.hashKeys.add(keyName);
final TableDescription description = table.describe(); System.out.println(description.getAttributeDefinitions()); final List<AttributeDefinition> tableAttributeDefinitions = description.getAttributeDefinitions(); for (GlobalSecondaryIndexDescription gsi : description .getGlobalSecondaryIndexes()) { System.out.println("IndexName: " + gsi.getIndexName()); for (KeySchemaElement keySchemaElement : gsi.getKeySchema()) { final String attrName = keySchemaElement.getAttributeName(); final AttributeDefinition attrDef = tableAttributeDefinitions.stream() .filter(ad -> ad.getAttributeName().equals(attrName)).findFirst() .orElseThrow(RuntimeException::new); System.out.println( "AttributeName: " + attrDef.getAttributeName() + ", AttributeType: " + attrDef.getAttributeType()); } }