Search in sources :

Example 1 with ProvisionedThroughput

use of com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput in project openhab1-addons by openhab.

the class DynamoDBPersistenceService method createTable.

/**
     * Create table (if not present) and wait for table to become active.
     *
     * Synchronized in order to ensure that at most single thread is creating the table at a time
     *
     * @param mapper
     * @param dtoClass
     * @return whether table creation succeeded.
     */
private synchronized boolean createTable(DynamoDBMapper mapper, Class<?> dtoClass) {
    if (db == null) {
        return false;
    }
    String tableName;
    try {
        ProvisionedThroughput provisionedThroughput = new ProvisionedThroughput(dbConfig.getReadCapacityUnits(), dbConfig.getWriteCapacityUnits());
        CreateTableRequest request = mapper.generateCreateTableRequest(dtoClass);
        request.setProvisionedThroughput(provisionedThroughput);
        if (request.getGlobalSecondaryIndexes() != null) {
            for (GlobalSecondaryIndex index : request.getGlobalSecondaryIndexes()) {
                index.setProvisionedThroughput(provisionedThroughput);
            }
        }
        tableName = request.getTableName();
        try {
            db.getDynamoClient().describeTable(tableName);
        } catch (ResourceNotFoundException e) {
            // No table present, continue with creation
            db.getDynamoClient().createTable(request);
        } catch (AmazonClientException e) {
            logger.error("Table creation failed due to error in describeTable operation", e);
            return false;
        }
        // table found or just created, wait
        return waitForTableToBecomeActive(tableName);
    } catch (AmazonClientException e) {
        logger.error("Exception when creating table", e);
        return false;
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) GlobalSecondaryIndex(com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex)

Example 2 with ProvisionedThroughput

use of com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput in project tutorials by eugenp.

the class ProductInfoRepositoryIntegrationTest method setup.

@Before
public void setup() throws Exception {
    try {
        dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
        CreateTableRequest tableRequest = dynamoDBMapper.generateCreateTableRequest(ProductInfo.class);
        tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
        amazonDynamoDB.createTable(tableRequest);
    } catch (ResourceInUseException e) {
    // Do nothing, table already created
    }
    // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table
    dynamoDBMapper.batchDelete((List<ProductInfo>) repository.findAll());
}
Also used : ProductInfo(com.baeldung.spring.data.dynamodb.model.ProductInfo) ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) Before(org.junit.Before)

Example 3 with ProvisionedThroughput

use of com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput in project jcabi-dynamo by jcabi.

the class RegionMock method get.

/**
 * Get region with a table.
 * @param table Table name
 * @return Region
 * @throws Exception If fails
 */
public Region get(final String table) throws Exception {
    final Region region = new Region.Simple(new Credentials.Direct(Credentials.TEST, this.prt));
    final MadeTable mocker = new MadeTable(region, new CreateTableRequest().withTableName(table).withProvisionedThroughput(new ProvisionedThroughput().withReadCapacityUnits(1L).withWriteCapacityUnits(1L)).withAttributeDefinitions(new AttributeDefinition().withAttributeName(this.ahash).withAttributeType(ScalarAttributeType.S), new AttributeDefinition().withAttributeName(this.arange).withAttributeType(ScalarAttributeType.N)).withKeySchema(new KeySchemaElement().withAttributeName(this.ahash).withKeyType(KeyType.HASH), new KeySchemaElement().withAttributeName(this.arange).withKeyType(KeyType.RANGE)));
    mocker.create();
    mocker.createIfAbsent();
    return new ReRegion(region);
}
Also used : MadeTable(com.jcabi.dynamo.mock.MadeTable) ReRegion(com.jcabi.dynamo.retry.ReRegion) ReRegion(com.jcabi.dynamo.retry.ReRegion) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 4 with ProvisionedThroughput

use of com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput in project jcabi-dynamo by jcabi.

the class ThroughputTest method adjustsThroughput.

/**
 * Throughput can change throughput of a table.
 * @throws Exception If something went wrong
 */
@Test
public final void adjustsThroughput() throws Exception {
    final Table table = Mockito.mock(Table.class);
    final Region region = Mockito.mock(Region.class);
    final AmazonDynamoDB aws = Mockito.mock(AmazonDynamoDB.class);
    Mockito.when(table.region()).thenReturn(region);
    final String name = "Customers";
    Mockito.when(table.name()).thenReturn(name);
    Mockito.when(region.aws()).thenReturn(aws);
    new Throughput(table).adjust();
    Mockito.verify(aws, Mockito.times(1)).updateTable(Mockito.eq(name), Mockito.<ProvisionedThroughput>any());
}
Also used : ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) Test(org.junit.Test)

Example 5 with ProvisionedThroughput

use of com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput in project aws-doc-sdk-examples by awsdocs.

the class LowLevelParallelScan method createTable.

private static void createTable(String tableName, long readCapacityUnits, long writeCapacityUnits, String partitionKeyName, String partitionKeyType, String sortKeyName, String sortKeyType) {
    try {
        System.out.println("Creating table " + tableName);
        ArrayList<KeySchemaElement> ks = new ArrayList<KeySchemaElement>();
        ArrayList<AttributeDefinition> attributeDefinitions = new ArrayList<AttributeDefinition>();
        // Partition
        ks.add(new KeySchemaElement().withAttributeName(partitionKeyName).withKeyType(KeyType.HASH));
        // key
        attributeDefinitions.add(new AttributeDefinition().withAttributeName(partitionKeyName).withAttributeType(partitionKeyType));
        if (sortKeyName != null) {
            // Sort
            ks.add(new KeySchemaElement().withAttributeName(sortKeyName).withKeyType(KeyType.RANGE));
            // key
            attributeDefinitions.add(new AttributeDefinition().withAttributeName(sortKeyName).withAttributeType(sortKeyType));
        }
        // Provide initial provisioned throughput values as Java long data
        // types
        ProvisionedThroughput provisionedthroughput = new ProvisionedThroughput().withReadCapacityUnits(readCapacityUnits).withWriteCapacityUnits(writeCapacityUnits);
        CreateTableRequest request = new CreateTableRequest().withTableName(tableName).withKeySchema(ks).withProvisionedThroughput(provisionedthroughput).withAttributeDefinitions(attributeDefinitions);
        client.createTable(request);
    } catch (AmazonServiceException ase) {
        System.err.println("Failed to create table " + tableName + " " + ase);
    }
}
Also used : ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Aggregations

ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)30 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)25 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)23 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)23 ArrayList (java.util.ArrayList)14 Table (com.amazonaws.services.dynamodbv2.document.Table)11 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)7 AmazonServiceException (com.amazonaws.AmazonServiceException)5 CreateTableResult (com.amazonaws.services.dynamodbv2.model.CreateTableResult)5 Projection (com.amazonaws.services.dynamodbv2.model.Projection)5 ResourceInUseException (com.amazonaws.services.dynamodbv2.model.ResourceInUseException)5 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)3 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)3 GlobalSecondaryIndex (com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndex)3 LocalSecondaryIndex (com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndex)3 StreamSpecification (com.amazonaws.services.dynamodbv2.model.StreamSpecification)3 AmazonClientException (com.amazonaws.AmazonClientException)2 DeleteTableRequest (com.amazonaws.services.dynamodbv2.model.DeleteTableRequest)2 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)2 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)2