Search in sources :

Example 36 with DynamoDbClient

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 "";
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient)

Example 37 with DynamoDbClient

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 "";
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient)

Example 38 with DynamoDbClient

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);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Instant(java.time.Instant) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 39 with DynamoDbClient

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();
    }
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) Region(software.amazon.awssdk.regions.Region) PutItemEnhancedRequest(software.amazon.awssdk.enhanced.dynamodb.model.PutItemEnhancedRequest) DynamoDbEnhancedClient(software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)

Example 40 with DynamoDbClient

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());
}
Also used : DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) SdkHttpResponse(software.amazon.awssdk.http.SdkHttpResponse) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Subsegment(com.amazonaws.xray.entities.Subsegment) Map(java.util.Map) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Aggregations

DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)40 Region (software.amazon.awssdk.regions.Region)32 DynamoDbEnhancedClient (software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient)13 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)5 Instant (java.time.Instant)2 LocalDateTime (java.time.LocalDateTime)2 List (java.util.List)2 Map (java.util.Map)2 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)2 Segment (com.amazonaws.xray.entities.Segment)1 Subsegment (com.amazonaws.xray.entities.Subsegment)1 LocalDate (java.time.LocalDate)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 UtilityClass (lombok.experimental.UtilityClass)1 Slf4j (lombok.extern.slf4j.Slf4j)1 lombok.val (lombok.val)1