@Override public boolean deleteTable(String tableName) { try { DeleteTableRequest deleteTableRequest = new DeleteTableRequest().withTableName(tableName); DeleteTableResult deleteTableResult = dynamoDBClient.deleteTable(deleteTableRequest); if (deleteTableRequest != null) { LOG.info("Deleting table description: " + deleteTableResult); // Waiting for table deleted waitForTableDeleted(tableName); LOG.info("Successfully deleted table " + tableName); return true; } } catch (ResourceInUseException rie) { LOG.warn("Table " + tableName + " already exists"); } catch (AmazonServiceException ase) { LOG.error(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.error(ace.getMessage(), ace); } return false; }
/** * Create a table with the given hashKey as row id * * @param tableName * @param primaryKey */ public static void createTable(String tableName, String primaryKey) { ArrayList<KeySchemaElement> ks = new ArrayList<KeySchemaElement>(); ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); ks.add(new KeySchemaElement().withAttributeName(primaryKey) .withKeyType(KeyType.HASH)); attributeDefinitions.add(new AttributeDefinition().withAttributeName( primaryKey).withAttributeType("S")); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName).withKeySchema(ks) .withProvisionedThroughput(DEFAULT_PROVISIONED_THROUGHPUT); request.setAttributeDefinitions(attributeDefinitions); try { DynamoDbHandler.CLIENT.createTable(request); } catch (ResourceInUseException e) { //System.err.println("Table '" + tableName + "' already exists"); } }
/** * Private interface for creating tables which handles any instances of * Throttling of the API * * @param dynamoClient * @param dynamoTable * @return * @throws Exception */ public static CreateTableResult safeCreateTable( final AmazonDynamoDB dynamoClient, final CreateTableRequest createTableRequest) throws Exception { CreateTableResult res = null; final int tryMax = 10; int tries = 0; while (true) { try { res = dynamoClient.createTable(createTableRequest); return res; } catch (LimitExceededException le) { if (tries < tryMax) { // back off for 1 second Thread.sleep(1000); tries++; } else { throw le; } } catch (ResourceInUseException rie) { // someone else is trying to create the table while we are, so // return ok return null; } } }
/** * Executes a create table request using the DynamoDB client and waits the * default time until it's been created. * * @param awsClient * @param keySchema * @param tableName * @param proThrou */ public static void executeCreateTableRequest(AmazonDynamoDB awsClient, String tableName, ArrayList<KeySchemaElement> keySchema, Map<String, String> attrs, ProvisionedThroughput proThrou) { CreateTableRequest createTableRequest = buildCreateTableRequest(tableName, keySchema, proThrou, attrs); // use the client to perform the request try { awsClient.createTable(createTableRequest).getTableDescription(); // wait for table to become active waitForTableToBecomeAvailable(awsClient, tableName); } catch (ResourceInUseException ex) { LOG.warn("Table '{}' already exists.", tableName); } finally { LOG.info("Table '{}' is available.", tableName); } }
@Override public String create() { readWriteLock.writeLock().lock(); try { CreateTableResult result = client.createTable(constructCreateTableRequest()); waitForTableToBecomeActive(); return result.getTableDescription().getTableArn(); } catch (ResourceInUseException e) { throw new AlreadyExistsException(String.format("There is already a DynamoDB table called '%s'", tableName), e); } finally { readWriteLock.writeLock().unlock(); } }
@Test public void testResourceInUseException() throws InterruptedException { CreateTableRequest request = new CreateTableRequest(); when(mapper.generateCreateTableRequest(Model.class)).thenReturn(request); when(dynamoDB.createTable(request)).thenReturn(table); when(table.waitForActive()).thenThrow(new ResourceInUseException("")); getService(ModelRepository.class); verify(table).waitForActive(); }
public static String createTable(AmazonDynamoDBClient client, String tableName) { java.util.List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N")); java.util.List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(2L).withWriteCapacityUnits(2L); StreamSpecification streamSpecification = new StreamSpecification(); streamSpecification.setStreamEnabled(true); streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(tableName) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(keySchema) .withProvisionedThroughput(provisionedThroughput) .withStreamSpecification(streamSpecification); try { System.out.println("Creating table " + tableName); CreateTableResult result = client.createTable(createTableRequest); return result.getTableDescription().getLatestStreamId(); } catch(ResourceInUseException e) { System.out.println("Table already exists."); return describeTable(client, tableName).getTable().getLatestStreamId(); } }
/** * * Create table with given name and schema * */ public boolean createNewTable(String tableName, String hashKeyName, String hashKeyType, String rangeKeyName, String rangeKeyType, long readCapacity, long writeCapacity) { /** Attribute definition */ ArrayList<AttributeDefinition> attributeDefinition = new ArrayList<AttributeDefinition>(); attributeDefinition.add(new AttributeDefinition().withAttributeName(hashKeyName).withAttributeType(hashKeyType)); if (rangeKeyName != null) attributeDefinition.add(new AttributeDefinition().withAttributeName(rangeKeyName).withAttributeType(rangeKeyType)); /** KeySchema */ ArrayList<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName(hashKeyName).withKeyType(KeyType.HASH)); if (rangeKeyName != null) keySchema.add(new KeySchemaElement().withAttributeName(rangeKeyName).withKeyType(KeyType.RANGE)); /** Provisioned throughput */ ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(readCapacity).withWriteCapacityUnits(writeCapacity); /** Create table request */ CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withAttributeDefinitions(attributeDefinition).withKeySchema(keySchema) .withProvisionedThroughput(provisionedThroughput); /** Try to create table */ try { client.createTable(request); waitForTableToBecomeAvailable(tableName); System.out.println("Table " + tableName + " created."); } catch (ResourceInUseException riue) { System.out.println("Table " + tableName + " already existed."); return false; } catch (Exception e) { System.out.println(e.getMessage()); return false; } return true; }
@Override public boolean createTable(String tableName) { List<AttributeDefinition> attributeDefinitions= new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName(AttributeKey.UUID) .withAttributeType(ScalarAttributeType.S)); List<KeySchemaElement> keySchemaElement = new ArrayList<KeySchemaElement>(); keySchemaElement.add(new KeySchemaElement().withAttributeName(AttributeKey.UUID) .withKeyType(KeyType.HASH)); // Provide the initial provisioned throughput values as Java long data types ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(10L) .withWriteCapacityUnits(10L); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(tableName) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(keySchemaElement) .withProvisionedThroughput(provisionedThroughput); try { CreateTableResult createdTableDescription = dynamoDBClient.createTable(createTableRequest); LOG.info("Creating table description: " + createdTableDescription); // Wait for it to become active waitForTableAvailable(tableName); if (describeTable(tableName) != null) { return true; } } catch (ResourceInUseException rie) { LOG.warn("Table " + tableName + " already exists"); } catch (AmazonServiceException ase) { LOG.error(ase.getMessage(), ase); } catch (AmazonClientException ace) { LOG.error(ace.getMessage(), ace); } return false; }
/** * @return StreamArn */ public static String createTable(AmazonDynamoDBClient client, String tableName) { java.util.List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName("Id").withAttributeType("N")); java.util.List<KeySchemaElement> keySchema = new ArrayList<KeySchemaElement>(); keySchema.add(new KeySchemaElement().withAttributeName("Id").withKeyType(KeyType.HASH)); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput() .withReadCapacityUnits(2L).withWriteCapacityUnits(2L); StreamSpecification streamSpecification = new StreamSpecification(); streamSpecification.setStreamEnabled(true); streamSpecification.setStreamViewType(StreamViewType.NEW_IMAGE); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(tableName) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(keySchema) .withProvisionedThroughput(provisionedThroughput) .withStreamSpecification(streamSpecification); try { System.out.println("Creating table " + tableName); CreateTableResult result = client.createTable(createTableRequest); return result.getTableDescription().getLatestStreamArn(); } catch(ResourceInUseException e) { System.out.println("Table already exists."); return describeTable(client, tableName).getTable().getLatestStreamArn(); } }
private static void createTable() { List<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>(); attributeDefinitions.add(new AttributeDefinition().withAttributeName("name").withAttributeType("S")); List<KeySchemaElement> ks = new ArrayList<KeySchemaElement>(); ks.add(new KeySchemaElement().withAttributeName("name").withKeyType(KeyType.HASH)); ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput().withReadCapacityUnits(10L) .withWriteCapacityUnits(10L); CreateTableRequest request = new CreateTableRequest().withTableName(TABLENAME) .withAttributeDefinitions(attributeDefinitions).withKeySchema(ks) .withProvisionedThroughput(provisionedThroughput); try { CreateTableResult createdTableDescription = dynamoDBClient.createTable(request); logger.info("Created Table: " + createdTableDescription); // Wait for it to become active waitForTableToBecomeAvailable(TABLENAME); } catch (ResourceInUseException e) { logger.warn("Table already existed", e); } }
private void open(boolean batchLoading) { if(type == GraphDatabaseType.TITAN_DYNAMODB && config.getDynamodbPrecreateTables()) { List<CreateTableRequest> requests = new LinkedList<>(); long wcu = config.getDynamodbTps(); long rcu = Math.max(1, config.dynamodbConsistentRead() ? wcu : (wcu / 2)); for(String store : Constants.REQUIRED_BACKEND_STORES) { final String tableName = config.getDynamodbTablePrefix() + "_" + store; if(BackendDataModel.MULTI == config.getDynamodbDataModel()) { requests.add(DynamoDBStore.createTableRequest(tableName, rcu, wcu)); } else if(BackendDataModel.SINGLE == config.getDynamodbDataModel()) { requests.add(DynamoDBSingleRowStore.createTableRequest(tableName, rcu, wcu)); } } //TODO is this autocloseable? final AmazonDynamoDB client = new AmazonDynamoDBClient(Client.createAWSCredentialsProvider(config.getDynamodbCredentialsFqClassName(), config.getDynamodbCredentialsCtorArguments() == null ? null : config.getDynamodbCredentialsCtorArguments().split(","))); client.setEndpoint(config.getDynamodbEndpoint()); for(CreateTableRequest request : requests) { try { client.createTable(request); } catch(ResourceInUseException ignore) { //already created, good } } client.shutdown(); } titanGraph = buildTitanGraph(type, dbStorageDirectory, config, batchLoading); }
public static Task<Table> getTable(final DynamoDBConnection dynamoDBConnection, final String tableName) { final String tableCacheId = generateTableCacheId(dynamoDBConnection, tableName); final Table table = tableCache.get(tableCacheId); if(table != null) { return Task.fromValue(table); } else { return Task.fromFuture(dynamoDBConnection.getDynamoClient().describeTableAsync(tableName)) .thenApply(DescribeTableResult::getTable) .thenCompose(descriptor -> { if (descriptor.getTableStatus().equals(TableStatus.CREATING.name())) { return waitForActiveTableStatus(dynamoDBConnection, tableName); } else { return Task.fromValue(descriptor); } }) .thenApply(descriptor -> { final Table retrievedTable = dynamoDBConnection.getDynamoDB().getTable(descriptor.getTableName()); tableCache.putIfAbsent(tableCacheId, retrievedTable); return retrievedTable; }) .exceptionally(e -> { if (e instanceof ResourceInUseException) { return getTable(dynamoDBConnection, tableName).join(); } if (ExceptionUtils.isCauseInChain(ResourceNotFoundException.class, e)) { try { createTable(dynamoDBConnection, tableName); return getTable(dynamoDBConnection, tableName).join(); } catch (ResourceInUseException resourceInUseException) { return getTable(dynamoDBConnection, tableName).join(); } catch (InterruptedException interruptedException) { throw new UncheckedException(interruptedException); } } else { throw new UncheckedException(e); } }); } }
/** * Creates a table in Dynamo DB with the requested read and write capacity, * attributes, key schema and GSI's. This method will block until the table * is Active in Dynamo DB. * * @param dynamoClient * Dynamo DB Client to use for connection to Dynamo DB. * @param dynamoTable * The table name to create in Dynamo DB. * @param readCapacity * The requested amount of read IOPS to be provisioned. * @param writeCapacity * The requested amount of write IOPS to be provisioned. * @param attributes * Attribute Names which must be indicated to create the key * schema and/or GSI's. * @param keySchema * The keys used for the primary key of the table. * @param gsi * List of Global Secondary Indexes to be created on the table * @throws Exception */ public static void initTable(final AmazonDynamoDB dynamoClient, final String dynamoTable, final long readCapacity, final long writeCapacity, List<AttributeDefinition> attributes, List<KeySchemaElement> keySchema, final Collection<GlobalSecondaryIndex> gsi) throws Exception { try { DescribeTableResult res = safeDescribeTable(dynamoClient, dynamoTable); if (!res.getTable().getTableStatus().equals("ACTIVE")) { waitForTableActive(dynamoClient, dynamoTable); } } catch (ResourceInUseException r) { waitForTableActive(dynamoClient, dynamoTable); } catch (ResourceNotFoundException e) { LOG.info(String .format("Table %s Not Found - Creating with %s Reads/sec & %s Writes/sec", dynamoTable, readCapacity, writeCapacity)); CreateTableRequest createTableRequest = new CreateTableRequest() .withTableName(dynamoTable) .withProvisionedThroughput( new ProvisionedThroughput().withReadCapacityUnits( readCapacity).withWriteCapacityUnits( writeCapacity)).withKeySchema(keySchema) .withAttributeDefinitions(attributes); if (gsi != null) createTableRequest.withGlobalSecondaryIndexes(gsi); // create the table try { safeCreateTable(dynamoClient, createTableRequest); } catch (Exception ex) { LOG.error(ex); throw e; } // wait for it to go to active state waitForTableActive(dynamoClient, dynamoTable); } }