Search in sources :

Example 1 with ScanRequest

use of software.amazon.awssdk.services.dynamodb.model.ScanRequest 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 ScanRequest

use of software.amazon.awssdk.services.dynamodb.model.ScanRequest in project syndesis-qe by syndesisio.

the class DynamoDbUtils method getNrOfRecordsPresentInTable.

public int getNrOfRecordsPresentInTable() {
    ScanRequest scanRequest = ScanRequest.builder().tableName(tableName).build();
    ScanResponse result = dynamoDb.scan(scanRequest);
    return result.items().size();
}
Also used : ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse)

Example 3 with ScanRequest

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

the class DynamoDBScanItems method scanItems.

// snippet-start:[dynamodb.java2.dynamoDB_scan.main]
public static void scanItems(DynamoDbClient ddb, String tableName) {
    try {
        ScanRequest scanRequest = ScanRequest.builder().tableName(tableName).build();
        ScanResponse response = ddb.scan(scanRequest);
        for (Map<String, AttributeValue> item : response.items()) {
            Set<String> keys = item.keySet();
            for (String key : keys) {
                System.out.println("The key name is " + key + "\n");
                System.out.println("The value is " + item.get(key).s());
            }
        }
    } catch (DynamoDbException e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DynamoDbException(software.amazon.awssdk.services.dynamodb.model.DynamoDbException) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse)

Example 4 with ScanRequest

use of software.amazon.awssdk.services.dynamodb.model.ScanRequest in project cas by apereo.

the class DynamoDbTableUtils method getRecordsByKeys.

/**
 * Gets records by keys.
 *
 * @param <T>            the type parameter
 * @param dynamoDbClient the dynamo db client
 * @param tableName      the table name
 * @param queries        the queries
 * @param itemMapper     the item mapper
 * @return the records by keys
 */
public static <T> Stream<T> getRecordsByKeys(final DynamoDbClient dynamoDbClient, final String tableName, final List<DynamoDbQueryBuilder> queries, final Function<Map<String, AttributeValue>, T> itemMapper) {
    try {
        val scanFilter = queries.stream().map(query -> {
            val cond = Condition.builder().comparisonOperator(query.getOperator()).attributeValueList(query.getAttributeValue()).build();
            return Pair.of(query.getKey(), cond);
        }).collect(Collectors.toMap(Pair::getKey, Pair::getValue));
        val scanRequest = ScanRequest.builder().tableName(tableName).scanFilter(scanFilter).build();
        LOGGER.debug("Submitting request [{}] to get record with keys [{}]", scanRequest, queries);
        val items = dynamoDbClient.scan(scanRequest).items();
        return items.stream().map(itemMapper);
    } catch (final Exception e) {
        LoggingUtils.error(LOGGER, e);
    }
    return Stream.empty();
}
Also used : lombok.val(lombok.val) Condition(software.amazon.awssdk.services.dynamodb.model.Condition) TableDescription(software.amazon.awssdk.services.dynamodb.model.TableDescription) AbstractDynamoDbProperties(org.apereo.cas.configuration.model.support.dynamodb.AbstractDynamoDbProperties) Function(java.util.function.Function) UtilityClass(lombok.experimental.UtilityClass) LoggingUtils(org.apereo.cas.util.LoggingUtils) Pair(org.apache.commons.lang3.tuple.Pair) ProvisionedThroughput(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughput) CreateTableRequest(software.amazon.awssdk.services.dynamodb.model.CreateTableRequest) ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) Map(java.util.Map) DeleteTableRequest(software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest) DynamoDbClient(software.amazon.awssdk.services.dynamodb.DynamoDbClient) TableStatus(software.amazon.awssdk.services.dynamodb.model.TableStatus) lombok.val(lombok.val) AttributeDefinition(software.amazon.awssdk.services.dynamodb.model.AttributeDefinition) Collectors(java.util.stream.Collectors) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) KeySchemaElement(software.amazon.awssdk.services.dynamodb.model.KeySchemaElement) Stream(java.util.stream.Stream) BillingMode(software.amazon.awssdk.services.dynamodb.model.BillingMode) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) DescribeTableRequest(software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) ResourceNotFoundException(software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)

Example 5 with ScanRequest

use of software.amazon.awssdk.services.dynamodb.model.ScanRequest in project syndesis-qe by syndesisio.

the class DynamoDbUtils method purgeTable.

public void purgeTable() {
    ScanRequest scanRequest = ScanRequest.builder().tableName(tableName).build();
    ScanResponse result = dynamoDb.scan(scanRequest);
    for (Map<String, AttributeValue> item : result.items()) {
        dynamoDb.deleteItem(DeleteItemRequest.builder().tableName(tableName).key(item).build());
    }
}
Also used : ScanRequest(software.amazon.awssdk.services.dynamodb.model.ScanRequest) AttributeValue(software.amazon.awssdk.services.dynamodb.model.AttributeValue) ScanResponse(software.amazon.awssdk.services.dynamodb.model.ScanResponse)

Aggregations

ScanRequest (software.amazon.awssdk.services.dynamodb.model.ScanRequest)6 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)5 ScanResponse (software.amazon.awssdk.services.dynamodb.model.ScanResponse)5 Map (java.util.Map)2 Function (java.util.function.Function)2 Flowable (io.reactivex.Flowable)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 SortedSet (java.util.SortedSet)1 TreeSet (java.util.TreeSet)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)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 Pair (org.apache.commons.lang3.tuple.Pair)1 AbstractDynamoDbProperties (org.apereo.cas.configuration.model.support.dynamodb.AbstractDynamoDbProperties)1 LoggingUtils (org.apereo.cas.util.LoggingUtils)1