Search in sources :

Example 6 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project aws-doc-sdk-examples by awsdocs.

the class EnhancedQueryRecordsWithFilter method queryTableFilter.

// snippet-start:[dynamodb.java2.mapping.queryfilter.main]
public static void queryTableFilter(DynamoDbEnhancedClient enhancedClient) {
    try {
        DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
        AttributeValue att = AttributeValue.builder().s("sblue@noserver.com").build();
        Map<String, AttributeValue> expressionValues = new HashMap<>();
        expressionValues.put(":value", att);
        Expression expression = Expression.builder().expression("email = :value").expressionValues(expressionValues).build();
        // Create a QueryConditional object that is used in the query operation.
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue("id103").build());
        // Get items in the Customer table and write out the ID value.
        Iterator<Customer> results = mappedTable.query(r -> r.queryConditional(queryConditional).filterExpression(expression)).items().iterator();
        while (results.hasNext()) {
            Customer rec = results.next();
            System.out.println("The record id is " + rec.getId());
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Done");
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException)

Example 7 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project aws-doc-sdk-examples by awsdocs.

the class Query method queryTable.

// snippet-start:[dynamodb.java2.query.main]
public static int queryTable(DynamoDbClient ddb, String tableName, String partitionKeyName, String partitionKeyVal, String partitionAlias) {
    // Set up an alias for the partition key name in case it's a reserved word
    HashMap<String, String> attrNameAlias = new HashMap<String, String>();
    attrNameAlias.put(partitionAlias, partitionKeyName);
    // Set up mapping of the partition name with the value
    HashMap<String, AttributeValue> attrValues = new HashMap<String, AttributeValue>();
    attrValues.put(":" + partitionKeyName, AttributeValue.builder().s(partitionKeyVal).build());
    QueryRequest queryReq = QueryRequest.builder().tableName(tableName).keyConditionExpression(partitionAlias + " = :" + partitionKeyName).expressionAttributeNames(attrNameAlias).expressionAttributeValues(attrValues).build();
    try {
        QueryResponse response = ddb.query(queryReq);
        return response.count();
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return -1;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) QueryRequest(software.amazon.awssdk.services.dynamodb.model.QueryRequest) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) QueryResponse(software.amazon.awssdk.services.dynamodb.model.QueryResponse)

Example 8 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project aws-doc-sdk-examples by awsdocs.

the class ScanEmployees method sendEmployeMessage.

public Boolean sendEmployeMessage() {
    Boolean send = false;
    String myDate = getDate();
    Region region = Region.US_WEST_2;
    DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
    // Create a DynamoDbEnhancedClient and use the DynamoDbClient object.
    DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
    // Create a DynamoDbTable object based on Employee.
    DynamoDbTable<Employee> table = enhancedClient.table("Employee", TableSchema.fromBean(Employee.class));
    try {
        AttributeValue attVal = AttributeValue.builder().s(myDate).build();
        // Get only items in the Employee table that match the date.
        Map<String, AttributeValue> myMap = new HashMap<>();
        myMap.put(":val1", attVal);
        Map<String, String> myExMap = new HashMap<>();
        myExMap.put("#startDate", "startDate");
        Expression expression = Expression.builder().expressionValues(myMap).expressionNames(myExMap).expression("#startDate = :val1").build();
        ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder().filterExpression(expression).limit(// you can increase this value.
        15).build();
        // Get items in the Employee table.
        Iterator<Employee> employees = table.scan(enhancedRequest).items().iterator();
        while (employees.hasNext()) {
            Employee employee = employees.next();
            String first = employee.getFirst();
            String phone = employee.getPhone();
            // Send an anniversary message.
            sentTextMessage(first, phone);
            send = true;
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return send;
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) ScanEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.ScanEnhancedRequest) Expression(software.amazon.awssdk.enhanced.dynamodb.Expression) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Region(software.amazon.awssdk.regions.Region) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 9 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project aws-doc-sdk-examples by awsdocs.

the class PutItemEncrypt method putItemInTable.

// snippet-start:[dynamodb.java2.put_item_enc.main]
public static void putItemInTable(DynamoDbClient ddb, KmsClient kmsClient, String tableName, String key, String keyVal, String albumTitle, String albumTitleValue, String awards, String awardVal, String songTitle, String songTitleVal, String keyId) {
    HashMap<String, AttributeValue> itemValues = new HashMap<String, AttributeValue>();
    // Encrypt the albumTitleValue before writing it to the table.
    SdkBytes myBytes = SdkBytes.fromUtf8String(albumTitleValue);
    EncryptRequest encryptRequest = EncryptRequest.builder().keyId(keyId).plaintext(myBytes).build();
    EncryptResponse response = kmsClient.encrypt(encryptRequest);
    // Get the encrypted data.
    SdkBytes encryptedData = response.ciphertextBlob();
    // Add content to the table.
    itemValues.put(key, AttributeValue.builder().s(keyVal).build());
    itemValues.put(songTitle, AttributeValue.builder().s(songTitleVal).build());
    itemValues.put(albumTitle, AttributeValue.builder().bs(encryptedData).build());
    itemValues.put(awards, AttributeValue.builder().s(awardVal).build());
    PutItemRequest request = PutItemRequest.builder().tableName(tableName).item(itemValues).build();
    try {
        ddb.putItem(request);
        System.out.println(tableName + " was successfully updated");
    } catch (ResourceNotFoundException e) {
        System.err.format("Error: The Amazon DynamoDB table \"%s\" can't be found.\n", tableName);
        System.err.println("Be sure that it exists and that you've typed its name correctly!");
        System.exit(1);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) SdkBytes(software.amazon.awssdk.core.SdkBytes) EncryptResponse(software.amazon.awssdk.services.kms.model.EncryptResponse) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) PutItemRequest(software.amazon.awssdk.services.dynamodb.model.PutItemRequest) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) EncryptRequest(software.amazon.awssdk.services.kms.model.EncryptRequest)

Example 10 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project aws-doc-sdk-examples by awsdocs.

the class Scenario method updateTableItem.

// snippet-end:[dynamodb.java2.scenario.populate_table.main]
// Update the record to include show only directors.
public static void updateTableItem(DynamoDbClient ddb, String tableName) {
    HashMap<String, AttributeValue> itemKey = new HashMap<String, AttributeValue>();
    // Specify the key and sort key.
    itemKey.put("year", AttributeValue.builder().n("1933").build());
    itemKey.put("title", AttributeValue.builder().s("King Kong").build());
    HashMap<String, AttributeValueUpdate> updatedValues = new HashMap<String, AttributeValueUpdate>();
    // Update the column specified by info with updatedVal.
    updatedValues.put("info", AttributeValueUpdate.builder().value(AttributeValue.builder().s("{\"directors\":[\"Merian C. Cooper\",\"Ernest B. Schoedsack\"]").build()).action(AttributeAction.PUT).build());
    UpdateItemRequest request = UpdateItemRequest.builder().tableName(tableName).key(itemKey).attributeUpdates(updatedValues).build();
    try {
        ddb.updateItem(request);
    } catch (ResourceNotFoundException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    System.out.println("Item was updated!");
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) UpdateItemRequest(software.amazon.awssdk.services.dynamodb.model.UpdateItemRequest) AttributeValueUpdate(software.amazon.awssdk.services.dynamodb.model.AttributeValueUpdate) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)

Aggregations

AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)31 HashMap (java.util.HashMap)14 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)13 Test (org.junit.Test)12 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10 Map (java.util.Map)6 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)5 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)5 ScanRequest (software.amazon.awssdk.services.dynamodb.model.ScanRequest)5 Collectors (java.util.stream.Collectors)4 PutItemRequest (software.amazon.awssdk.services.dynamodb.model.PutItemRequest)4 List (java.util.List)3 Slf4j (lombok.extern.slf4j.Slf4j)3 lombok.val (lombok.val)3 AttributeDefinition (software.amazon.awssdk.services.dynamodb.model.AttributeDefinition)3 KeySchemaElement (software.amazon.awssdk.services.dynamodb.model.KeySchemaElement)3 ScanResponse (software.amazon.awssdk.services.dynamodb.model.ScanResponse)3 Set (java.util.Set)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2