Search in sources :

Example 21 with DynamoDBMapper

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

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

the class DynamoDbConfigTest method dbMapperNoAudit.

@Test
public void dbMapperNoAudit() {
    when(environment.getProperty(eq(Constants.NO_AUDIT_ENV_VARIABLE))).thenReturn("true");
    when(environment.getProperty(eq(Constants.DYNAMO_TABLE_NAME_ENV_VARIABLE))).thenReturn(null);
    when(environment.getProperty(eq(Constants.KMS_KEY_ENV_VARIABLE))).thenReturn(null);
    DynamoDBMapper dynamoDBMapper = underTest.dynamoDbMapper(amazonDynamoDB);
    assertThat(dynamoDBMapper).isNull();
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 23 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project gora by apache.

the class DynamoDBNativeStore method execute.

/**
 * Executes a query after building a DynamoDB specific query based on the
 * received one
 */
@Override
public Result<K, T> execute(Query<K, T> query) throws GoraException {
    try {
        DynamoDBQuery<K, T> dynamoDBQuery = buildDynamoDBQuery(query);
        DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
        List<T> objList = null;
        if (DynamoDBQuery.getType().equals(DynamoDBQuery.RANGE_QUERY))
            objList = mapper.scan(persistentClass, (DynamoDBScanExpression) dynamoDBQuery.getQueryExpression());
        if (DynamoDBQuery.getType().equals(DynamoDBQuery.SCAN_QUERY))
            objList = mapper.scan(persistentClass, (DynamoDBScanExpression) dynamoDBQuery.getQueryExpression());
        return new DynamoDBResult<K, T>(this, query, objList);
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) DynamoDBResult(org.apache.gora.dynamodb.query.DynamoDBResult) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GoraException(org.apache.gora.util.GoraException)

Example 24 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project gora by apache.

the class DynamoDBNativeStore method delete.

/**
 * Deletes the object using key
 *
 * @param key the key of the object
 * @return true for a successful process
 */
@Override
public boolean delete(K key) throws GoraException {
    try {
        T object = null;
        Object rangeKey = null, hashKey = null;
        DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
        for (Method met : key.getClass().getDeclaredMethods()) {
            if (met.getName().equals(GET_RANGE_KEY_METHOD)) {
                Object[] params = null;
                rangeKey = met.invoke(key, params);
                break;
            }
        }
        for (Method met : key.getClass().getDeclaredMethods()) {
            if (met.getName().equals(GET_HASH_KEY_METHOD)) {
                Object[] params = null;
                hashKey = met.invoke(key, params);
                break;
            }
        }
        if (hashKey == null)
            object = (T) mapper.load(persistentClass, key);
        if (rangeKey == null)
            object = (T) mapper.load(persistentClass, hashKey);
        else
            object = (T) mapper.load(persistentClass, hashKey, rangeKey);
        if (object == null)
            return false;
        // setting key for dynamodbMapper
        mapper.delete(object);
        return true;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) Method(java.lang.reflect.Method) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GoraException(org.apache.gora.util.GoraException)

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