Search in sources :

Example 11 with DynamoDBMapper

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

the class UseDynamoMapping method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "To run this example, supply the following values: \n" + "artist name \n" + "song title \n" + "album title \n" + "number of awards \n";
    if (args.length < 4) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String artist = args[0];
    String songTitle = args[1];
    String albumTitle = args[2];
    String awards = args[3];
    // snippet-start:[dynamodb.java.dynamoDB_mapping.main]
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
    MusicItems items = new MusicItems();
    try {
        // Add new content to the Music table
        items.setArtist(artist);
        items.setSongTitle(songTitle);
        items.setAlbumTitle(albumTitle);
        // convert to an int
        items.setAwards(Integer.parseInt(awards));
        // Save the item
        DynamoDBMapper mapper = new DynamoDBMapper(client);
        mapper.save(items);
        // Load an item based on the Partition Key and Sort Key
        // Both values need to be passed to the mapper.load method
        String artistName = artist;
        String songQueryTitle = songTitle;
        // Retrieve the item
        MusicItems itemRetrieved = mapper.load(MusicItems.class, artistName, songQueryTitle);
        System.out.println("Item retrieved:");
        System.out.println(itemRetrieved);
        // Modify the Award value
        itemRetrieved.setAwards(2);
        mapper.save(itemRetrieved);
        System.out.println("Item updated:");
        System.out.println(itemRetrieved);
        System.out.print("Done");
    } catch (AmazonDynamoDBException e) {
        e.getStackTrace();
    }
}
Also used : AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 12 with DynamoDBMapper

use of com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper in project java-design-patterns by iluwatar.

the class AbstractDynamoDbHandler method initAmazonDynamoDb.

private void initAmazonDynamoDb() {
    var amazonDynamoDb = AmazonDynamoDBClientBuilder.standard().withRegion(Regions.US_EAST_1).build();
    this.dynamoDbMapper = new DynamoDBMapper(amazonDynamoDb);
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)

Example 13 with DynamoDBMapper

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

the class DynamoDBNativeStore method get.

@Override
public /**
 * Gets the object with the specific key
 * @throws IOException
 */
T get(K key) throws GoraException {
    T object = null;
    try {
        Object rangeKey;
        rangeKey = getRangeKeyFromKey(key);
        Object hashKey = getHashFromKey(key);
        if (hashKey != null) {
            DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
            if (rangeKey != null)
                object = mapper.load(persistentClass, hashKey, rangeKey);
            else
                object = mapper.load(persistentClass, hashKey);
            return object;
        } else {
            throw new GoraException("Error while retrieving keys from object: " + key.toString());
        }
    } catch (GoraException e) {
        throw e;
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GoraException(org.apache.gora.util.GoraException)

Example 14 with DynamoDBMapper

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

the class DynamoDBNativeStore method put.

/**
 * Puts an object identified by a key
 *
 * @param key
 * @param obj
 */
@Override
public void put(K key, T obj) throws GoraException {
    try {
        Object hashKey = getHashKey(key, obj);
        Object rangeKey = getRangeKey(key, obj);
        if (hashKey != null) {
            DynamoDBMapper mapper = new DynamoDBMapper(dynamoDBStoreHandler.getDynamoDbClient());
            if (rangeKey != null) {
                mapper.load(persistentClass, hashKey, rangeKey);
            } else {
                mapper.load(persistentClass, hashKey);
            }
            mapper.save(obj);
        } else
            throw new GoraException("No HashKey found in Key nor in Object.");
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : GoraException(org.apache.gora.util.GoraException) DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GoraException(org.apache.gora.util.GoraException)

Example 15 with DynamoDBMapper

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

the class DynamoDBPersistenceService method query.

/**
     * {@inheritDoc}
     */
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    logger.debug("got a query");
    if (!isProperlyConfigured) {
        logger.warn("Configuration for dynamodb not yet loaded or broken. Not storing item.");
        return Collections.emptyList();
    }
    if (!maybeConnectAndCheckConnection()) {
        logger.warn("DynamoDB not connected. Not storing item.");
        return Collections.emptyList();
    }
    String itemName = filter.getItemName();
    Item item = getItemFromRegistry(itemName);
    if (item == null) {
        logger.warn("Could not get item {} from registry!", itemName);
        return Collections.emptyList();
    }
    Class<DynamoDBItem<?>> dtoClass = AbstractDynamoDBItem.getDynamoItemClass(item.getClass());
    String tableName = tableNameResolver.fromClass(dtoClass);
    DynamoDBMapper mapper = getDBMapper(tableName);
    logger.debug("item {} (class {}) will be tried to query using dto class {} from table {}", itemName, item.getClass(), dtoClass, tableName);
    List<HistoricItem> historicItems = new ArrayList<HistoricItem>();
    DynamoDBQueryExpression<DynamoDBItem<?>> queryExpression = createQueryExpression(dtoClass, filter);
    @SuppressWarnings("rawtypes") final PaginatedQueryList<? extends DynamoDBItem> paginatedList;
    try {
        paginatedList = mapper.query(dtoClass, queryExpression);
    } catch (AmazonServiceException e) {
        logger.error("DynamoDB query raised unexpected exception: {}. Returning empty collection. " + "Status code 400 (resource not found) might occur if table was just created.", e.getMessage());
        return Collections.emptyList();
    }
    for (int itemIndexOnPage = 0; itemIndexOnPage < filter.getPageSize(); itemIndexOnPage++) {
        int itemIndex = filter.getPageNumber() * filter.getPageSize() + itemIndexOnPage;
        DynamoDBItem<?> dynamoItem;
        try {
            dynamoItem = paginatedList.get(itemIndex);
        } catch (IndexOutOfBoundsException e) {
            logger.debug("Index {} is out-of-bounds", itemIndex);
            break;
        }
        if (dynamoItem != null) {
            HistoricItem historicItem = dynamoItem.asHistoricItem(item);
            logger.trace("Dynamo item {} converted to historic item: {}", item, historicItem);
            historicItems.add(historicItem);
        }
    }
    return historicItems;
}
Also used : DynamoDBMapper(com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper) ArrayList(java.util.ArrayList) HistoricItem(org.openhab.core.persistence.HistoricItem) Item(org.openhab.core.items.Item) AmazonServiceException(com.amazonaws.AmazonServiceException) HistoricItem(org.openhab.core.persistence.HistoricItem)

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