use of software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient in project aws-doc-sdk-examples by awsdocs.
the class PersistCase method putRecord.
// Puts an item into a DynamoDB table
public void putRecord(String caseId, String employeeName, String email) {
// Create a DynamoDbClient object
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();
try {
// Create a DynamoDbTable object
DynamoDbTable<Case> caseTable = enhancedClient.table("Case", TableSchema.fromBean(Case.class));
// Create an Instant object
LocalDate localDate = LocalDate.parse("2020-04-07");
LocalDateTime localDateTime = localDate.atStartOfDay();
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
// Populate the table
Case caseRecord = new Case();
caseRecord.setName(employeeName);
caseRecord.setId(caseId);
caseRecord.setEmail(email);
caseRecord.setRegistrationDate(instant);
// Put the case data into a DynamoDB table
caseTable.putItem(caseRecord);
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
System.out.println("done");
}
use of software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient 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.enhanced.dynamodb.DynamoDbEnhancedClient in project aws-doc-sdk-examples by awsdocs.
the class EnhancedBatchWriteItems method putBatchRecords.
// snippet-start:[dynamodb.java2.mapping.batchitems.main]
public static void putBatchRecords(DynamoDbEnhancedClient enhancedClient) {
try {
DynamoDbTable<Customer> mappedTable = enhancedClient.table("Customer", TableSchema.fromBean(Customer.class));
LocalDate localDate = LocalDate.parse("2020-04-07");
LocalDateTime localDateTime = localDate.atStartOfDay();
Instant instant = localDateTime.toInstant(ZoneOffset.UTC);
Customer record2 = new Customer();
record2.setCustName("Fred Pink");
record2.setId("id110");
record2.setEmail("fredp@noserver.com");
record2.setRegistrationDate(instant);
Customer record3 = new Customer();
record3.setCustName("Susan Pink");
record3.setId("id120");
record3.setEmail("spink@noserver.com");
record3.setRegistrationDate(instant);
// Create a BatchWriteItemEnhancedRequest object
BatchWriteItemEnhancedRequest batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest.builder().writeBatches(WriteBatch.builder(Customer.class).mappedTableResource(mappedTable).addPutItem(r -> r.item(record2)).addPutItem(r -> r.item(record3)).build()).build();
// Add these two items to the table
enhancedClient.batchWriteItem(batchWriteItemEnhancedRequest);
System.out.println("done");
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient in project aws-doc-sdk-examples by awsdocs.
the class EnhancedBatchWriteItems method main.
public static void main(String[] args) {
Region region = Region.US_EAST_1;
DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
putBatchRecords(enhancedClient);
ddb.close();
}
use of software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient in project aws-doc-sdk-examples by awsdocs.
the class EnhancedScanRecordsWithExpression method scanIndex.
// snippet-start:[dynamodb.java2.mapping.scanEx.main]
// Scan the table and retrieve only items where createDate is 2013-11-15.
public static void scanIndex(DynamoDbClient ddb, String tableName, String indexName) {
System.out.println("\n***********************************************************\n");
System.out.print("Select items for " + tableName + " where createDate is 2013-11-15!");
try {
// Create a DynamoDbEnhancedClient and use the DynamoDbClient object.
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
// Create a DynamoDbTable object based on Issues.
DynamoDbTable<Issues> table = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class));
// Setup the scan based on the index.
if (indexName == "CreateDateIndex") {
System.out.println("Issues filed on 2013-11-15");
AttributeValue attVal = AttributeValue.builder().s("2013-11-15").build();
// Get only items in the Issues table for 2013-11-15.
Map<String, AttributeValue> myMap = new HashMap<>();
myMap.put(":val1", attVal);
Map<String, String> myExMap = new HashMap<>();
myExMap.put("#createDate", "createDate");
Expression expression = Expression.builder().expressionValues(myMap).expressionNames(myExMap).expression("#createDate = :val1").build();
ScanEnhancedRequest enhancedRequest = ScanEnhancedRequest.builder().filterExpression(expression).limit(15).build();
// Get items in the Issues table.
Iterator<Issues> results = table.scan(enhancedRequest).items().iterator();
while (results.hasNext()) {
Issues issue = results.next();
System.out.println("The record description is " + issue.getDescription());
System.out.println("The record title is " + issue.getTitle());
}
}
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
Aggregations