com.amazonaws.services.dynamodbv2.document.spec.ScanSpec Java Examples

The following examples show how to use com.amazonaws.services.dynamodbv2.document.spec.ScanSpec. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MoviesScan.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {

        AmazonDynamoDBClient client = new AmazonDynamoDBClient();
        client.setEndpoint("http://localhost:8000");
        DynamoDB dynamoDB = new DynamoDB(client);
   
        Table table = dynamoDB.getTable("Movies");
        
        ScanSpec scanSpec = new ScanSpec()
            .withProjectionExpression("#yr, title, info.rating")
            .withFilterExpression("#yr between :start_yr and :end_yr")
            .withNameMap(new NameMap().with("#yr",  "year"))
            .withValueMap(new ValueMap().withNumber(":start_yr", 1950).withNumber(":end_yr", 1959));
        
        ItemCollection<ScanOutcome> items = table.scan(scanSpec);
        
        Iterator<Item> iter = items.iterator();
        while (iter.hasNext()) {
            Item item = iter.next();
            System.out.println(item.toString());
        }
    }
 
Example #2
Source File: DynamoDBServiceImpl1.java    From Serverless-Programming-Cookbook with MIT License 5 votes vote down vote up
@Override
public final Response scan(final Request request) {
    final Table table = dynamoDB.getTable(request.getTableName());

    final String projectionExpression = request.getPartitionKey() + " , " + request.getSortKey();
    final ScanSpec scanSpec = new ScanSpec()
            .withProjectionExpression(projectionExpression);

    StringBuilder filterExpressionBuilder;
    Map<String, Object> valueMap;
    if (request.getFilterData() != null) {
        filterExpressionBuilder = new StringBuilder();
        valueMap = new HashMap<>();
        processFilterData(request, filterExpressionBuilder, valueMap);
        // Add to ScanSpec.
        scanSpec.withFilterExpression(filterExpressionBuilder.toString());
        scanSpec.withValueMap(valueMap);
    }

    ItemCollection<ScanOutcome> scanItems = table.scan(scanSpec);

    final StringBuilder response = new StringBuilder();
    response.append("PK of items read with scan (V1): ");
    for (Item item : scanItems) {
        response.append(prepareKeyStr(item, request));
    }

    return new Response(response.toString(), null);
}
 
Example #3
Source File: MoviesScan.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://localhost:8000", "us-west-2"))
            .build();

        DynamoDB dynamoDB = new DynamoDB(client);

        Table table = dynamoDB.getTable("Movies");

        ScanSpec scanSpec = new ScanSpec().withProjectionExpression("#yr, title, info.rating")
            .withFilterExpression("#yr between :start_yr and :end_yr").withNameMap(new NameMap().with("#yr", "year"))
            .withValueMap(new ValueMap().withNumber(":start_yr", 1950).withNumber(":end_yr", 1959));

        try {
            ItemCollection<ScanOutcome> items = table.scan(scanSpec);

            Iterator<Item> iter = items.iterator();
            while (iter.hasNext()) {
                Item item = iter.next();
                System.out.println(item.toString());
            }

        }
        catch (Exception e) {
            System.err.println("Unable to scan the table:");
            System.err.println(e.getMessage());
        }
    }
 
Example #4
Source File: DocumentAPIParallelScan.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    System.out.println("Scanning " + tableName + " segment " + segment + " out of " + totalSegments
        + " segments " + itemLimit + " items at a time...");
    int totalScannedItemCount = 0;

    Table table = dynamoDB.getTable(tableName);

    try {
        ScanSpec spec = new ScanSpec().withMaxResultSize(itemLimit).withTotalSegments(totalSegments)
            .withSegment(segment);

        ItemCollection<ScanOutcome> items = table.scan(spec);
        Iterator<Item> iterator = items.iterator();

        Item currentItem = null;
        while (iterator.hasNext()) {
            totalScannedItemCount++;
            currentItem = iterator.next();
            System.out.println(currentItem.toString());
        }

    }
    catch (Exception e) {
        System.err.println(e.getMessage());
    }
    finally {
        System.out.println("Scanned " + totalScannedItemCount + " items from segment " + segment + " out of "
            + totalSegments + " of " + tableName);
    }
}
 
Example #5
Source File: DocumentAPIParallelScan.java    From aws-dynamodb-examples with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    System.out.println("Scanning " + tableName + " segment " + segment + " out of " + totalSegments + " segments " + itemLimit + " items at a time...");
    int totalScannedItemCount = 0;

    Table table = dynamoDB.getTable(tableName);
    
    try {
        ScanSpec spec = new ScanSpec()
            .withMaxResultSize(itemLimit)
            .withTotalSegments(totalSegments)
            .withSegment(segment);
        
        ItemCollection<ScanOutcome> items = table.scan(spec);
        Iterator<Item> iterator = items.iterator();
          
        Item currentItem = null;
        while (iterator.hasNext()) {
            totalScannedItemCount++;
            currentItem = iterator.next();
            System.out.println(currentItem.toString());
        }    
            
    } catch (Exception e) {
        System.err.println(e.getMessage());
    } finally {
        System.out.println("Scanned " + totalScannedItemCount 
            + " items from segment " + segment + " out of " 
            + totalSegments + " of " + tableName);
    }
}
 
Example #6
Source File: DynamoSpaceConfigClient.java    From xyz-hub with Apache License 2.0 4 votes vote down vote up
private Set<String> getAuthorizedSpaces(Marker marker, SpaceAuthorizationCondition authorizedCondition) throws AmazonDynamoDBException {
  final Set<String> authorizedSpaces = new LinkedHashSet<>();

  logger.info(marker, "Getting authorized spaces by condition");

  try {
    // get the space ids which are authorized by the authorizedCondition
    if (authorizedCondition.spaceIds != null) {
      authorizedSpaces.addAll(authorizedCondition.spaceIds);
      logger.debug(marker, "Number of space IDs after addition from authorized condition space IDs: {}", authorizedSpaces.size());
    }

    // then get the owners which are authorized by the authorizedCondition
    if (authorizedCondition.ownerIds != null) {
      authorizedCondition.ownerIds.forEach(owner ->
          spaces.getIndex("owner-index").query("owner", owner).pages().forEach(p -> p.forEach(i -> {
            authorizedSpaces.add(i.getString("id"));
          }))
      );
      logger.debug(marker, "Number of space IDs after addition from owners: {}", authorizedSpaces.size());
    }

    // then get the packages which are authorized by the authorizedCondition
    if (authorizedCondition.packages != null) {
      authorizedCondition.packages.forEach(packageName ->
          packages.query("packageName", packageName).pages().forEach(p -> p.forEach(i -> {
            authorizedSpaces.add(i.getString("spaceId"));
          }))
      );
      logger.debug(marker, "Number of space IDs after addition from packages: {}", authorizedSpaces.size());
    }

    // then get the "empty" case, when no spaceIds or ownerIds os packages are provided, meaning select ALL spaces
    if (CollectionUtils.isNullOrEmpty(authorizedCondition.spaceIds)
        && CollectionUtils.isNullOrEmpty(authorizedCondition.ownerIds)
        && CollectionUtils.isNullOrEmpty(authorizedCondition.packages)) {
      spaces.scan(new ScanSpec().withProjectionExpression("id")).pages()
          .forEach(p -> p.forEach(i -> authorizedSpaces.add(i.getString("id"))));
    }
  } catch (AmazonDynamoDBException e) {
    logger.error(marker, "Failure to get the authorized spaces", e);
    throw e;
  }

  logger.info(marker, "Returning the list of authorized spaces with size of: {}", authorizedSpaces.size());
  return authorizedSpaces;
}
 
Example #7
Source File: DynamoDBLockProviderIntegrationTest.java    From ShedLock with Apache License 2.0 4 votes vote down vote up
@AfterEach
public void truncateLockTable() {
    for (Item item : lockTable.scan(new ScanSpec())) {
        lockTable.deleteItem(new PrimaryKey(ID, item.getString(ID)));
    }
}