/** * Creates the table and ignores any errors if it already exists. * @param dynamo The Dynamo client to use. * @param createTableRequest The create table request. * @return True if created, false otherwise. */ public static final boolean createTableIfNotExists(final AmazonDynamoDB dynamo, final CreateTableRequest createTableRequest) { try { dynamo.createTable(createTableRequest); return true; } catch (final ResourceInUseException e) { if (LOG.isTraceEnabled()) { LOG.trace("Table " + createTableRequest.getTableName() + " already exists", e); } } return false; }
/** * Creates the specified table in DynamoDB. */ public Table createTable(CreateTableRequest req) { CreateTableResult result = client.createTable(req); return new Table(client, req.getTableName(), result.getTableDescription()); }
/** * Creates the table with the specified throughput; also populates the same * throughput for all global secondary indexes. * @param throughput The provisioned throughput. * @return The table decription. * @see com.amazonaws.services.dynamodbv2.AmazonDynamoDB#createTable * @see com.amazonaws.services.dynamodbv2.model.CreateTableRequest */ public TableDescription createTable(ProvisionedThroughput throughput) { final CreateTableRequest request = mapper.generateCreateTableRequest(model.targetType()); request.setProvisionedThroughput(throughput); if (request.getGlobalSecondaryIndexes() != null) { for (final GlobalSecondaryIndex gsi : request.getGlobalSecondaryIndexes()) { gsi.setProvisionedThroughput(throughput); } } return db.createTable(request).getTableDescription(); }
public void createTables() { ProvisionedThroughput throughput = new ProvisionedThroughput().withReadCapacityUnits(1000L).withWriteCapacityUnits(1000L); CreateTableRequest createEnvironments = dynamoDBMapper .generateCreateTableRequest(EnvironmentDDBRecord.class) .withProvisionedThroughput(throughput); createEnvironments .getGlobalSecondaryIndexes() .forEach(index -> index.withProvisionedThroughput(throughput)); CreateTableRequest createEnvironmentRevisions = dynamoDBMapper .generateCreateTableRequest(EnvironmentRevisionDDBRecord.class) .withProvisionedThroughput(throughput); amazonDynamoDB.createTable(createEnvironments); amazonDynamoDB.createTable(createEnvironmentRevisions); } }
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 CreateTableResult createTable( List<AttributeDefinition> attributeDefinitions, String tableName, List<KeySchemaElement> keySchema, ProvisionedThroughput provisionedThroughput) throws AmazonServiceException, AmazonClientException { return getBackend().createTable(attributeDefinitions, tableName, keySchema, provisionedThroughput); }
@Override public CreateTableResult createTable(CreateTableRequest request) throws AmazonServiceException, AmazonClientException { return getBackend().createTable(request); }
/** * Creates the table and ignores any errors if it already exists. * @param dynamo The Dynamo client to use. * @param createTableRequest The create table request. * @return True if created, false otherwise. */ public static final boolean createTableIfNotExists(final AmazonDynamoDB dynamo, final CreateTableRequest createTableRequest) { try { dynamo.createTable(createTableRequest); return true; } catch (final ResourceInUseException e) { if (LOG.isTraceEnabled()) { LOG.trace("Table " + createTableRequest.getTableName() + " already exists", e); } } return false; }
/** * 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); }
/** * Creates the specified table in DynamoDB. */ public Table createTable(CreateTableRequest req) { CreateTableResult result = client.createTable(req); return new Table(client, req.getTableName(), result.getTableDescription()); }
/** * Creates a DynamoDB Table with the correct properties to be used with a ProviderStore. * * @param ddb interface for accessing DynamoDB * @param tableName name of table that stores the meta data of the material. * @param provisionedThroughput required provisioned throughput of the this table. * @return result of create table request. */ 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); }
/** * 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); } }
private CreateTableResult createTable(final CreateTableRequest request) throws BackendException { controlPlaneRateLimiter.acquire(); final Timer.Context apiTimerContext = getTimerContext(CREATE_TABLE, request.getTableName()); CreateTableResult result; try { result = client.createTable(request); } catch (final Exception e) { throw processDynamoDbApiException(e, CREATE_TABLE, request.getTableName()); } finally { apiTimerContext.stop(); } return result; }
@Override public CreateTableResult createTable(String tableName, String hashKeyName) { List<AttributeDefinition> attributeDefinitions = new ArrayList<>(); attributeDefinitions.add(new AttributeDefinition(hashKeyName, ScalarAttributeType.S)); List<KeySchemaElement> ks = new ArrayList<>(); ks.add(new KeySchemaElement(hashKeyName, KeyType.HASH)); ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput(this.readCapacityUnits, this.writeCapacityUnits); CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withAttributeDefinitions(attributeDefinitions) .withKeySchema(ks) .withProvisionedThroughput(provisionedthroughput); return amazonDynamoDB.createTable(request); }
@Override public void clear() { Loggers.DYNAMODB_LOG.trace("[DS0123] DynamoDB store: Clearing {} items", getCacheName()); if (requestFactory.appliesRangeKey()) { throw new PersistenceException("DynamoDB clear operation not supported with applied range key"); } try { DeleteTableResult result = table.delete(); int numDeleted = result.getTableDescription().getItemCount().intValue(); Loggers.DYNAMODB_LOG.info("[DS0125] DynamoDB store: Cleared {} {} items", numDeleted, table.getTableName()); table.waitForDelete(); client.createTable(requestFactory.resolveCreateTableRequest()); table.waitForActive(); } catch (Exception e) { Loggers.DYNAMODB_LOG.error("[DS0124] {}: {}", e.getMessage(), e); throw new PersistenceException(e.getMessage(), e); } }
/** * Creates the FragmentCheckpoint table if it doesn't exist already. */ public void createTableIfDoesntExist() { // Check if table exists if (!checkIfTableExists()) { log.info("Creating table : {}", TABLE_NAME); final CreateTableRequest request = new CreateTableRequest() { { setAttributeDefinitions(Collections.singletonList(new AttributeDefinition(KVS_STREAM_NAME, ScalarAttributeType.S))); setKeySchema(Collections.singletonList(new KeySchemaElement(KVS_STREAM_NAME, KeyType.HASH))); setProvisionedThroughput(new ProvisionedThroughput(1000L, 1000L)); setTableName(TABLE_NAME); } }; try { final CreateTableResult result = ddbClient.createTable(request); log.info("Table created : {}", result.getTableDescription()); } catch (final AmazonDynamoDBException e) { log.error("Error creating DDB table {}...", TABLE_NAME, e); throw e; } } }
@Test public void get_whenTableAlreadyExists_doesNotCreateTable() { CreateDynamoDBTable createDynamoDBTable = new CreateDynamoDBTable(dynamoDB, "asdf", "asdf", 1, 1); //setup when(dynamoDB.describeTable("asdf")).thenReturn(new DescribeTableResult().withTable(new TableDescription())); //test createDynamoDBTable.get(); //verify verify(dynamoDB).describeTable("asdf"); verify(dynamoDB, Mockito.never()).createTable(any(CreateTableRequest.class)); }
@Test public void get_whenTableDoesNotExist_createsTable() { CreateDynamoDBTable createDynamoDBTable = new CreateDynamoDBTable(dynamoDB, "asdf", "asdf", 1, 1); //setup when(dynamoDB.describeTable("asdf")).thenThrow(new ResourceNotFoundException("asdf")); when(dynamoDB.createTable(any(CreateTableRequest.class))) .thenReturn(new CreateTableResult().withTableDescription(new TableDescription())); when(dynamoDB.describeTable(new DescribeTableRequest().withTableName("asdf"))) .thenReturn(new DescribeTableResult().withTable(new TableDescription().withTableStatus(TableStatus.ACTIVE))); //test createDynamoDBTable.get(); //verify verify(dynamoDB).describeTable("asdf"); verify(dynamoDB).createTable(any(CreateTableRequest.class)); } }
/** * Creates the table with the specified throughput; also populates the same * throughput for all global secondary indexes. * @param throughput The provisioned throughput. * @return The table decription. * @see com.amazonaws.services.dynamodbv2.AmazonDynamoDB#createTable * @see com.amazonaws.services.dynamodbv2.model.CreateTableRequest */ public TableDescription createTable(ProvisionedThroughput throughput) { final CreateTableRequest request = mapper.generateCreateTableRequest(model.targetType()); request.setProvisionedThroughput(throughput); if (request.getGlobalSecondaryIndexes() != null) { for (final GlobalSecondaryIndex gsi : request.getGlobalSecondaryIndexes()) { gsi.setProvisionedThroughput(throughput); } } return db.createTable(request).getTableDescription(); }
/** * Creates a locking table with the given name. * <p> * This method does not check if a table with the given name exists already. * * @param dynamodb DynamoDB to be used * @param tableName table to be used * @param throughput AWS {@link ProvisionedThroughput throughput requirements} for the given lock setup * @return a {@link Table reference to the newly created table} * * @throws ResourceInUseException * The operation conflicts with the resource's availability. You attempted to recreate an * existing table. */ public static Table createLockTable( AmazonDynamoDB dynamodb, String tableName, ProvisionedThroughput throughput ) { CreateTableRequest request = new CreateTableRequest() .withTableName(tableName) .withKeySchema(new KeySchemaElement(ID, KeyType.HASH)) .withAttributeDefinitions(new AttributeDefinition(ID, ScalarAttributeType.S)) .withProvisionedThroughput(throughput); dynamodb.createTable(request).getTableDescription(); return new DynamoDB(dynamodb).getTable(tableName); } }