List of usage examples for com.amazonaws.services.dynamodbv2.model KeyType HASH
KeyType HASH
To view the source code for com.amazonaws.services.dynamodbv2.model KeyType HASH.
Click Source Link
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 {/*from ww w . j a va 2s .c om*/ 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 ww . java 2s .com 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 createStubItemTable() throws Exception { final String tableName = unitTestSchemaName + "." + stubItemTableName; boolean tableCreated = false; try {/*w w w . ja v a2 s .co 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)); final Collection<KeySchemaElement> keySchema = new ArrayList<>(); keySchema.add(new KeySchemaElement("id", "S").withKeyType(KeyType.HASH)); 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.integration.DynamoDbDataGenerator.java
License:Apache License
void createStubItemWithRangeTable() throws Exception { final String tableName = unitTestSchemaName + "." + stubItemWithRangeTableName; boolean tableCreated = false; try {//from ww w . j av a 2 s . 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 ww .j a v a 2 s. 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.cloudkon.remote.worker.Dynamodb.java
License:Open Source License
public void createTable(String tableName) { init();/* w w w .j a v a 2 s .c om*/ this.tableName = tableName; // Create table if it does not exist yet if (!Tables.doesTableExist(dynamoDB, tableName)) { // Create a table with a primary hash key named 'taskId', which holds a string CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema(new KeySchemaElement().withAttributeName("taskId").withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName("taskId") .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L)); TableDescription createdTableDescription = dynamoDB.createTable(createTableRequest) .getTableDescription(); System.out.println("Created Table: " + createdTableDescription); // Wait for it to become active System.out.println("Waiting for " + tableName + " to become ACTIVE..."); try { Tables.awaitTableToBecomeActive(dynamoDB, tableName); } catch (InterruptedException e) { System.out.println("Interrupted Exception"); } // Describe our new table DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(tableName); @SuppressWarnings("unused") TableDescription tableDescription = dynamoDB.describeTable(describeTableRequest).getTable(); } }
From source file:com.dell.doradus.db.dynamodb.DynamoDBService.java
License:Apache License
@Override public void createStoreIfAbsent(String storeName, boolean bBinaryValues) { String tableName = storeToTableName(storeName); if (!Tables.doesTableExist(m_ddbClient, tableName)) { // Create a table with a primary hash key named '_key', which holds a string m_logger.info("Creating table: {}", tableName); CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(tableName) .withKeySchema(/*from ww w . j a va2s . c o m*/ new KeySchemaElement().withAttributeName(ROW_KEY_ATTR_NAME).withKeyType(KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition().withAttributeName(ROW_KEY_ATTR_NAME) .withAttributeType(ScalarAttributeType.S)) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits(READ_CAPACITY_UNITS) .withWriteCapacityUnits(WRITE_CAPACITY_UNITS)); m_ddbClient.createTable(createTableRequest).getTableDescription(); try { Tables.awaitTableToBecomeActive(m_ddbClient, tableName); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
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;/*from w w w . ja v a 2 s . c om*/ 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 {//www. j av a2 s .co 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); }
From source file:com.erudika.para.persistence.AWSDynamoUtils.java
License:Apache License
/** * Creates a table in AWS DynamoDB.// w w w.ja v a 2 s. c o m * @param appid name of the {@link com.erudika.para.core.App} * @param readCapacity read capacity * @param writeCapacity write capacity * @return true if created */ public static boolean createTable(String appid, long readCapacity, long writeCapacity) { if (StringUtils.isBlank(appid)) { return false; } else if (StringUtils.containsWhitespace(appid)) { logger.warn("DynamoDB table name contains whitespace. The name '{}' is invalid.", appid); return false; } else if (existsTable(appid)) { logger.warn("DynamoDB table '{}' already exists.", appid); return false; } try { getClient().createTable(new CreateTableRequest().withTableName(getTableNameForAppid(appid)) .withKeySchema(new KeySchemaElement(Config._KEY, KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition(Config._KEY, ScalarAttributeType.S)) .withProvisionedThroughput(new ProvisionedThroughput(readCapacity, writeCapacity))); } catch (Exception e) { logger.error(null, e); return false; } return true; }