Search in sources :

Example 96 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project jcabi-dynamo by jcabi.

the class RegionITCase method worksWithAmazon.

/**
 * Region.Simple can work with AWS.
 * @throws Exception If some problem inside
 */
@Test
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public void worksWithAmazon() throws Exception {
    final String name = RandomStringUtils.randomAlphabetic(Tv.EIGHT);
    final RegionMock mock = new RegionMock();
    final Table tbl = mock.get(name).table(name);
    final String attr = RandomStringUtils.randomAlphabetic(Tv.EIGHT);
    final String value = RandomStringUtils.randomAlphanumeric(Tv.TEN);
    final String hash = RandomStringUtils.randomAlphanumeric(Tv.TEN);
    for (int idx = 0; idx < Tv.FIVE; ++idx) {
        tbl.put(new Attributes().with(mock.hash(), hash).with(mock.range(), idx).with(attr, value));
    }
    MatcherAssert.assertThat(tbl.frame().where(mock.hash(), Conditions.equalTo(hash)).through(new QueryValve().withLimit(1)), Matchers.hasSize(Tv.FIVE));
    final Frame frame = tbl.frame().where(attr, Conditions.equalTo(value)).through(new ScanValve().withLimit(Tv.TEN).withAttributeToGet(attr));
    MatcherAssert.assertThat(frame, Matchers.hasSize(Tv.FIVE));
    final Iterator<Item> items = frame.iterator();
    final Item item = items.next();
    final int range = Integer.parseInt(item.get(mock.range()).getN());
    MatcherAssert.assertThat(item.get(attr).getS(), Matchers.equalTo(value));
    item.put(attr, new AttributeValueUpdate(new AttributeValue("empty"), AttributeAction.PUT));
    MatcherAssert.assertThat(tbl.frame().where(mock.hash(), hash).where(mock.range(), Conditions.equalTo(range)).through(new ScanValve()).iterator().next().get(attr).getS(), Matchers.not(Matchers.equalTo(value)));
    items.remove();
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) Test(org.junit.Test)

Example 97 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project jcabi-dynamo by jcabi.

the class H2DataTest method updatesTableAttributes.

/**
 * H2Data can update table attributes.
 * @throws Exception In case test fails
 */
@Test
public void updatesTableAttributes() throws Exception {
    final String table = "tests";
    final String key = "tid";
    final int number = 43;
    final String attr = "descr";
    final String value = "Dummy\n\t\u20ac text";
    final String updated = "Updated";
    final MkData data = new H2Data().with(table, new String[] { key }, attr);
    data.put(table, new Attributes().with(key, number).with(attr, value));
    data.update(table, new Attributes().with(key, number), new AttributeUpdates().with(attr, "something else"));
    data.update(table, new Attributes().with(key, number), new AttributeUpdates().with(attr, updated));
    final Iterable<Attributes> result = data.iterate(table, new Conditions().with(key, Conditions.equalTo(number)));
    MatcherAssert.assertThat(result.iterator().next(), Matchers.hasEntry(Matchers.equalTo(attr), Matchers.equalTo(new AttributeValue(updated))));
    MatcherAssert.assertThat(result, Matchers.<Attributes>iterableWithSize(1));
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Attributes(com.jcabi.dynamo.Attributes) AttributeUpdates(com.jcabi.dynamo.AttributeUpdates) Conditions(com.jcabi.dynamo.Conditions) Test(org.junit.Test)

Example 98 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project jcabi-dynamo by jcabi.

the class MkRegionTest method storesAndReadsSingleAttribute.

/**
 * MkRegion can store and read items.
 * @throws Exception If some problem inside
 */
@Test
public void storesAndReadsSingleAttribute() throws Exception {
    final String table = "ideas";
    final String key = "number";
    final String attr = "total";
    final Region region = new MkRegion(new H2Data().with(table, new String[] { key }, attr));
    final Table tbl = region.table(table);
    tbl.put(new Attributes().with(key, "32443").with(attr, "0"));
    final Item item = tbl.frame().iterator().next();
    item.put(attr, new AttributeValueUpdate().withValue(new AttributeValue().withN("2")).withAction(AttributeAction.PUT));
    MatcherAssert.assertThat(item.get(attr).getN(), Matchers.equalTo("2"));
}
Also used : Item(com.jcabi.dynamo.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Table(com.jcabi.dynamo.Table) AttributeValueUpdate(com.amazonaws.services.dynamodbv2.model.AttributeValueUpdate) Attributes(com.jcabi.dynamo.Attributes) Region(com.jcabi.dynamo.Region) Test(org.junit.Test)

Example 99 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project tutorials by eugenp.

the class ProductInfoRepositoryIntegrationTest method setup.

@Before
public void setup() {
    try {
        repository = new ProductInfoRepository();
        repository.setMapper(dynamoDBMapper);
        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 : ProductInfoRepository(com.baeldung.dynamodb.repository.ProductInfoRepository) ProductInfo(com.baeldung.dynamodb.entity.ProductInfo) ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) Before(org.junit.Before)

Example 100 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project spring-integration-aws by spring-projects.

the class DynamoDbMetaDataStore method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws Exception {
    try {
        this.table.describe();
        this.createTableLatch.countDown();
        return;
    } catch (ResourceNotFoundException e) {
        if (logger.isInfoEnabled()) {
            logger.info("No table '" + this.table.getTableName() + "'. Creating one...");
        }
    }
    CreateTableRequest createTableRequest = new CreateTableRequest().withTableName(this.table.getTableName()).withKeySchema(new KeySchemaElement(KEY, KeyType.HASH)).withAttributeDefinitions(new AttributeDefinition(KEY, ScalarAttributeType.S)).withProvisionedThroughput(new ProvisionedThroughput(this.readCapacity, this.writeCapacity));
    this.dynamoDB.createTableAsync(createTableRequest, new AsyncHandler<CreateTableRequest, CreateTableResult>() {

        @Override
        public void onError(Exception e) {
            logger.error("Cannot create DynamoDb table: " + DynamoDbMetaDataStore.this.table.getTableName(), e);
            DynamoDbMetaDataStore.this.createTableLatch.countDown();
        }

        @Override
        public void onSuccess(CreateTableRequest request, CreateTableResult createTableResult) {
            Waiter<DescribeTableRequest> waiter = DynamoDbMetaDataStore.this.dynamoDB.waiters().tableExists();
            WaiterParameters<DescribeTableRequest> waiterParameters = new WaiterParameters<>(new DescribeTableRequest(DynamoDbMetaDataStore.this.table.getTableName())).withPollingStrategy(new PollingStrategy(new MaxAttemptsRetryStrategy(DynamoDbMetaDataStore.this.createTableRetries), new FixedDelayStrategy(DynamoDbMetaDataStore.this.createTableDelay)));
            waiter.runAsync(waiterParameters, new WaiterHandler<DescribeTableRequest>() {

                @Override
                public void onWaitSuccess(DescribeTableRequest request) {
                    DynamoDbMetaDataStore.this.createTableLatch.countDown();
                    DynamoDbMetaDataStore.this.table.describe();
                }

                @Override
                public void onWaitFailure(Exception e) {
                    logger.error("Cannot describe DynamoDb table: " + DynamoDbMetaDataStore.this.table.getTableName(), e);
                    DynamoDbMetaDataStore.this.createTableLatch.countDown();
                }
            });
        }
    });
}
Also used : MaxAttemptsRetryStrategy(com.amazonaws.waiters.MaxAttemptsRetryStrategy) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) WaiterParameters(com.amazonaws.waiters.WaiterParameters) PollingStrategy(com.amazonaws.waiters.PollingStrategy) FixedDelayStrategy(com.amazonaws.waiters.FixedDelayStrategy) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException) Waiter(com.amazonaws.waiters.Waiter) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) WaiterHandler(com.amazonaws.waiters.WaiterHandler) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) CreateTableResult(com.amazonaws.services.dynamodbv2.model.CreateTableResult)

Aggregations

Table (com.amazonaws.services.dynamodbv2.document.Table)63 Item (com.amazonaws.services.dynamodbv2.document.Item)40 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)38 AmazonServiceException (com.amazonaws.AmazonServiceException)35 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)31 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)28 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)28 IOException (java.io.IOException)24 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)23 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)23 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)23 HashMap (java.util.HashMap)23 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)22 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)22 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)17 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)17 ArrayList (java.util.ArrayList)17 AmazonClientException (com.amazonaws.AmazonClientException)14 Test (org.junit.Test)14 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)10