Search in sources :

Example 6 with GetItemRequest

use of com.amazonaws.services.dynamodbv2.model.GetItemRequest in project aws-doc-sdk-examples by awsdocs.

the class DaxAsyncClientDemo method main.

public static void main(String[] args) throws Exception {
    ClientConfig daxConfig = new ClientConfig().withCredentialsProvider(new ProfileCredentialsProvider()).withEndpoints("mydaxcluster.2cmrwl.clustercfg.dax.use1.cache.amazonaws.com:8111");
    AmazonDynamoDBAsync client = new ClusterDaxAsyncClient(daxConfig);
    HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
    key.put("Artist", new AttributeValue().withS("No One You Know"));
    key.put("SongTitle", new AttributeValue().withS("Scared of My Shadow"));
    GetItemRequest request = new GetItemRequest().withTableName("Music").withKey(key);
    // Java Futures
    Future<GetItemResult> call = client.getItemAsync(request);
    while (!call.isDone()) {
        // Do other processing while you're waiting for the response
        System.out.println("Doing something else for a few seconds...");
        Thread.sleep(3000);
    }
    try {
        call.get();
    } catch (ExecutionException ee) {
        // Futures always wrap errors as an ExecutionException.
        // The *real* exception is stored as the cause of the
        // ExecutionException
        Throwable exception = ee.getCause();
        System.out.println("Error getting item: " + exception.getMessage());
    }
    // Async callbacks
    call = client.getItemAsync(request, new AsyncHandler<GetItemRequest, GetItemResult>() {

        @Override
        public void onSuccess(GetItemRequest request, GetItemResult getItemResult) {
            System.out.println("Result: " + getItemResult);
        }

        @Override
        public void onError(Exception e) {
            System.out.println("Unable to read item");
            System.err.println(e.getMessage());
        // Callers can also test if exception is an instance of
        // AmazonServiceException or AmazonClientException and cast
        // it to get additional information
        }
    });
    call.get();
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) AsyncHandler(com.amazonaws.handlers.AsyncHandler) HashMap(java.util.HashMap) ClusterDaxAsyncClient(com.amazon.dax.client.dynamodbv2.ClusterDaxAsyncClient) ExecutionException(java.util.concurrent.ExecutionException) GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) AmazonDynamoDBAsync(com.amazonaws.services.dynamodbv2.AmazonDynamoDBAsync) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) ClientConfig(com.amazon.dax.client.dynamodbv2.ClientConfig) ExecutionException(java.util.concurrent.ExecutionException) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 7 with GetItemRequest

use of com.amazonaws.services.dynamodbv2.model.GetItemRequest in project camel by apache.

the class GetItemCommand method execute.

@Override
public void execute() {
    GetItemResult result = ddbClient.getItem(new GetItemRequest().withKey(determineKey()).withTableName(determineTableName()).withAttributesToGet(determineAttributeNames()).withConsistentRead(determineConsistentRead()));
    addAttributesToResult(result.getItem());
}
Also used : GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 8 with GetItemRequest

use of com.amazonaws.services.dynamodbv2.model.GetItemRequest in project jcabi-dynamo by jcabi.

the class AwsItem method get.

@Override
public AttributeValue get(final String attr) throws IOException {
    final String attrib = String.format(Locale.ENGLISH, attr);
    AttributeValue value = this.attributes.get(attrib);
    if (value == null) {
        final AmazonDynamoDB aws = this.credentials.aws();
        try {
            final GetItemRequest request = this.makeItemRequestFor(attrib);
            final long start = System.currentTimeMillis();
            final GetItemResult result = aws.getItem(request);
            value = result.getItem().get(attrib);
            Logger.info(this, // @checkstyle LineLength (1 line)
            "#get('%s'): loaded '%[text]s' from DynamoDB, %s, in %[ms]s", attrib, value, new PrintableConsumedCapacity(result.getConsumedCapacity()).print(), System.currentTimeMillis() - start);
        } catch (final AmazonClientException ex) {
            throw new IOException(String.format("Failed to get \"%s\" from \"%s\" by %s", attr, this.name, this.keys), ex);
        } finally {
            aws.shutdown();
        }
    }
    if (value == null) {
        throw new NoSuchElementException(String.format("attribute \"%s\" not found", attr));
    }
    return value;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) AmazonClientException(com.amazonaws.AmazonClientException) ToString(lombok.ToString) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 9 with GetItemRequest

use of com.amazonaws.services.dynamodbv2.model.GetItemRequest in project aws-doc-sdk-examples by awsdocs.

the class GetItem method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "    GetItem <table> <name> [projection_expression]\n\n" + "Where:\n" + "    table - the table to get an item from.\n" + "    name  - the item to get.\n\n" + "You can add an optional projection expression (a quote-delimited,\n" + "comma-separated list of attributes to retrieve) to limit the\n" + "fields returned from the table.\n\n" + "Example:\n" + "    GetItem HelloTable World\n" + "    GetItem SiteColors text \"default, bold\"\n";
    if (args.length < 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String table_name = args[0];
    String name = args[1];
    String projection_expression = null;
    // if a projection expression was included, set it.
    if (args.length == 3) {
        projection_expression = args[2];
    }
    System.out.format("Retrieving item \"%s\" from \"%s\"\n", name, table_name);
    HashMap<String, AttributeValue> key_to_get = new HashMap<String, AttributeValue>();
    key_to_get.put("DATABASE_NAME", new AttributeValue(name));
    GetItemRequest request = null;
    if (projection_expression != null) {
        request = new GetItemRequest().withKey(key_to_get).withTableName(table_name).withProjectionExpression(projection_expression);
    } else {
        request = new GetItemRequest().withKey(key_to_get).withTableName(table_name);
    }
    final AmazonDynamoDB ddb = AmazonDynamoDBClientBuilder.defaultClient();
    try {
        Map<String, AttributeValue> returned_item = ddb.getItem(request).getItem();
        if (returned_item != null) {
            Set<String> keys = returned_item.keySet();
            for (String key : keys) {
                System.out.format("%s: %s\n", key, returned_item.get(key).toString());
            }
        } else {
            System.out.format("No item found with the key %s!\n", name);
        }
    } catch (AmazonServiceException e) {
        System.err.println(e.getErrorMessage());
        System.exit(1);
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest)

Example 10 with GetItemRequest

use of com.amazonaws.services.dynamodbv2.model.GetItemRequest in project aws-doc-sdk-examples by awsdocs.

the class LowLevelItemCRUDExample method retrieveItem.

private static void retrieveItem() {
    try {
        HashMap<String, AttributeValue> key = new HashMap<String, AttributeValue>();
        key.put("Id", new AttributeValue().withN("120"));
        GetItemRequest getItemRequest = new GetItemRequest().withTableName(tableName).withKey(key).withProjectionExpression("Id, ISBN, Title, Authors");
        GetItemResult result = client.getItem(getItemRequest);
        // Check the response.
        System.out.println("Printing item after retrieving it....");
        printItem(result.getItem());
    } catch (AmazonServiceException ase) {
        System.err.println("Failed to retrieve item in " + tableName);
    }
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) GetItemResult(com.amazonaws.services.dynamodbv2.model.GetItemResult) HashMap(java.util.HashMap) AmazonServiceException(com.amazonaws.AmazonServiceException) 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