List of usage examples for com.amazonaws.services.dynamodbv2.model KeyType RANGE
KeyType RANGE
To view the source code for com.amazonaws.services.dynamodbv2.model KeyType RANGE.
Click Source Link
From source file:amazon.dynamodb.util.GeoTableUtil.java
License:Open Source License
/** * <p>/* w ww . j av a 2s . co m*/ * 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; }
From source file:aws.example.dynamodb.CreateTableCompositeKey.java
License:Open Source License
public static void main(String[] args) { final String USAGE = "\n" + "Usage:\n" + " CreateTable <table>\n\n" + "Where:\n" + " table - the table to create.\n\n" + "Example:\n" + " CreateTable GreetingsTable\n"; if (args.length < 1) { System.out.println(USAGE); System.exit(1);//from ww w.j a va 2 s.com } /* Read the name from command args */ String table_name = args[0]; System.out.format("Creating table %s\n with a composite primary key:\n"); System.out.format("* Language - partition key\n"); System.out.format("* Greeting - sort key\n"); CreateTableRequest request = new CreateTableRequest() .withAttributeDefinitions(new AttributeDefinition("Language", ScalarAttributeType.S), new AttributeDefinition("Greeting", ScalarAttributeType.S)) .withKeySchema(new KeySchemaElement("Language", KeyType.HASH), new KeySchemaElement("Greeting", KeyType.RANGE)) .withProvisionedThroughput(new ProvisionedThroughput(new Long(10), new Long(10))) .withTableName(table_name); final AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(); try { CreateTableResult result = ddb.createTable(request); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } System.out.println("Done!"); }
From source file:com.alertlogic.aws.analytics.poc.DynamoDBUtils.java
License:Open Source License
/** * Creates the table to store our counts in with a hash key of "resource" and a range key of "timestamp" so we can * query counts for a given resource by time. This uses an initial provisioned throughput of 10 read capacity units * and 5 write capacity units/* www .j ava 2 s .c o m*/ * * @param tableName The name of the table to create. */ public void createCountTableIfNotExists(String tableName) { List<KeySchemaElement> ks = new ArrayList<>(); ks.add(new KeySchemaElement().withKeyType(KeyType.HASH).withAttributeName(ATTRIBUTE_NAME_HASH_KEY)); ks.add(new KeySchemaElement().withKeyType(KeyType.RANGE).withAttributeName(ATTRIBUTE_NAME_RANGE_KEY)); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName(ATTRIBUTE_NAME_HASH_KEY) .withAttributeType(ScalarAttributeType.S)); // Range key must be a String. DynamoDBMapper translates Dates to ISO8601 strings. attributeDefinitions.add(new AttributeDefinition().withAttributeName(ATTRIBUTE_NAME_RANGE_KEY) .withAttributeType(ScalarAttributeType.S)); // Create the table with enough write IOPS to handle 5 distinct resources updated every 1 second: // 1 update/second * 5 resources = 5 write IOPS. // The provisioned throughput will need to be adjusted if the cadinality of the input data or the interval for // updates changes. CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withProvisionedThroughput(new ProvisionedThroughput(10L, 5L)).withKeySchema(ks) .withAttributeDefinitions(attributeDefinitions); try { dynamoDB.createTable(createTableRequest); LOG.info(String.format("Created DynamoDB table: %s. Waiting up to 5 minutes for it to become ACTIVE...", tableName)); // Wait 5 minutes for the table to become ACTIVE if (!waitUntilTableIsActive(tableName, 10, TimeUnit.MINUTES.toSeconds(5))) { throw new IllegalStateException( String.format("Timed out while waiting for DynamoDB table %s to become ready", tableName)); } } catch (ResourceInUseException ex) { // Assume table exists and is ready to use } }
From source file:com.amazon.janusgraph.diskstorage.dynamodb.DynamoDbStore.java
License:Open Source License
@Override public CreateTableRequest getTableSchema() { return super.getTableSchema() .withAttributeDefinitions(/* w ww . j av a 2 s .c o m*/ new AttributeDefinition().withAttributeName(Constants.JANUSGRAPH_HASH_KEY) .withAttributeType(ScalarAttributeType.S), new AttributeDefinition().withAttributeName(Constants.JANUSGRAPH_RANGE_KEY) .withAttributeType(ScalarAttributeType.S)) .withKeySchema( new KeySchemaElement().withAttributeName(Constants.JANUSGRAPH_HASH_KEY) .withKeyType(KeyType.HASH), new KeySchemaElement().withAttributeName(Constants.JANUSGRAPH_RANGE_KEY) .withKeyType(KeyType.RANGE)); }
From source file:com.amazon.services.awsrum.utils.DynamoDBUtils.java
License:Open Source License
public static void createTable(AmazonDynamoDBClient client, String tableName, String key, String rangeKey, long readCapacityUnits, long writeCapacityUnits) { if (tableExists(client, tableName)) { if (tableHasCorrectSchema(client, tableName, key, rangeKey)) { waitForActive(client, tableName); return; } else {/* ww w .j a v a 2s. com*/ LOG.error("Table already exists and schema does not match"); } } CreateTableRequest createTableRequest = new CreateTableRequest(); createTableRequest.setTableName(tableName); createTableRequest.setKeySchema(Arrays.asList(new KeySchemaElement(key, KeyType.HASH), new KeySchemaElement(rangeKey, KeyType.RANGE))); createTableRequest .setProvisionedThroughput(new ProvisionedThroughput(readCapacityUnits, writeCapacityUnits)); createTableRequest .setAttributeDefinitions(Arrays.asList(new AttributeDefinition(key, ScalarAttributeType.S), new AttributeDefinition(rangeKey, ScalarAttributeType.N))); try { client.createTable(createTableRequest); } catch (ResourceInUseException e) { throw new IllegalStateException("The table may already be getting created.", e); } LOG.info("Table " + tableName + " created"); waitForActive(client, tableName); }
From source file:com.amazon.services.awsrum.utils.DynamoDBUtils.java
License:Open Source License
private static boolean tableHasCorrectSchema(AmazonDynamoDBClient client, String tableName, String key, String rangeKey) {//from w w w . ja va 2s.c o m DescribeTableRequest describeTableRequest = new DescribeTableRequest(); describeTableRequest.setTableName(tableName); DescribeTableResult describeTableResult = client.describeTable(describeTableRequest); TableDescription tableDescription = describeTableResult.getTable(); if (tableDescription.getAttributeDefinitions().size() != 2) { LOG.error("The number of attribute definitions does not match the existing table."); return false; } AttributeDefinition attributeDefinition = tableDescription.getAttributeDefinitions().get(0); if (!attributeDefinition.getAttributeName().equals(key) || !attributeDefinition.getAttributeType().equals(ScalarAttributeType.S.toString())) { if (!attributeDefinition.getAttributeName().equals(rangeKey) || !attributeDefinition.getAttributeType().equals(ScalarAttributeType.N.toString())) { LOG.error("Attribute name or type does not match existing table."); return false; } } if (!attributeDefinition.getAttributeName().equals(rangeKey) || !attributeDefinition.getAttributeType().equals(ScalarAttributeType.N.toString())) { if (!attributeDefinition.getAttributeName().equals(key) || !attributeDefinition.getAttributeType().equals(ScalarAttributeType.S.toString())) { LOG.error("Attribute name or type does not match existing table."); return false; } } List<KeySchemaElement> KSEs = tableDescription.getKeySchema(); if (KSEs.size() != 2) { LOG.error("The number of key schema elements does not match the existing table."); return false; } KeySchemaElement kse = KSEs.get(0); if (!kse.getAttributeName().equals(key) || !kse.getKeyType().equals(KeyType.HASH.toString())) { LOG.error("The hash key does not match the existing table."); return false; } KeySchemaElement seconndKse = KSEs.get(1); if (!seconndKse.getAttributeName().equals(rangeKey) || !seconndKse.getKeyType().equals(KeyType.RANGE.toString())) { LOG.error("The hash range key does not match the existing table."); return false; } return true; }
From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.integration.DynamoDbDataGenerator.java
License:Apache License
void createStubItemWithRangeTable() throws Exception { final String tableName = unitTestSchemaName + "." + stubItemWithRangeTableName; boolean tableCreated = false; try {// w w w . j ava 2s. c o m final DescribeTableResult result = amazonDynamoDbClient.describeTable(tableName); if (isTableCreated(tableName, result)) { tableCreated = true; } } catch (final ResourceNotFoundException e) { tableCreated = false; } if (!tableCreated) { final Collection<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions.add(new AttributeDefinition("id", ScalarAttributeType.S)); attributeDefinitions.add(new AttributeDefinition("supportingId", ScalarAttributeType.S)); final Collection<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement("id", "S").withKeyType(KeyType.HASH)); keySchema.add(new KeySchemaElement("supportingId", "S").withKeyType(KeyType.RANGE)); final CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema) .withProvisionedThroughput(new ProvisionedThroughput(10L, 10L)); amazonDynamoDbClient.createTable(createTableRequest); final long startTime = System.currentTimeMillis(); do { Thread.sleep(1000); final DescribeTableResult describeTableResult = amazonDynamoDbClient.describeTable(tableName); tableCreated = isTableCreated(tableName, describeTableResult); } while (!tableCreated && System.currentTimeMillis() - startTime < 60000); } }
From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.manager.DynamoDbTemplateInfrastructureManager.java
License:Apache License
public void init() { for (final AbstractDynamoDbTemplate dynamoDbTemplate : dynamoDbTemplates) { final Collection<String> tablesPendingCreation = new ArrayList<>(); final DatabaseSchemaHolder databaseSchemaHolder = dynamoDbTemplate.databaseSchemaHolder(); for (final ItemConfiguration itemConfiguration : databaseSchemaHolder.itemConfigurations()) { if (VariantItemConfiguration.class.isAssignableFrom(itemConfiguration.getClass())) { continue; }//from w w w . j a v a2s .c o m final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName(); if (!tablesPendingCreation.contains(tableName) && !isTableCreated(tableName)) { final List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); final PrimaryKeyDefinition primaryKeyDefinition = itemConfiguration.primaryKeyDefinition(); final String hashKey = primaryKeyDefinition.propertyName(); final ScalarAttributeType hashKeyType = getAttributeType(primaryKeyDefinition.propertyType()); attributeDefinitions.add( new AttributeDefinition().withAttributeName(hashKey).withAttributeType(hashKeyType)); final List<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement().withAttributeName(hashKey).withKeyType(KeyType.HASH)); if (CompoundPrimaryKeyDefinition.class.isAssignableFrom(primaryKeyDefinition.getClass())) { final CompoundPrimaryKeyDefinition compoundPrimaryKeyDefinition = (CompoundPrimaryKeyDefinition) primaryKeyDefinition; final String rangeKey = compoundPrimaryKeyDefinition.supportingPropertyName(); final ScalarAttributeType rangeKeyType = getAttributeType( compoundPrimaryKeyDefinition.supportingPropertyType()); attributeDefinitions.add(new AttributeDefinition().withAttributeName(rangeKey) .withAttributeType(rangeKeyType)); keySchema .add(new KeySchemaElement().withAttributeName(rangeKey).withKeyType(KeyType.RANGE)); } final Collection<GlobalSecondaryIndex> globalSecondaryIndexes = new ArrayList<>(); for (final IndexDefinition indexDefinition : itemConfiguration.indexDefinitions()) { final ScalarAttributeType attributeType = getAttributeType( primaryKeyDefinition.propertyType()); // if there are any indexes, we need to add attributes for them attributeDefinitions .add(new AttributeDefinition().withAttributeName(indexDefinition.propertyName()) .withAttributeType(attributeType)); final ProvisionedThroughput indexProvisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits((long) readThroughput) .withWriteCapacityUnits((long) writeThroughput); final GlobalSecondaryIndex globalSecondaryIndex = new GlobalSecondaryIndex() .withIndexName(indexDefinition.propertyName() + "_idx") .withProvisionedThroughput(indexProvisionedThroughput) .withProjection(new Projection().withProjectionType("ALL")); final ArrayList<KeySchemaElement> indexKeySchema = new ArrayList<KeySchemaElement>(); indexKeySchema.add(new KeySchemaElement().withAttributeName(indexDefinition.propertyName()) .withKeyType(KeyType.HASH)); globalSecondaryIndex.setKeySchema(indexKeySchema); globalSecondaryIndexes.add(globalSecondaryIndex); } final ProvisionedThroughput tableProvisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits((long) readThroughput) .withWriteCapacityUnits((long) writeThroughput); CreateTableRequest request = new CreateTableRequest().withTableName(tableName) .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema) .withProvisionedThroughput(tableProvisionedThroughput); if (!globalSecondaryIndexes.isEmpty()) { request = request.withGlobalSecondaryIndexes(globalSecondaryIndexes); } logger.debug("Creating table " + tableName); createTable(request, globalSecondaryIndexes.isEmpty()); tablesPendingCreation.add(tableName); } // Create tables for unique constraints if (!itemConfiguration.uniqueConstraints().isEmpty()) { final String uniqueConstraintTableName = databaseSchemaHolder.schemaName() + "-indexes." + itemConfiguration.tableName(); if (!isTableCreated(uniqueConstraintTableName)) { final List<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement("property", KeyType.HASH)); keySchema.add(new KeySchemaElement("value", KeyType.RANGE)); final List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions.add(new AttributeDefinition("property", ScalarAttributeType.S)); attributeDefinitions.add(new AttributeDefinition("value", ScalarAttributeType.S)); final ProvisionedThroughput tableProvisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits((long) readThroughput) .withWriteCapacityUnits((long) writeThroughput); final CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(uniqueConstraintTableName) .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema) .withProvisionedThroughput(tableProvisionedThroughput); createTable(createTableRequest, true); tablesPendingCreation.add(uniqueConstraintTableName); } } } // Create table for sequences if (!databaseSchemaHolder.sequenceConfigurations().isEmpty()) { final String sequenceTableName = databaseSchemaHolder.schemaName() + "-sequences"; if (!isTableCreated(sequenceTableName)) { final ProvisionedThroughput sequenceProvisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits((long) readThroughput) .withWriteCapacityUnits((long) writeThroughput); final List<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH)); final List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions .add(new AttributeDefinition().withAttributeName("name").withAttributeType("S")); final CreateTableRequest request = new CreateTableRequest().withTableName(sequenceTableName) .withAttributeDefinitions(attributeDefinitions).withKeySchema(keySchema) .withProvisionedThroughput(sequenceProvisionedThroughput); logger.debug("Creating sequence table " + sequenceTableName); amazonDynamoDbClient.createTable(request); tablesPendingCreation.add(sequenceTableName); } } logger.debug("Waiting for table creation: " + tablesPendingCreation); final Collection<String> tableNames = new ArrayList<>(tablesPendingCreation); final long startTime = System.currentTimeMillis(); while (System.currentTimeMillis() - startTime < TABLE_CREATION_TIMEOUT_MS) { for (final String tableName : tableNames) { if (isTableCreated(tableName)) { logger.debug("Table " + tableName + " successfully created"); tablesPendingCreation.remove(tableName); } try { Thread.sleep(1000); } catch (final InterruptedException e) { throw new IllegalStateException(e); } } if (tablesPendingCreation.size() == 0) { break; } tableNames.clear(); tableNames.addAll(tablesPendingCreation); } if (tablesPendingCreation.size() != 0) { throw new IllegalStateException( "Unable to create tables for DynamoDb template: " + tablesPendingCreation); } // Seed the sequences with their starting values if (!databaseSchemaHolder.sequenceConfigurations().isEmpty()) { final ScanRequest scanRequest = new ScanRequest(databaseSchemaHolder.schemaName() + "-sequences"); final ScanResult scanResult = amazonDynamoDbClient.scan(scanRequest); Map<String, AttributeValue> lastEvaluatedKey = null; final Map<String, Map<String, AttributeValue>> sequenceItems = new HashMap<>(); do { for (final Map<String, AttributeValue> item : scanResult.getItems()) { sequenceItems.put(item.get("name").getS(), item); } lastEvaluatedKey = scanResult.getLastEvaluatedKey(); } while (lastEvaluatedKey != null); for (final SequenceConfiguration sequenceConfiguration : databaseSchemaHolder .sequenceConfigurations()) { final Map<String, AttributeValue> sequenceItem = sequenceItems .get(sequenceConfiguration.sequenceName()); if (sequenceItem == null) { final Map<String, ExpectedAttributeValue> expectedResults = new HashMap<>(); expectedResults.put("name", new ExpectedAttributeValue(false)); final Map<String, AttributeValue> attributeMap = new HashMap<>(); attributeMap.put("name", new AttributeValue().withS(sequenceConfiguration.sequenceName())); attributeMap.put("currentValue", new AttributeValue() .withN(String.valueOf(sequenceConfiguration.startingValue() - 1))); final String tableName = databaseSchemaHolder.schemaName() + "-sequences"; final PutItemRequest itemRequest = new PutItemRequest().withTableName(tableName) .withItem(attributeMap).withExpected(expectedResults); amazonDynamoDbClient.putItem(itemRequest); } } } dynamoDbTemplate.initialize(amazonDynamoDbClient); } }
From source file:com.dell.doradus.db.s3.DynamoDBService2.java
License:Apache License
@Override public void createNamespace() { String table = getTenant().getName(); if (Tables.doesTableExist(m_client, table)) return;// w w w . j a v a 2s .c o m m_logger.info("Creating table: {}", table); CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(table) .withKeySchema(new KeySchemaElement().withAttributeName("key").withKeyType(KeyType.HASH), new KeySchemaElement().withAttributeName("column").withKeyType(KeyType.RANGE)) .withAttributeDefinitions( new AttributeDefinition().withAttributeName("key").withAttributeType(ScalarAttributeType.S), new AttributeDefinition().withAttributeName("column") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(m_readCapacityUnits) .withWriteCapacityUnits(m_writeCapacityUnits)); m_client.createTable(createTableRequest); try { Tables.awaitTableToBecomeActive(m_client, table); } catch (InterruptedException e) { throw new RuntimeException(e); } }
From source file:com.envirover.spl.stream.DynamoDBOutputStream.java
License:Open Source License
@Override public void open() throws IOException { AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder.defaultClient(); if (TableUtils.createTableIfNotExists(dynamoDBClient, new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement(ATTR_DEVICE_ID, KeyType.HASH), new KeySchemaElement(ATTR_TIME, KeyType.RANGE)) .withAttributeDefinitions(new AttributeDefinition(ATTR_DEVICE_ID, ScalarAttributeType.S), new AttributeDefinition(ATTR_TIME, ScalarAttributeType.N)) .withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(READ_CAPACITY) .withWriteCapacityUnits(WRITE_CAPACITY)))) { try {//from w w w. jav a2 s. c o m TableUtils.waitUntilActive(dynamoDBClient, tableName); } catch (TableNeverTransitionedToStateException e) { throw new IOException(e); } catch (InterruptedException e) { throw new IOException(e); } logger.info(MessageFormat.format("DynamoDB table ''{0}'' created.", tableName)); } dynamoDB = new DynamoDB(dynamoDBClient); }