use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.
the class DynamoDBService method archiveItemEC.
// Update the archive column by using the Enhanced Client.
public String archiveItemEC(String id) {
DynamoDbClient ddb = getClient();
try {
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(getClient()).build();
DynamoDbTable<Work> workTable = enhancedClient.table("Work", TableSchema.fromBean(Work.class));
// Get the Key object.
Key key = Key.builder().partitionValue(id).build();
// Get the item by using the key.
Work work = workTable.getItem(r -> r.key(key));
work.setArchive("Closed");
workTable.updateItem(r -> r.item(work));
return "The item was successfully archived";
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.
the class DynamoDBService method UpdateItem.
// Updates items in the Work Table.
public String UpdateItem(String id, String status) {
DynamoDbClient ddb = getClient();
HashMap<String, AttributeValue> itemKey = new HashMap<String, AttributeValue>();
itemKey.put("id", AttributeValue.builder().s(id).build());
HashMap<String, AttributeValueUpdate> updatedValues = new HashMap<String, AttributeValueUpdate>();
// Update the column specified by name with updatedVal.
updatedValues.put("status", AttributeValueUpdate.builder().value(AttributeValue.builder().s(status).build()).action(AttributeAction.PUT).build());
UpdateItemRequest request = UpdateItemRequest.builder().tableName("Work").key(itemKey).attributeUpdates(updatedValues).build();
try {
ddb.updateItem(request);
return "The Status for the the item was successfully updated";
} catch (ResourceNotFoundException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return "";
}
use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.
the class DynamoDBService method persistItem.
// Persist the PPE Items into the Gear table.
public void persistItem(List<ArrayList<GearItem>> gearList) {
DynamoDbClient ddb = getClient();
try {
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
DynamoDbTable<Gear> gearTable = enhancedClient.table("Gear", TableSchema.fromBean(Gear.class));
Gear gearRecord;
// Create an Instant.
// current date and time
LocalDateTime now = LocalDateTime.now();
LocalDateTime timeVal = now.toLocalDate().atStartOfDay();
Instant instant = timeVal.toInstant(ZoneOffset.UTC);
// Persist the data into a DynamoDB table.
for (Object o : gearList) {
// Need to get the WorkItem from each list.
List innerList = (List) o;
for (Object value : innerList) {
gearRecord = new Gear();
UUID uuid = UUID.randomUUID();
GearItem gearItem = (GearItem) value;
gearRecord.setId(uuid.toString());
gearRecord.setKey(gearItem.getKey());
gearRecord.setDate(instant.toString());
gearRecord.setItem(gearItem.getName());
gearRecord.setCoverDescription(gearItem.getBodyCoverDescription());
gearRecord.setItemDescription(gearItem.getItemDescription());
gearRecord.setConfidence(gearItem.getConfidence());
// Put PPE data into a DynamoDB table.
gearTable.putItem(gearRecord);
}
}
} catch (DynamoDbException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-doc-sdk-examples by awsdocs.
the class DynamoDBEnhanced method injectDynamoItem.
// Uses the Enhanced Client to inject a new post into a DynamoDB table
public void injectDynamoItem(Greeting item) {
Region region = Region.US_EAST_1;
DynamoDbClient ddb = DynamoDbClient.builder().region(region).credentialsProvider(EnvironmentVariableCredentialsProvider.create()).build();
try {
DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(ddb).build();
// Create a DynamoDbTable object
DynamoDbTable<GreetingItems> mappedTable = enhancedClient.table("Greeting", TableSchema.fromBean(GreetingItems.class));
GreetingItems gi = new GreetingItems();
gi.setName(item.getName());
gi.setMessage(item.getBody());
gi.setTitle(item.getTitle());
gi.setId(item.getId());
PutItemEnhancedRequest enReq = PutItemEnhancedRequest.builder(GreetingItems.class).item(gi).build();
mappedTable.putItem(enReq);
} catch (Exception e) {
e.getStackTrace();
}
}
use of software.amazon.awssdk.services.dynamodb.DynamoDbClient in project aws-xray-sdk-java by aws.
the class TracingInterceptorTest method testResponseDescriptors.
@Test
public void testResponseDescriptors() throws Exception {
String responseBody = "{\"LastEvaluatedTableName\":\"baz\",\"TableNames\":[\"foo\",\"bar\",\"baz\"]}";
SdkHttpResponse mockResponse = SdkHttpResponse.builder().statusCode(200).putHeader("x-amzn-requestid", "1111-2222-3333-4444").putHeader("Content-Length", "84").putHeader("Content-Type", "application/x-amz-json-1.0").build();
SdkHttpClient mockClient = mockSdkHttpClient(mockResponse, responseBody);
DynamoDbClient client = DynamoDbClient.builder().httpClient(mockClient).endpointOverride(URI.create("http://example.com")).region(Region.of("us-west-42")).credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create("key", "secret", "session"))).overrideConfiguration(ClientOverrideConfiguration.builder().addExecutionInterceptor(new TracingInterceptor()).build()).build();
Segment segment = AWSXRay.getCurrentSegment();
client.listTables(ListTablesRequest.builder().limit(3).build());
Assert.assertEquals(1, segment.getSubsegments().size());
Subsegment subsegment = segment.getSubsegments().get(0);
Map<String, Object> awsStats = subsegment.getAws();
@SuppressWarnings("unchecked") Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");
Assert.assertEquals("ListTables", awsStats.get("operation"));
Assert.assertEquals(3, awsStats.get("limit"));
Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
Assert.assertEquals(3, awsStats.get("table_count"));
Assert.assertEquals("us-west-42", awsStats.get("region"));
Assert.assertEquals(0, awsStats.get("retries"));
Assert.assertEquals(84L, httpResponseStats.get("content_length"));
Assert.assertEquals(200, httpResponseStats.get("status"));
Assert.assertEquals(false, subsegment.isInProgress());
}
Aggregations