下面列出了怎么用com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void init(Handler<AsyncResult<Void>> onReady) {
if (dynamoClient.isLocal()) {
logger.info("DynamoDB running locally, initializing tables.");
try {
dynamoClient.createTable(spaces.getTableName(), "id:S,owner:S,shared:N", "id", "owner,shared", "exp");
dynamoClient.createTable(packages.getTableName(), "packageName:S,spaceId:S", "packageName,spaceId", null, null);
} catch (AmazonDynamoDBException e) {
logger.error("Failure during creating tables on DynamoSpaceConfigClient init", e);
onReady.handle(Future.failedFuture(e));
return;
}
}
onReady.handle(Future.succeededFuture());
}
@Override
public void getSpace(Marker marker, String spaceId, Handler<AsyncResult<Space>> handler) {
try {
logger.info(marker, "Getting space with ID: {}", spaceId);
final Item item = spaces.getItem("id", spaceId);
if (item == null) {
logger.info(marker, "Getting space with ID: {} returned null", spaceId);
handler.handle(Future.succeededFuture(null));
return;
}
final Space space = Json.decodeValue(item.toJSON(), Space.class);
if (space != null) {
logger.info(marker, "Space ID: {} with title: \"{}\" has been decoded", spaceId, space.getTitle());
} else {
logger.info(marker, "Space ID: {} has been decoded to null", spaceId);
}
handler.handle(Future.succeededFuture(space));
} catch (AmazonDynamoDBException e) {
logger.error(marker, "Failure during getting a space from DynamoDB", e);
handler.handle(Future.failedFuture(e));
}
}
@Override
public void storeSpace(Marker marker, Space space, Handler<AsyncResult<Space>> handler) {
try {
logger.info(marker, "Storing space with ID: {}", space.getId());
final Map<String, Object> itemData = XyzSerializable.STATIC_MAPPER.get().convertValue(space, new TypeReference<Map<String, Object>>() {});
itemData.put("shared", space.isShared() ? 1 : 0); // shared value must be a number because it's also used as index
sanitize(itemData);
spaces.putItem(Item.fromMap(itemData));
deleteSpaceFromPackage(marker, space);
storeSpaceIntoPackages(marker, space);
logger.info(marker, "Space with ID: {} has been successfully stored", space.getId());
handler.handle(Future.succeededFuture(space));
} catch (AmazonDynamoDBException e) {
logger.error(marker, "Failure storing a space into DynamoDB", e);
handler.handle(Future.failedFuture(e));
}
}
@Override
public void deleteSpace(Marker marker, String spaceId, Handler<AsyncResult<Space>> handler) {
logger.info(marker, "Deleting space ID: {}", spaceId);
get(marker, spaceId, ar -> {
if (ar.succeeded()) {
try {
final Space space = ar.result();
logger.info(marker, "Space ID: {} has been retrieved", spaceId);
deleteSpaceFromPackage(marker, space);
spaces.deleteItem("id", spaceId);
handler.handle(Future.succeededFuture(space));
} catch (AmazonDynamoDBException e) {
logger.error(marker, "Failure to delete space ID: {}", spaceId);
handler.handle(Future.failedFuture(ar.cause()));
}
} else {
logger.error(marker, "Failure to get space ID: {} during space deletion", spaceId);
handler.handle(Future.failedFuture(ar.cause()));
}
});
}
/**
* Stores the relation between package name and spaces in Dynamo
*
* @param marker used in logs
* @param space the space which is being stored
*/
private void storeSpaceIntoPackages(Marker marker, Space space) throws AmazonDynamoDBException {
if (space == null) {
return;
}
try {
logger.info(marker, "Inserting packages {} into the packages table for space ID: {}", space.getPackages(), space.getId());
if (space.getPackages() != null) {
for (String packageName : space.getPackages()) {
logger.info(marker, "Adding space ID: {} into package: {}", space.getId(), packageName);
final Map<String, Object> packagesMap = new HashMap<>();
packagesMap.put("packageName", packageName);
packagesMap.put("spaceId", space.getId());
packages.putItem(Item.fromMap(packagesMap));
}
}
} catch (AmazonDynamoDBException e) {
logger.error(marker, "Failure storing space ID: {} into the packages table", space.getId(), e);
throw e;
}
}
/**
* Gets the FragmentCheckpoint item from the table for the specified stream name.
*
* @param streamName Input stream name
* @return FragmentCheckpoint entry. null if any exception is thrown.
*/
public Map<String, AttributeValue> getItem(final String streamName) {
try {
final Map<String,AttributeValue> key = new HashMap<>();
key.put(KVS_STREAM_NAME, new AttributeValue().withS(streamName));
final GetItemRequest getItemRequest = new GetItemRequest() {{
setTableName(TABLE_NAME);
setKey(key);
}};
return ddbClient.getItem(getItemRequest).getItem();
} catch (final AmazonDynamoDBException e) {
log.warn("Error while getting item from table!", e);
}
return null;
}
@Override
public void save(AnalysisOutput analysis) {
try{
analysisOutputRepository.save(analysis);
} catch(AmazonDynamoDBException addbe) {
if(isS3Use && !isDynamoDbLocal) {
log.info("Storing item in s3 since it item size exceeds more than 400kb");
ddbs3.saveLargeItem(s3BucketName, analysis.getReCiterFeature(), AnalysisOutput.class.getSimpleName() + "/" + analysis.getUid());
analysis.setReCiterFeature(null);
analysis.setUsingS3(true);
analysisOutputRepository.save(analysis);
} else if(isDynamoDbLocal){
log.info("You are running dynamodb in local mode. Add AWS access key and secret key to environment variable to enable S3 storage.");
} else {
log.info("Enable s3 use in application properties file to store larger objects. Set aws.s3.use to true and set aws.s3.dynamodb.bucketName");
}
}
}
/**
* Deletes the relationship between a space and their packages
*
* @param marker used in logs
* @param space the spaceId which is being deleted
*/
private void deleteSpaceFromPackage(Marker marker, Space space) throws AmazonDynamoDBException {
if (space == null) {
return;
}
try {
logger.info(marker, "Removing packages from space ID: {}", space.getId());
final List<String> packagesList = new ArrayList<>();
if (space.getPackages() != null) {
packagesList.addAll(space.getPackages());
} else {
final GetItemSpec spec = new GetItemSpec().withPrimaryKey("id", space.getId()).withProjectionExpression("packages");
final Item item = spaces.getItem(spec);
if (item != null && item.isPresent("packages")) {
packagesList.addAll(item.getList("packages"));
}
}
logger.info(marker, "Packages {} to be removed from space ID: {}", packagesList, space.getId());
for (String packageName : packagesList) {
packages.deleteItem("packageName", packageName, "spaceId", space.getId());
}
} catch (AmazonDynamoDBException e) {
logger.error(marker, "Failure deleting space ID: {} from the packages table", space.getId(), e);
throw e;
}
}
/**
* Creates the FragmentCheckpoint table if it doesn't exist already.
*/
public void createTableIfDoesntExist() {
// Check if table exists
if (!checkIfTableExists()) {
log.info("Creating table : {}", TABLE_NAME);
final CreateTableRequest request = new CreateTableRequest() {{
setAttributeDefinitions(
Collections.singletonList(
new AttributeDefinition(
KVS_STREAM_NAME,
ScalarAttributeType.S)));
setKeySchema(
Collections.singletonList(
new KeySchemaElement(
KVS_STREAM_NAME,
KeyType.HASH)));
setProvisionedThroughput(
new ProvisionedThroughput(1000L, 1000L));
setTableName(TABLE_NAME);
}};
try {
final CreateTableResult result = ddbClient.createTable(request);
log.info("Table created : {}", result.getTableDescription());
} catch (final AmazonDynamoDBException e) {
log.error("Error creating DDB table {}...", TABLE_NAME, e);
throw e;
}
}
}
@Override
public boolean test(Throwable throwable) {
return (throwable instanceof IOException
|| (throwable instanceof AmazonDynamoDBException)
|| (throwable instanceof AmazonDynamoDBException
&& ELIGIBLE_CODES.contains(((AmazonDynamoDBException) throwable).getStatusCode())));
}
@Test
public void testRetries() throws Throwable {
thrown.expectMessage("Error writing to DynamoDB");
final List<WriteRequest> writeRequests = DynamoDBIOTestHelper.generateWriteRequests(numOfItems);
AmazonDynamoDB amazonDynamoDBMock = Mockito.mock(AmazonDynamoDB.class);
Mockito.when(amazonDynamoDBMock.batchWriteItem(Mockito.any(BatchWriteItemRequest.class)))
.thenThrow(new AmazonDynamoDBException("Service unavailable"));
pipeline
.apply(Create.of(writeRequests))
.apply(
DynamoDBIO.<WriteRequest>write()
.withWriteRequestMapperFn(
(SerializableFunction<WriteRequest, KV<String, WriteRequest>>)
writeRequest -> KV.of(tableName, writeRequest))
.withRetryConfiguration(
DynamoDBIO.RetryConfiguration.create(4, Duration.standardSeconds(10)))
.withAwsClientsProvider(AwsClientsProviderMock.of(amazonDynamoDBMock)));
try {
pipeline.run().waitUntilFinish();
} catch (final Pipeline.PipelineExecutionException e) {
// check 3 retries were initiated by inspecting the log before passing on the exception
expectedLogs.verifyWarn(String.format(DynamoDBIO.Write.WriteFn.RETRY_ATTEMPT_LOG, 1));
expectedLogs.verifyWarn(String.format(DynamoDBIO.Write.WriteFn.RETRY_ATTEMPT_LOG, 2));
expectedLogs.verifyWarn(String.format(DynamoDBIO.Write.WriteFn.RETRY_ATTEMPT_LOG, 3));
throw e.getCause();
}
fail("Pipeline is expected to fail because we were unable to write to DynamoDB.");
}
@Test
public void testGetX509CertRecordNotFoundException() {
Mockito.doThrow(new AmazonDynamoDBException("item not found"))
.when(table).getItem("primaryKey", "athenz.provider:cn:1234");
DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
X509CertRecord certRecord = dbConn.getX509CertRecord("athenz.provider", "1234", "cn");
assertNull(certRecord);
dbConn.close();
}
@Test
public void testInsertX509RecordException() {
Date now = new Date();
X509CertRecord certRecord = getRecordNonNullableColumns(now);
Mockito.doThrow(new AmazonDynamoDBException("invalid operation"))
.when(table).putItem(any(Item.class));
DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
boolean requestSuccess = dbConn.insertX509CertRecord(certRecord);
assertFalse(requestSuccess);
dbConn.close();
}
@Test
public void testUpdateX509RecordException() {
Date now = new Date();
X509CertRecord certRecord = getRecordNonNullableColumns(now);
Mockito.doThrow(new AmazonDynamoDBException("invalid operation"))
.when(table).updateItem(any(UpdateItemSpec.class));
DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
boolean requestSuccess = dbConn.updateX509CertRecord(certRecord);
assertFalse(requestSuccess);
dbConn.close();
}
@Test
public void testDeleteX509RecordException() {
Mockito.doThrow(new AmazonDynamoDBException("invalid operation"))
.when(table).deleteItem(any(DeleteItemSpec.class));
DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
boolean requestSuccess = dbConn.deleteX509CertRecord("athenz.provider", "12345", "cn");
assertFalse(requestSuccess);
dbConn.close();
}
@Test
public void testUpdateUnrefreshedCertificatesNotificationTimestampException() {
Mockito.doThrow(new AmazonDynamoDBException("invalid operation"))
.when(table).updateItem(any(UpdateItemSpec.class));
DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
List<X509CertRecord> result = dbConn.updateUnrefreshedCertificatesNotificationTimestamp(
"serverTest",
1591706189000L,
"providerTest");
assertEquals(result.size(), 0);
dbConn.close();
}
@Test
public void testGetSSHCertRecordNotFoundException() {
Mockito.doThrow(new AmazonDynamoDBException("item not found"))
.when(table).getItem("primaryKey", "cn:1234");
DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
SSHCertRecord certRecord = dbConn.getSSHCertRecord("1234", "cn");
assertNull(certRecord);
dbConn.close();
}
@Test
public void testInsertSSHRecordException() {
SSHCertRecord certRecord = new SSHCertRecord();
Mockito.doThrow(new AmazonDynamoDBException("invalid operation"))
.when(table).putItem(ArgumentMatchers.any(Item.class));
DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
boolean requestSuccess = dbConn.insertSSHCertRecord(certRecord);
assertFalse(requestSuccess);
dbConn.close();
}
@Test
public void testUpdateSSHRecordException() {
SSHCertRecord certRecord = new SSHCertRecord();
Mockito.doThrow(new AmazonDynamoDBException("invalid operation"))
.when(table).updateItem(ArgumentMatchers.any(UpdateItemSpec.class));
DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
boolean requestSuccess = dbConn.updateSSHCertRecord(certRecord);
assertFalse(requestSuccess);
dbConn.close();
}
@Test
public void testDeleteSSHRecordException() {
Mockito.doThrow(new AmazonDynamoDBException("invalid operation"))
.when(table).deleteItem(ArgumentMatchers.any(DeleteItemSpec.class));
DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
boolean requestSuccess = dbConn.deleteSSHCertRecord("12345", "cn");
assertFalse(requestSuccess);
dbConn.close();
}
@Override
public boolean indexExists(final String indexName) throws IOException {
try {
return TableStatus.ACTIVE.name().equals(
client.describeTable(getQualifiedTableName(indexName)).getTable().getTableStatus());
} catch (final AmazonDynamoDBException e) {
LOGGER.info("Unable to check existence of table", e);
}
return false;
}
@Override
public boolean metadataExists(final MetadataType type) throws IOException {
try {
return TableStatus.ACTIVE.name().equals(
client.describeTable(getMetadataTableName(type)).getTable().getTableStatus());
} catch (final AmazonDynamoDBException e) {
LOGGER.info("Unable to check existence of table", e);
}
return false;
}
@Test
public void getNoSqlTableMetaDataAwsError() {
when(dynamoDb.describeTable(argThat((ArgumentMatcher<String>) argument -> true))).thenThrow(new AmazonDynamoDBException("provider error"));
thrown.expect(CloudConnectorException.class);
thrown.expectMessage("provider error");
underTest.getNoSqlTableMetaData(new NoSqlTableMetadataRequest());
}
@Test
public void getNoSqlTableAwsError() {
when(dynamoDb.deleteTable(argThat((ArgumentMatcher<String>) argument -> true))).thenThrow(new AmazonDynamoDBException("provider error"));
thrown.expect(CloudConnectorException.class);
thrown.expectMessage("provider error");
underTest.deleteNoSqlTable(new NoSqlTableDeleteRequest());
}
@Override
public void getSelectedSpaces(Marker marker, SpaceAuthorizationCondition authorizedCondition, SpaceSelectionCondition selectedCondition,
Handler<AsyncResult<List<Space>>> handler) {
logger.info(marker, "Getting selected spaces");
if (authorizedCondition == null || selectedCondition == null) {
throw new NullPointerException("authorizedCondition and selectedCondition are required");
}
final List<Space> result = new ArrayList<>();
logger.debug(marker, "authorizedCondition: spaceIds: {}, ownerIds {}, packages: {}", authorizedCondition.spaceIds, authorizedCondition.ownerIds, authorizedCondition.packages);
logger.debug(marker, "selectedCondition: spaceIds: {}, ownerIds {}, packages: {}, shared: {}, negateOwnerIds: {}", selectedCondition.spaceIds, selectedCondition.ownerIds, selectedCondition.packages, selectedCondition.shared, selectedCondition.negateOwnerIds);
try {
final Set<String> authorizedSpaces = getAuthorizedSpaces(marker, authorizedCondition);
// get all shared spaces if the selection for shared spaces is enabled
if (selectedCondition.shared) {
spaces.getIndex("shared-index").query(new QuerySpec().withHashKey("shared", 1).withProjectionExpression("id")).pages()
.forEach(p -> p.forEach(i -> {
authorizedSpaces.add(i.getString("id"));
}));
logger.debug(marker, "Number of space IDs after addition of shared spaces: {}", authorizedSpaces.size());
}
// filter out the ones not present in the selectedCondition (null or empty represents 'do not filter')
if (!CollectionUtils.isNullOrEmpty(selectedCondition.spaceIds)) {
authorizedSpaces.removeIf(i -> !selectedCondition.spaceIds.contains(i));
logger.debug(marker, "Number of space IDs after removal of the ones not selected by ID: {}", authorizedSpaces.size());
}
// now filter all spaceIds with the ones being selected in the selectedCondition (by checking the space's ownership) (
if (!CollectionUtils.isNullOrEmpty(selectedCondition.ownerIds)) {
final Set<String> ownersSpaces = new HashSet<>();
selectedCondition.ownerIds.forEach(o ->
spaces.getIndex("owner-index").query(new QuerySpec().withHashKey("owner", o).withProjectionExpression("id")).pages()
.forEach(p -> p.forEach(i -> ownersSpaces.add(i.getString("id")))));
// HINT: A ^ TRUE == !A (negateOwnerIds: keep or remove the spaces contained in the owner's spaces list)
authorizedSpaces.removeIf(i -> !selectedCondition.negateOwnerIds ^ ownersSpaces.contains(i));
logger.debug(marker, "Number of space IDs after removal of the ones not selected by owner: {}", authorizedSpaces.size());
}
// TODO selection per packages is not yet supported: selectedCondition.packages
logger.info(marker, "Final number of space IDs to be retrieved from DynamoDB: {}", authorizedSpaces.size());
if (!authorizedSpaces.isEmpty()) {
int batches = (int) Math.ceil((double) authorizedSpaces.size()/100);
for (int i=0; i<batches; i++) {
final TableKeysAndAttributes keys = new TableKeysAndAttributes(dynamoClient.tableName);
authorizedSpaces.stream().skip(i*100).limit(100).forEach(id -> keys.addHashOnlyPrimaryKey("id", id));
BatchGetItemOutcome outcome = dynamoClient.db.batchGetItem(keys);
processOutcome(outcome, result);
while (!outcome.getUnprocessedKeys().isEmpty()) {
outcome = dynamoClient.db.batchGetItemUnprocessed(outcome.getUnprocessedKeys());
processOutcome(outcome, result);
}
}
}
logger.info(marker, "Number of spaces retrieved from DynamoDB: {}", result.size());
handler.handle(Future.succeededFuture(result));
} catch (AmazonDynamoDBException e) {
logger.error(marker, "Failure getting authorized spaces", e);
handler.handle(Future.failedFuture(e));
}
}
private Set<String> getAuthorizedSpaces(Marker marker, SpaceAuthorizationCondition authorizedCondition) throws AmazonDynamoDBException {
final Set<String> authorizedSpaces = new LinkedHashSet<>();
logger.info(marker, "Getting authorized spaces by condition");
try {
// get the space ids which are authorized by the authorizedCondition
if (authorizedCondition.spaceIds != null) {
authorizedSpaces.addAll(authorizedCondition.spaceIds);
logger.debug(marker, "Number of space IDs after addition from authorized condition space IDs: {}", authorizedSpaces.size());
}
// then get the owners which are authorized by the authorizedCondition
if (authorizedCondition.ownerIds != null) {
authorizedCondition.ownerIds.forEach(owner ->
spaces.getIndex("owner-index").query("owner", owner).pages().forEach(p -> p.forEach(i -> {
authorizedSpaces.add(i.getString("id"));
}))
);
logger.debug(marker, "Number of space IDs after addition from owners: {}", authorizedSpaces.size());
}
// then get the packages which are authorized by the authorizedCondition
if (authorizedCondition.packages != null) {
authorizedCondition.packages.forEach(packageName ->
packages.query("packageName", packageName).pages().forEach(p -> p.forEach(i -> {
authorizedSpaces.add(i.getString("spaceId"));
}))
);
logger.debug(marker, "Number of space IDs after addition from packages: {}", authorizedSpaces.size());
}
// then get the "empty" case, when no spaceIds or ownerIds os packages are provided, meaning select ALL spaces
if (CollectionUtils.isNullOrEmpty(authorizedCondition.spaceIds)
&& CollectionUtils.isNullOrEmpty(authorizedCondition.ownerIds)
&& CollectionUtils.isNullOrEmpty(authorizedCondition.packages)) {
spaces.scan(new ScanSpec().withProjectionExpression("id")).pages()
.forEach(p -> p.forEach(i -> authorizedSpaces.add(i.getString("id"))));
}
} catch (AmazonDynamoDBException e) {
logger.error(marker, "Failure to get the authorized spaces", e);
throw e;
}
logger.info(marker, "Returning the list of authorized spaces with size of: {}", authorizedSpaces.size());
return authorizedSpaces;
}
protected Map<String, List<PropertyPair>> setupDefaultValues(Map<String, Object> properties) {
Map<String, List<PropertyPair>> res = new HashMap<String, List<PropertyPair>>();
try {
DescribeTableResult table = fetchTableDescription(properties);
StringBuilder element = new StringBuilder("{");
StringBuilder attributes = new StringBuilder();
for (AttributeDefinition attribute : table.getTable().getAttributeDefinitions()) {
if (attributes.length() > 0) {
attributes.append(", ");
}
attributes.append(attribute.getAttributeName());
if (element.length() > 1) {
element.append(", ");
}
element.append('\"');
element.append(attribute.getAttributeName());
element.append("\" : \"");
element.append(attribute.getAttributeType());
element.append('\"');
}
element.append('}');
List<PropertyPair> list = new ArrayList<PropertyPair>();
list.add(new PropertyPair(element.toString(), element.toString()));
res.put("element", list);
list = new ArrayList<PropertyPair>();
list.add(new PropertyPair(attributes.toString(), attributes.toString()));
res.put("attributes", list);
} catch (AmazonDynamoDBException t) {
LOG.error("Couldn't connect to Amazon services. No suggestions on the fields.", t);
}
return res;
}
public static void main(String[] args)
{
final String USAGE = "\n" +
"Usage:\n" +
" Query <table> <partitionkey> <partitionkeyvalue>\n\n" +
"Where:\n" +
" table - the table to put the item in.\n" +
" partitionkey - partition key name of the table.\n" +
" partitionkeyvalue - value of the partition key that should match.\n\n" +
"Example:\n" +
" Query GreetingsTable Language eng \n";
if (args.length < 3) {
System.out.println(USAGE);
System.exit(1);
}
String table_name = args[0];
String partition_key_name = args[1];
String partition_key_val = args[2];
String partition_alias = "#a";
System.out.format("Querying %s", table_name);
System.out.println("");
final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
//set up an alias for the partition key name in case it's a reserved word
HashMap<String,String> attrNameAlias =
new HashMap<String,String>();
attrNameAlias.put(partition_alias, partition_key_name);
//set up mapping of the partition name with the value
HashMap<String, AttributeValue> attrValues =
new HashMap<String,AttributeValue>();
attrValues.put(":"+partition_key_name, new AttributeValue().withS(partition_key_val));
QueryRequest queryReq = new QueryRequest()
.withTableName(table_name)
.withKeyConditionExpression(partition_alias + " = :" + partition_key_name)
.withExpressionAttributeNames(attrNameAlias)
.withExpressionAttributeValues(attrValues);
try {
QueryResult response = ddb.query(queryReq);
System.out.println(response.getCount());
} catch (AmazonDynamoDBException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Done!");
}
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);
items.setAwards(Integer.parseInt(awards)); //convert to an int
// 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();
}
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Please specify a table name");
System.exit(1);
}
// snippet-start:[dynamodb.java.dynamoDB_scan.main]
String tableName = args[0];
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
try {
ScanRequest scanRequest = new ScanRequest()
.withTableName(tableName);
ScanResult result = client.scan(scanRequest);
for (Map<String, AttributeValue> item : result.getItems()) {
Set<String> keys = item.keySet();
for (String key : keys) {
System.out.println ("The key name is "+key +"\n" );
System.out.println("The value is "+item.get(key).getS());
}
}
} catch (AmazonDynamoDBException e) {
e.getStackTrace();
}
// snippet-end:[dynamodb.java.dynamoDB_scan.main]
}