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();
}
}
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);
}
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);
}
}
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);
}
}
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;
}
Aggregations