Search in sources :

Example 1 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper 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 DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project openhab1-addons by openhab.

the class DynamoDBPersistenceService method store.

/**
     * {@inheritDoc}
     */
@Override
public void store(Item item, String alias) {
    if (item.getState() instanceof UnDefType) {
        logger.debug("Undefined item state received. Not storing item.");
        return;
    }
    if (!isProperlyConfigured) {
        logger.warn("Configuration for dynamodb not yet loaded or broken. Not storing item.");
        return;
    }
    if (!maybeConnectAndCheckConnection()) {
        logger.warn("DynamoDB not connected. Not storing item.");
        return;
    }
    String realName = item.getName();
    String name = (alias != null) ? alias : realName;
    Date time = new Date(System.currentTimeMillis());
    State state = item.getState();
    logger.trace("Tried to get item from item class {}, state is {}", item.getClass(), state.toString());
    DynamoDBItem<?> dynamoItem = AbstractDynamoDBItem.fromState(name, state, time);
    DynamoDBMapper mapper = getDBMapper(tableNameResolver.fromItem(dynamoItem));
    if (!createTable(mapper, dynamoItem.getClass())) {
        logger.warn("Table creation failed. Not storing item");
        return;
    }
    try {
        logger.debug("storing {} in dynamo. Serialized value {}. Original Item: {}", name, state, item);
        mapper.save(dynamoItem);
        logger.debug("Sucessfully stored item {}", item);
    } catch (AmazonClientException e) {
        logger.error("Error storing object to dynamo: {}", e.getMessage());
    }
}
Also used : UnDefType(org.openhab.core.types.UnDefType) State(org.openhab.core.types.State) AmazonClientException(com.amazonaws.AmazonClientException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Date(java.util.Date)

Example 3 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper 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 4 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project tutorials by eugenp.

the class ProductInfoRepositoryIntegrationTest method setupClass.

@BeforeClass
public static void setupClass() {
    Properties testProperties = loadFromFileInClasspath("test.properties").filter(properties -> !isEmpty(properties.getProperty(AWS_ACCESSKEY))).filter(properties -> !isEmpty(properties.getProperty(AWS_SECRETKEY))).filter(properties -> !isEmpty(properties.getProperty(DYNAMODB_ENDPOINT))).orElseThrow(() -> new RuntimeException("Unable to get all of the required test property values"));
    String amazonAWSAccessKey = testProperties.getProperty(AWS_ACCESSKEY);
    String amazonAWSSecretKey = testProperties.getProperty(AWS_SECRETKEY);
    String amazonDynamoDBEndpoint = testProperties.getProperty(DYNAMODB_ENDPOINT);
    amazonDynamoDB = new AmazonDynamoDBClient(new BasicAWSCredentials(amazonAWSAccessKey, amazonAWSSecretKey));
    amazonDynamoDB.setEndpoint(amazonDynamoDBEndpoint);
    dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB);
}
Also used : ResourceInUseException(com.amazonaws.services.dynamodbv2.model.ResourceInUseException) BeforeClass(org.junit.BeforeClass) AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient) URISyntaxException(java.net.URISyntaxException) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Assert.assertThat(org.junit.Assert.assertThat) ProductInfo(com.baeldung.dynamodb.entity.ProductInfo) Is.is(org.hamcrest.core.Is.is) LocalDbCreationRule(com.baeldung.dynamodb.rule.LocalDbCreationRule) ClassRule(org.junit.ClassRule) Path(java.nio.file.Path) Before(org.junit.Before) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Properties(java.util.Properties) Files(java.nio.file.Files) Test(org.junit.Test) IOException(java.io.IOException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) List(java.util.List) ProductInfoRepository(com.baeldung.dynamodb.repository.ProductInfoRepository) Paths(java.nio.file.Paths) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Optional(java.util.Optional) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) InputStream(java.io.InputStream) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) AmazonDynamoDBClient(com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient) Properties(java.util.Properties) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) BeforeClass(org.junit.BeforeClass)

Example 5 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project qpp-conversion-tool by CMSgov.

the class DynamoDbConfig method dynamoDbMapper.

/**
 * Creates a DynamoDB object mapper {@link Bean} that interacts with {@link DynamoDBTable} annotated POJOs.
 * Based on an available {@link AmazonDynamoDB} and {@link AWSKMS} {@link Bean}.
 *
 * Creates a DynamoDB object that depends on two different environment variables. {@code DYNAMO_TABLE_NAME} and
 * {@code KMS_KEY}.
 *
 * If {@code DYNAMO_TABLE_NAME} is specified, the value will be used for all read/write from/to the table of that name
 * regardless of what is specified for {@link DynamoDBTable}.  Else, the table specified for {@link DynamoDBTable} will be
 * used like normal.
 *
 * If {@code KMS_KEY} is specified, items in the tables will be encrypted.  Else, the items will not be encrypted.
 *
 * @param dynamoDbClient The {@link AmazonDynamoDB} {@link Bean}.
 * @return The DynamoDB object mapper
 */
@Bean
public DynamoDBMapper dynamoDbMapper(AmazonDynamoDB dynamoDbClient) {
    DynamoDBMapper dynamoDbMapper;
    final Optional<String> kmsKey = getOptionalProperty(Constants.KMS_KEY_ENV_VARIABLE);
    final Optional<String> tableName = getOptionalProperty(Constants.DYNAMO_TABLE_NAME_ENV_VARIABLE);
    final Optional<String> noAudit = getOptionalProperty(Constants.NO_AUDIT_ENV_VARIABLE);
    if (!noAudit.isPresent()) {
        if (tableName.isPresent() && kmsKey.isPresent()) {
            API_LOG.info("Using DynamoDB table name {} and KMS key {}.", tableName, kmsKey);
            dynamoDbMapper = createDynamoDbMapper(dynamoDbClient, tableNameOverrideConfig(tableName.get()), encryptionTransformer(kmsKey.get()));
        } else if (kmsKey.isPresent()) {
            API_LOG.warn("Using KMS key {}, but no DynamoDB table name specified.", tableName);
            dynamoDbMapper = createDynamoDbMapper(dynamoDbClient, getDynamoDbMapperConfig(), encryptionTransformer(kmsKey.get()));
        } else {
            API_LOG.error(NO_KMS_KEY + " This is a fatal error.");
            throw new BeanInitializationException(NO_KMS_KEY);
        }
    } else {
        API_LOG.info("Will not save any audit information.");
        dynamoDbMapper = null;
    }
    return dynamoDbMapper;
}
Also used : BeanInitializationException(org.springframework.beans.factory.BeanInitializationException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Bean(org.springframework.context.annotation.Bean)

Aggregations

DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)17 IOException (java.io.IOException)5 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)4 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)4 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 HashMap (java.util.HashMap)4 GoraException (org.apache.gora.util.GoraException)4 ResourceInUseException (com.amazonaws.services.dynamodbv2.model.ResourceInUseException)3 Date (java.util.Date)3 Before (org.junit.Before)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)2 DynamoDBMapperConfig (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapperConfig)2 DynamoDBQueryExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBQueryExpression)2 DynamoDBScanExpression (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBScanExpression)2 ProductInfo (com.baeldung.dynamodb.entity.ProductInfo)2 ProductInfoRepository (com.baeldung.dynamodb.repository.ProductInfoRepository)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2