Java Code Examples for com.amazonaws.services.dynamodbv2.model.GetItemResult#getItem()

The following examples show how to use com.amazonaws.services.dynamodbv2.model.GetItemResult#getItem() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LowLevelItemBinaryExample.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: DynamoDBWorkerUtils.java    From aws-dynamodb-mars-json-demo with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the stored ETag, if one exists, from DynamoDB.
 *
 * @param dynamoDB
 *            DynamoDB client configured with a region and credentials
 * @param table
 *            The resource table name
 * @param resource
 *            The URL String of the resource
 * @return The ETag String of the last copy processed or null if the resource has never been processed
 */
public static String getStoredETag(final AmazonDynamoDB dynamoDB, final String table, final String resource) {
    String oldETag;
    // Build key to retrieve item
    final Map<String, AttributeValue> resourceKey = new HashMap<String, AttributeValue>();
    resourceKey.put(MarsDynamoDBManager.RESOURCE_TABLE_HASH_KEY, new AttributeValue(resource));
    // Get item
    final GetItemResult result = dynamoDB.getItem(table, resourceKey);
    final Map<String, AttributeValue> item = result.getItem();
    if (item != null && item.containsKey(ETAG_KEY)) {
        // Item was found and contains ETag
        oldETag = item.get(ETAG_KEY).getS();
    } else {
        // Item was not found or did not contain ETag
        oldETag = null;
    }
    return oldETag;
}
 
Example 3
Source File: LowLevelItemBinaryExample.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: FaultInjectionRequestHandler.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
@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);
            }
        }
    }
}
 
Example 5
Source File: DynamoDbSingleRowStore.java    From dynamodb-janusgraph-storage-backend with Apache License 2.0 5 votes vote down vote up
private EntryList extractEntriesFromGetItemResult(final GetItemResult result, final StaticBuffer sliceStart, final StaticBuffer sliceEnd, final int limit) {
    final Map<String, AttributeValue> item = result.getItem();
    List<Entry> filteredEntries = Collections.emptyList();
    if (null != item) {
        item.remove(Constants.JANUSGRAPH_HASH_KEY);
        filteredEntries = new EntryBuilder(item)
                .slice(sliceStart, sliceEnd)
                .limit(limit)
                .buildAll();
    }
    return StaticArrayEntryList.of(filteredEntries);
}
 
Example 6
Source File: FaultInjectionRequestHandler.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
@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);
            }
        }
    }
}
 
Example 7
Source File: IntegrationTest.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
protected Map<String, AttributeValue> getItem(String tableName, Map<String, AttributeValue> key) {
    GetItemResult result = dynamodb.getItem(new GetItemRequest()
        .withTableName(tableName)
        .withKey(key)
        .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
        .withConsistentRead(true));
    return result.getItem();
}
 
Example 8
Source File: Transaction.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
protected static Map<String, AttributeValue> getItem(TransactionManager txManager, String tableName, Map<String, AttributeValue> key) {
    GetItemRequest getRequest = new GetItemRequest()
        .withTableName(tableName)
        .withConsistentRead(true)
        .withKey(key);
    GetItemResult getResult = txManager.getClient().getItem(getRequest);
    return getResult.getItem();
}
 
Example 9
Source File: TransactionDynamoDBFacade.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
private void checkExpectedValues(String tableName,
        Map<String, AttributeValue> itemKey,
        Map<String, ExpectedAttributeValue> expectedValues) {
    if (expectedValues != null && !expectedValues.isEmpty()) {
        for (Map.Entry<String, ExpectedAttributeValue> entry : expectedValues.entrySet()) {
            if ((entry.getValue().isExists() == null || entry.getValue().isExists() == true)
                    && entry.getValue().getValue() == null) {
                throw new IllegalArgumentException("An explicit value is required when Exists is null or true, "
                        + "but none was found in expected values for item with key " + itemKey +
                        ": " + expectedValues);
            }
        }

        // simulate by loading the item and checking the values;
        // this also has the effect of locking the item, which gives the
        // same behavior
        GetItemResult result = getItem(new GetItemRequest()
                .withAttributesToGet(expectedValues.keySet())
                .withKey(itemKey)
                .withTableName(tableName));
        Map<String, AttributeValue> item = result.getItem();
        try {
            checkExpectedValues(expectedValues, item);
        } catch (ConditionalCheckFailedException e) {
            throw new ConditionalCheckFailedException("Item " + itemKey + " had unexpected attributes: " + e.getMessage());
        }
    }
}