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");
}
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;
}
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;
}
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);
}
}
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!");
}
Aggregations