Search in sources :

Example 1 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project iep by Netflix.

the class PaginationTest method dynamoDB.

@Test
public void dynamoDB() throws Exception {
    Map<String, AttributeValue> nextPage = new HashMap<>();
    nextPage.put("abc", AttributeValue.builder().build());
    Map<String, AttributeValue> donePage = new HashMap<>();
    Function<ScanRequest, ScanResponse> f = r -> {
        if (r.exclusiveStartKey() != null) {
            Assert.assertTrue(r.exclusiveStartKey().containsKey("abc"));
        }
        return ScanResponse.builder().lastEvaluatedKey((r.exclusiveStartKey() == null) ? nextPage : donePage).build();
    };
    Publisher<ScanResponse> publisher = Pagination.createPublisher(ScanRequest.builder().build(), f);
    Iterable<ScanResponse> iter = Flowable.fromPublisher(publisher).blockingIterable();
    int count = 0;
    for (ScanResponse r : iter) {
        ++count;
    }
    Assert.assertEquals(2, count);
}
Also used : SortedSet(java.util.SortedSet) ListResourceRecordSetsResponse(software.amazon.awssdk.services.route53.model.ListResourceRecordSetsResponse) DescribeTargetGroupsRequest(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeTargetGroupsRequest) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) Function(java.util.function.Function) TreeSet(java.util.TreeSet) ListMetricsRequest(software.amazon.awssdk.services.cloudwatch.model.ListMetricsRequest) ListHostedZonesResponse(software.amazon.awssdk.services.route53.model.ListHostedZonesResponse) DescribeAutoScalingGroupsResponse(software.amazon.awssdk.services.autoscaling.model.DescribeAutoScalingGroupsResponse) ListClustersRequest(software.amazon.awssdk.services.emr.model.ListClustersRequest) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) DescribeInstancesResponse(software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse) Map(java.util.Map) DescribeInstancesRequest(software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest) PutMetricDataRequest(software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest) Iterator(java.util.Iterator) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse) Publisher(org.reactivestreams.Publisher) ListClustersResponse(software.amazon.awssdk.services.emr.model.ListClustersResponse) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) DescribeTargetGroupsResponse(software.amazon.awssdk.services.elasticloadbalancingv2.model.DescribeTargetGroupsResponse) PutMetricDataResponse(software.amazon.awssdk.services.cloudwatch.model.PutMetricDataResponse) ListHostedZonesRequest(software.amazon.awssdk.services.route53.model.ListHostedZonesRequest) DescribeAutoScalingGroupsRequest(software.amazon.awssdk.services.autoscaling.model.DescribeAutoScalingGroupsRequest) ListResourceRecordSetsRequest(software.amazon.awssdk.services.route53.model.ListResourceRecordSetsRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DescribeLoadBalancersRequest(software.amazon.awssdk.services.autoscaling.model.DescribeLoadBalancersRequest) Assert(org.junit.Assert) DescribeLoadBalancersResponse(software.amazon.awssdk.services.autoscaling.model.DescribeLoadBalancersResponse) ListMetricsResponse(software.amazon.awssdk.services.cloudwatch.model.ListMetricsResponse) ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse) Test(org.junit.Test)

Example 2 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project beam by apache.

the class AttributeValueCoderTest method shouldPassForOneListOfByteArray.

@Test
public void shouldPassForOneListOfByteArray() throws IOException {
    AttributeValue expected = AttributeValue.builder().bs(ImmutableList.of(SdkBytes.fromByteArray("mylistbyte1".getBytes(StandardCharsets.UTF_8)), SdkBytes.fromByteArray("mylistbyte2".getBytes(StandardCharsets.UTF_8)))).build();
    AttributeValueCoder coder = AttributeValueCoder.of();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    coder.encode(expected, output);
    ByteArrayInputStream in = new ByteArrayInputStream(output.toByteArray());
    AttributeValue actual = coder.decode(in);
    Assert.assertEquals(expected, actual);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with AttributeValue

use of software.amazon.awssdk.services.dynamodb.model.AttributeValue in project beam by apache.

the class AttributeValueCoderTest method shouldPassForNumberType.

@Test
public void shouldPassForNumberType() throws IOException {
    AttributeValue expected = AttributeValue.builder().n("123").build();
    AttributeValueCoder coder = AttributeValueCoder.of();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    coder.encode(expected, output);
    ByteArrayInputStream in = new ByteArrayInputStream(output.toByteArray());
    AttributeValue actual = coder.decode(in);
    Assert.assertEquals(expected, actual);
}
Also used : AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 4 with AttributeValue

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

the class DynamoDBAsyncGetItem method getItem.

// snippet-start:[dynamoasyc.java2.get_item.main]
public static void getItem(DynamoDbAsyncClient client, String tableName, String key, String keyVal) {
    HashMap<String, AttributeValue> keyToGet = new HashMap<String, AttributeValue>();
    keyToGet.put(key, AttributeValue.builder().s(keyVal).build());
    try {
        // Create a GetItemRequest instance
        GetItemRequest request = GetItemRequest.builder().key(keyToGet).tableName(tableName).build();
        // Invoke the DynamoDbAsyncClient object's getItem
        java.util.Collection<AttributeValue> returnedItem = client.getItem(request).join().item().values();
        // Convert Set to Map
        Map<String, AttributeValue> map = returnedItem.stream().collect(Collectors.toMap(AttributeValue::s, s -> s));
        Set<String> keys = map.keySet();
        for (String sinKey : keys) {
            System.out.format("%s: %s\n", sinKey, map.get(sinKey).toString());
        }
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
// snippet-end:[dynamoasyc.java2.get_item.main]
}
Also used : DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest) Map(java.util.Map) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) Set(java.util.Set) HashMap(java.util.HashMap) Region(software.amazon.awssdk.regions.Region) Collectors(java.util.stream.Collectors) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) HashMap(java.util.HashMap) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) GetItemRequest(software.amazon.awssdk.services.dynamodb.model.GetItemRequest)

Example 5 with AttributeValue

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

the class EnhancedQueryRecordsWithSortKey method queryTableSort.

// snippet-start:[dynamodb.java2.mapping.querykey.main]
public static void queryTableSort(DynamoDbEnhancedClient enhancedClient) {
    try {
        // Create a DynamoDbTable object based on Issues.
        DynamoDbTable<Issues> table = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class));
        String dateVal = "2013-11-19";
        DynamoDbIndex<Issues> secIndex = enhancedClient.table("Issues", TableSchema.fromBean(Issues.class)).index("dueDateIndex");
        AttributeValue attVal = AttributeValue.builder().s(dateVal).build();
        // Create a QueryConditional object that's used in the query operation.
        QueryConditional queryConditional = QueryConditional.keyEqualTo(Key.builder().partitionValue(attVal).build());
        // Get items in the Issues table.
        SdkIterable<Page<Issues>> results = secIndex.query(QueryEnhancedRequest.builder().queryConditional(queryConditional).build());
        AtomicInteger atomicInteger = new AtomicInteger();
        atomicInteger.set(0);
        results.forEach(page -> {
            Issues issue = (Issues) page.items().get(atomicInteger.get());
            System.out.println("The issue title is " + issue.getTitle());
            atomicInteger.incrementAndGet();
        });
    } 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) QueryConditional(software.amazon.awssdk.enhanced.dynamodb.model.QueryConditional) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) Page(software.amazon.awssdk.enhanced.dynamodb.model.Page)

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