Search in sources :

Example 1 with GetItemRequest

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;
}
Also used : GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) AmazonClientException(com.amazonaws.AmazonClientException) ToString(lombok.ToString) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 2 with GetItemRequest

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;
}
Also used : GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 3 with GetItemRequest

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);
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) HashMap(java.util.HashMap) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 4 with GetItemRequest

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);
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) HashMap(java.util.HashMap) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 5 with GetItemRequest

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);
            }
        }
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Aggregations

GetItemRequest (com.amazonaws.services.dynamodbv2.model.GetItemRequest)10 GetItemResult (com.amazonaws.services.dynamodbv2.model.GetItemResult)8 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)7 HashMap (java.util.HashMap)5 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AmazonServiceException (com.amazonaws.AmazonServiceException)2 IOException (java.io.IOException)2 ToString (lombok.ToString)2 ClientConfig (com.amazon.dax.client.dynamodbv2.ClientConfig)1 ClusterDaxAsyncClient (com.amazon.dax.client.dynamodbv2.ClusterDaxAsyncClient)1 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 AsyncHandler (com.amazonaws.handlers.AsyncHandler)1 AmazonDynamoDBAsync (com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync)1 NoSuchElementException (java.util.NoSuchElementException)1 ExecutionException (java.util.concurrent.ExecutionException)1