Search in sources :

Example 66 with Item

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

the class LowLevelQuery method findRepliesInLast15DaysWithConfig.

private static void findRepliesInLast15DaysWithConfig(String forumName, String threadSubject) {
    long twoWeeksAgoMilli = (new Date()).getTime() - (15L * 24L * 60L * 60L * 1000L);
    Date twoWeeksAgo = new Date();
    twoWeeksAgo.setTime(twoWeeksAgoMilli);
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    String twoWeeksAgoStr = df.format(twoWeeksAgo);
    Condition sortKeyCondition = new Condition().withComparisonOperator(ComparisonOperator.GT.toString()).withAttributeValueList(new AttributeValue().withS(twoWeeksAgoStr));
    Map<String, Condition> keyConditions = makeReplyKeyConditions(forumName, threadSubject);
    keyConditions.put("ReplyDateTime", sortKeyCondition);
    QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withKeyConditions(keyConditions).withProjectionExpression("Message, ReplyDateTime, PostedBy");
    QueryResult result = client.query(queryRequest);
    for (Map<String, AttributeValue> item : result.getItems()) {
        printItem(item);
    }
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 67 with Item

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

the class LowLevelQuery method findRepliesUsingAFilterExpression.

private static void findRepliesUsingAFilterExpression(String forumName, String threadSubject) {
    Map<String, Condition> keyConditions = makeReplyKeyConditions(forumName, threadSubject);
    Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
    expressionAttributeValues.put(":val", new AttributeValue().withS("User B"));
    QueryRequest queryRequest = new QueryRequest().withTableName(tableName).withKeyConditions(keyConditions).withFilterExpression("PostedBy = :val").withExpressionAttributeValues(expressionAttributeValues).withProjectionExpression("Message, ReplyDateTime, PostedBy");
    QueryResult result = client.query(queryRequest);
    for (Map<String, AttributeValue> item : result.getItems()) {
        printItem(item);
    }
}
Also used : Condition(com.amazonaws.services.dynamodbv2.model.Condition) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) QueryResult(com.amazonaws.services.dynamodbv2.model.QueryResult) QueryRequest(com.amazonaws.services.dynamodbv2.model.QueryRequest) HashMap(java.util.HashMap)

Example 68 with Item

use of com.amazonaws.services.dynamodbv2.document.Item 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)

Example 69 with Item

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

the class MoviesItemOps02 method main.

public static void main(String[] args) throws Exception {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.getTable("Movies");
    int year = 2015;
    String title = "The Big New Movie";
    GetItemSpec spec = new GetItemSpec().withPrimaryKey("year", year, "title", title);
    try {
        System.out.println("Attempting to read the item...");
        Item outcome = table.getItem(spec);
        System.out.println("GetItem succeeded: " + outcome);
    } catch (Exception e) {
        System.err.println("Unable to read item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) Table(com.amazonaws.services.dynamodbv2.document.Table) GetItemSpec(com.amazonaws.services.dynamodbv2.document.spec.GetItemSpec) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Example 70 with Item

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

the class MoviesItemOps06 method main.

public static void main(String[] args) throws Exception {
    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2")).build();
    DynamoDB dynamoDB = new DynamoDB(client);
    Table table = dynamoDB.getTable("Movies");
    int year = 2015;
    String title = "The Big New Movie";
    DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey(new PrimaryKey("year", year, "title", title)).withConditionExpression("info.rating <= :val").withValueMap(new ValueMap().withNumber(":val", 5.0));
    try {
        System.out.println("Attempting a conditional delete...");
        table.deleteItem(deleteItemSpec);
        System.out.println("DeleteItem succeeded");
    } catch (Exception e) {
        System.err.println("Unable to delete item: " + year + " " + title);
        System.err.println(e.getMessage());
    }
}
Also used : DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec) Table(com.amazonaws.services.dynamodbv2.document.Table) ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) DynamoDB(com.amazonaws.services.dynamodbv2.document.DynamoDB) PrimaryKey(com.amazonaws.services.dynamodbv2.document.PrimaryKey) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)107 Item (com.amazonaws.services.dynamodbv2.document.Item)95 HashMap (java.util.HashMap)60 Table (com.amazonaws.services.dynamodbv2.document.Table)53 Test (org.testng.annotations.Test)49 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)42 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)35 AmazonServiceException (com.amazonaws.AmazonServiceException)31 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)27 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)26 ArrayList (java.util.ArrayList)25 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)24 List (java.util.List)23 Test (org.junit.Test)23 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)22 IOException (java.io.IOException)21 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)20 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)18 Date (java.util.Date)18 Map (java.util.Map)17