use of com.amazonaws.services.dynamodbv2.model.GetItemRequest in project jcabi-dynamo by jcabi.
the class AwsItem method has.
@Override
public boolean has(final String attr) throws IOException {
final String attrib = String.format(Locale.ENGLISH, attr);
boolean has = this.attributes.containsKey(attrib);
if (!has) {
final AmazonDynamoDB aws = this.credentials.aws();
try {
final GetItemRequest request = this.makeItemRequestFor(attr);
final long start = System.currentTimeMillis();
final GetItemResult result = aws.getItem(request);
has = result.getItem().get(attrib) != null;
Logger.info(this, "#has('%s'): %B from DynamoDB, %s, in %[ms]s", attr, has, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
} catch (final AmazonClientException ex) {
throw new IOException(String.format("Failed to check existence of \"%s\" at \"%s\" by %s", attr, this.name, this.keys), ex);
} finally {
aws.shutdown();
}
}
return has;
}
use of com.amazonaws.services.dynamodbv2.model.GetItemRequest in project jcabi-dynamo by jcabi.
the class AwsItem method makeItemRequestFor.
/**
* Makes a GetItemRequest for a given attribute.
* @param attr Attribute name
* @return GetItemRequest
*/
private GetItemRequest makeItemRequestFor(final String attr) {
final GetItemRequest request = new GetItemRequest();
request.setTableName(this.name);
request.setAttributesToGet(Collections.singletonList(attr));
request.setKey(this.attributes.only(this.keys));
request.setReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
request.setConsistentRead(true);
return request;
}
use of com.amazonaws.services.dynamodbv2.model.GetItemRequest in project aws-doc-sdk-examples by awsdocs.
the class DynamoDBDynamicFaultInjection method getItem.
/*
* Get an item from the table
*/
private static void getItem(String keyVal) {
Map<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("name", new AttributeValue(keyVal));
GetItemRequest getItemRequest = new GetItemRequest().withTableName(TABLENAME).withKey(key);
GetItemResult item = dynamoDBClient.getItem(getItemRequest);
logger.info("Get Result: " + item);
}
use of com.amazonaws.services.dynamodbv2.model.GetItemRequest in project aws-doc-sdk-examples by awsdocs.
the class LowLevelItemBinaryExample method retrieveItem.
public static void retrieveItem(String threadId, String replyDateTime) throws IOException {
HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
key.put("Id", new AttributeValue().withS(threadId));
key.put("ReplyDateTime", new AttributeValue().withS(replyDateTime));
GetItemRequest getReplyRequest = new GetItemRequest().withTableName(tableName).withKey(key).withConsistentRead(true);
GetItemResult getReplyResult = client.getItem(getReplyRequest);
// Decompress the reply message and print
Map<String, AttributeValue> reply = getReplyResult.getItem();
String message = decompressString(reply.get("ExtendedMessage").getB());
System.out.println("Reply message:\n" + " Id: " + reply.get("Id").getS() + "\n" + " ReplyDateTime: " + reply.get("ReplyDateTime").getS() + "\n" + " PostedBy: " + reply.get("PostedBy").getS() + "\n" + " Message: " + reply.get("Message").getS() + "\n" + " ExtendedMessage (decompressed): " + message);
}
use of com.amazonaws.services.dynamodbv2.model.GetItemRequest in project aws-doc-sdk-examples by awsdocs.
the class FaultInjectionRequestHandler method afterResponse.
@Override
public void afterResponse(Request<?> request, Response<?> response) {
/*
* The following is a hit and miss for multi-threaded clients as the
* cache size is only 50 entries
*/
String awsRequestId = dynamoDBClient.getCachedResponseMetadata(request.getOriginalRequest()).getRequestId();
logger.info("AWS RequestID: " + awsRequestId);
/*
* Here you could inspect and alter the response object to see how your
* application behaves for specific data
*/
if (request.getOriginalRequest() instanceof GetItemRequest) {
GetItemResult result = (GetItemResult) response.getAwsResponse();
Map<String, AttributeValue> item = result.getItem();
if (item.get("name").getS().equals("Airplane")) {
// Alter the item
item.put("name", new AttributeValue("newAirplane"));
item.put("new attr", new AttributeValue("new attr"));
// Add some delay
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
logger.info(ie);
throw new RuntimeException(ie);
}
}
}
}
Aggregations