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()); } }
withKeyType(keyType);
/** * 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; }
@Override public int hashCode() { final int prime = 31; int hashCode = 1; hashCode = prime * hashCode + ((getAttributeName() == null) ? 0 : getAttributeName().hashCode()); hashCode = prime * hashCode + ((getKeyType() == null) ? 0 : getKeyType().hashCode()); return hashCode; }
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)); keySchema.add(new KeySchemaElement() .withAttributeName(rangeKeyName) .withKeyType(KeyType.RANGE)); attributeDefinitions.add(new AttributeDefinition() .withAttributeName(rangeKeyName) CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(keySchema) .withProvisionedThroughput( new ProvisionedThroughput() .withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits)); .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);
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)). withSSESpecification(new SSESpecification().withEnabled(ENCRYPTION_AT_REST_ENABLED)). 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)));
static void createExampleTable() { // Provide the initial provisioned throughput values as Java long data types ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(5L) .withWriteCapacityUnits(6L); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withProvisionedThroughput(provisionedThroughput); ArrayList<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N")); request.setAttributeDefinitions(attributeDefinitions); ArrayList<KeySchemaElement> tableKeySchema = new ArrayList<KeySchemaElement>(); tableKeySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); request.setKeySchema(tableKeySchema); client.createTable(request); waitForTableToBecomeAvailable(tableName); getTableInformation(); }
keySchema.add(new KeySchemaElement() .withAttributeName(hashKeyName) .withKeyType(KeyType.HASH)); attributeDefinitions.add(new AttributeDefinition() .withAttributeName(hashKeyName) .withAttributeType(hashKeyType)); keySchema.add(new KeySchemaElement() .withAttributeName(rangeKeyName) .withKeyType(KeyType.RANGE)); attributeDefinitions.add(new AttributeDefinition() .withAttributeName(rangeKeyName) keySchema, attributeDefinitions, new ProvisionedThroughput() .withReadCapacityUnits(readCapacityUnits) .withWriteCapacityUnits(writeCapacityUnits)); System.out.println("Waiting for " + tableName + " to be created...this may take a while...");
@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 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)); }
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)); .withKeySchema(new KeySchemaElement(a, KeyType.HASH)) .withProjection(new Projection().withProjectionType(ProjectionType.ALL)) .withProvisionedThroughput(rwCapacity) ); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(tableName) .withProvisionedThroughput(rwCapacity) .withSSESpecification(new SSESpecification().withEnabled(tableEncryptionAtRest)) .withKeySchema(keyAttrs)
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);
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; }
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));
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);
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; }
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);
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); }