Java Code Examples for com.amazonaws.services.dynamodbv2.model.ScanResult#getItems()

The following examples show how to use com.amazonaws.services.dynamodbv2.model.ScanResult#getItems() . 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: ScanRecordReadRequest.java    From emr-dynamodb-connector with Apache License 2.0 6 votes vote down vote up
@Override
protected PageResults<Map<String, AttributeValue>> fetchPage(RequestLimit lim) {
  // Read from DynamoDB
  RetryResult<ScanResult> retryResult = context.getClient().scanTable(tableName, null, segment,
      context.getSplit().getTotalSegments(), lastEvaluatedKey, lim.items, context.getReporter());

  ScanResult result = retryResult.result;
  int retries = retryResult.retries;

  double consumedCapacityUnits = 0.0;
  if (result.getConsumedCapacity() != null) {
    consumedCapacityUnits = result.getConsumedCapacity().getCapacityUnits();
  }
  return new PageResults<>(result.getItems(), result.getLastEvaluatedKey(), consumedCapacityUnits,
      retries);
}
 
Example 2
Source File: UserAuthentication.java    From reinvent2013-mobile-photo-share with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the list of usernames stored in the identity table.
 * 
 * @return list of existing usernames in DynamoDB table
 */
public List<String> listUsers() {
    List<String> users = new ArrayList<String>(1000);

    ScanResult result = ddb.scan(new ScanRequest().withTableName(USER_TABLE).withLimit(1000));

    for (Map<String, AttributeValue> item : result.getItems()) {
        String s = "";

        for (Entry<String, AttributeValue> entry : item.entrySet()) {
            s += " ** " + entry.getKey() + " = " + entry.getValue().getS();
        }

        users.add(s);
    }

    return users;
}
 
Example 3
Source File: DeviceAuthentication.java    From reinvent2013-mobile-photo-share with Apache License 2.0 6 votes vote down vote up
/**
 * @return the list of device ID (UID) stored in the identity table.
 */
public List<String> listDevices() {
    List<String> devices = new ArrayList<String>(1000);

    ScanResult result = ddb.scan(new ScanRequest().withTableName(DEVICE_TABLE).withLimit(1000));

    for (Map<String, AttributeValue> item : result.getItems()) {
        String s = "";

        for (Entry<String, AttributeValue> entry : item.entrySet()) {
            s += " ** " + entry.getKey() + " = " + entry.getValue().getS();
        }

        devices.add(s);
    }

    return devices;
}
 
Example 4
Source File: TransformerHolisticTests.java    From aws-dynamodb-encryption-java with Apache License 2.0 6 votes vote down vote up
private void dumpTables() {
    for (String table : client.listTables().getTableNames()) {
        ScanResult scanResult;
        Map<String, AttributeValue> lastKey = null;
        do {
            scanResult = client.scan(new ScanRequest().withTableName(table).withExclusiveStartKey(lastKey));
            lastKey = scanResult.getLastEvaluatedKey();
            for (Map<String, AttributeValue> map : scanResult.getItems()) {
                for (Map.Entry<String, AttributeValue> item : map.entrySet()) {
                    System.out.print("item.put(\"");
                    System.out.print(item.getKey());
                    System.out.print("\", b642Av(\"");
                    System.out.print(Base64.encodeAsString(AttributeValueMarshaller.marshall(item.getValue()).array()));
                    System.out.println("\"));");
                }
                System.out.print("ddb.putItem(new PutItemRequest(\"");
                System.out.print(table);
                System.out.println("\", item));");
                System.out.println("item.clear();");
                System.out.println();
            }
        } while (lastKey != null);

    }
}
 
Example 5
Source File: UserAuthentication.java    From amazon-cognito-developer-authentication-sample with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the list of usernames stored in the identity table.
 * 
 * @return list of existing usernames in DynamoDB table
 */
public List<String> listUsers() {
    List<String> users = new ArrayList<String>(1000);

    ScanResult result = ddb.scan(new ScanRequest().withTableName(USER_TABLE).withLimit(1000));

    for (Map<String, AttributeValue> item : result.getItems()) {
        String s = "";

        for (Entry<String, AttributeValue> entry : item.entrySet()) {
            s += " ** " + entry.getKey() + " = " + entry.getValue().getS();
        }

        users.add(s);
    }

    return users;
}
 
Example 6
Source File: LowLevelScan.java    From aws-dynamodb-examples with Apache License 2.0 6 votes vote down vote up
private static void findProductsForPriceLessThanZero() {
    
    Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
    expressionAttributeValues.put(":pr", new AttributeValue().withN("100"));
    
    ScanRequest scanRequest = new ScanRequest()
        .withTableName(tableName)
        .withFilterExpression("Price < :pr")
        .withExpressionAttributeValues(expressionAttributeValues)
        .withProjectionExpression("Id, Title, ProductCategory, Price");

    ScanResult result = client.scan(scanRequest);
    
    System.out.println("Scan of " + tableName + " for items with a price less than 100.");
    for (Map<String, AttributeValue> item : result.getItems()) {
        System.out.println("");
        printItem(item);
    }
}
 
Example 7
Source File: TransactionExamples.java    From dynamodb-transactions with Apache License 2.0 5 votes vote down vote up
public void sweepForStuckAndOldTransactions() {
    print("\n*** sweepForStuckAndOldTransactions() ***\n");
    
    // The scan should be done in a loop to follow the LastEvaluatedKey, and done with following the best practices for scanning a table.
    // This includes sleeping between pages, using Limit to limit the throughput of each operation to avoid hotspots,
    // and using parallel scan.
    print("Scanning one full page of the transactions table");
    ScanResult result = dynamodb.scan(new ScanRequest()
        .withTableName(TX_TABLE_NAME));
    
    // Pick some duration where transactions should be rolled back if they were sitting there PENDING.
    // 
    //long rollbackAfterDurationMills = 5 * 60 * 1000; // Must be idle and PENDING for 5 minutes to be rolled back
    //long deleteAfterDurationMillis = 24 * 60 * 60 * 1000; // Must be completed for 24 hours to be deleted
    long rollbackAfterDurationMills = 1;
    long deleteAfterDurationMillis = 1;
    for(Map<String, AttributeValue> txItem : result.getItems()) {
        print("Sweeping transaction " + txItem);
        try {
            if(TransactionManager.isTransactionItem(txItem)) {
                Transaction t = txManager.resumeTransaction(txItem);    
                t.sweep(rollbackAfterDurationMills, deleteAfterDurationMillis);
                print("  - Swept transaction (but it might have been skipped)");
            }
        } catch (TransactionException e) {
            // Log and report an error "unsticking" this transaction, but keep going.
            print("  - Error sweeping transaction " + txItem + " " + e);
        }
    }
    
}
 
Example 8
Source File: DDBTableUtils.java    From aws-athena-query-federation with Apache License 2.0 5 votes vote down vote up
/**
 * Derives an Arrow {@link Schema} for the given table by performing a small table scan and mapping the returned
 * attribute values' types to Arrow types. If the table is empty, only attributes found in the table's metadata
 * are added to the return schema.
 *
 * @param tableName the table to derive a schema for
 * @param invoker the ThrottlingInvoker to call DDB with
 * @param ddbClient the DDB client to use
 * @return the table's derived schema
 */
public static Schema peekTableForSchema(String tableName, ThrottlingInvoker invoker, AmazonDynamoDB ddbClient)
        throws TimeoutException
{
    ScanRequest scanRequest = new ScanRequest().withTableName(tableName).withLimit(SCHEMA_INFERENCE_NUM_RECORDS);
    ScanResult scanResult = invoker.invoke(() -> ddbClient.scan(scanRequest));
    List<Map<String, AttributeValue>> items = scanResult.getItems();
    Set<String> discoveredColumns = new HashSet<>();
    SchemaBuilder schemaBuilder = new SchemaBuilder();
    if (!items.isEmpty()) {
        for (Map<String, AttributeValue> item : items) {
            for (Map.Entry<String, AttributeValue> column : item.entrySet()) {
                if (!discoveredColumns.contains(column.getKey())) {
                    Field field = DDBTypeUtils.inferArrowField(column.getKey(), ItemUtils.toSimpleValue(column.getValue()));
                    if (field != null) {
                        schemaBuilder.addField(field);
                        discoveredColumns.add(column.getKey());
                    }
                }
            }
        }
    }
    else {
        // there's no items, so use any attributes defined in the table metadata
        DynamoDBTable table = getTable(tableName, invoker, ddbClient);
        for (AttributeDefinition attributeDefinition : table.getKnownAttributeDefinitions()) {
            schemaBuilder.addField(DDBTypeUtils.getArrowFieldFromDDBType(attributeDefinition.getAttributeName(), attributeDefinition.getAttributeType()));
        }
    }
    return schemaBuilder.build();
}
 
Example 9
Source File: ListUsers.java    From reinvent2013-mobile-photo-share with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the list of usernames stored in the user table.
 */
public List<String> listUsers(String userTable) {
    List<String> users = new ArrayList<String>(1000);

    ScanResult result = ddb.scan(new ScanRequest().withTableName(userTable).withLimit(1000));

    for (Map<String, AttributeValue> item : result.getItems()) {
        users.add(item.get("username").getS());
    }

    return users;
}
 
Example 10
Source File: DynamoDBService2.java    From Doradus with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> getRows(String storeName, String continuationToken, int count) {
	Timer t = new Timer();
	ScanRequest scanRequest = new ScanRequest(getTenant().getName());
    scanRequest.setAttributesToGet(Arrays.asList("key")); // attributes to get
    //if (continuationToken != null) {
    //	scanRequest.setExclusiveStartKey(getPrimaryKey(storeName + "_" + continuationToken, "\u007F"));
    //} else {
    //	scanRequest.setExclusiveStartKey(getPrimaryKey(storeName + "_", "\0"));
    //}

    Set<String> rowKeys = new HashSet<>();
    while (rowKeys.size() < count) {
        ScanResult scanResult = m_client.scan(scanRequest);
        List<Map<String, AttributeValue>> itemList = scanResult.getItems();
        if (itemList.size() == 0) break;
        for (Map<String, AttributeValue> attributeMap : itemList) {
            AttributeValue rowAttr = attributeMap.get("key");
            if(!rowAttr.getS().startsWith(storeName)) continue;
            String name = rowAttr.getS().substring(storeName.length() + 1);
            if(continuationToken != null && continuationToken.compareTo(name) >= 0) continue;
            rowKeys.add(name);
        }
        Map<String, AttributeValue> lastEvaluatedKey = scanResult.getLastEvaluatedKey();
        if (lastEvaluatedKey == null) break;
    	scanRequest.setExclusiveStartKey(getPrimaryKey(lastEvaluatedKey.get("key").getS(), "\u007F"));
    }
    List<String> list = new ArrayList<>(rowKeys);
    Collections.sort(list);
	m_logger.debug("get rows in {} in {}", storeName, t);
    return list;
}
 
Example 11
Source File: DynamoDBService.java    From Doradus with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> getRows(String storeName, String continuationToken, int count) {
    String tableName = storeToTableName(storeName);
    ScanRequest scanRequest = new ScanRequest(tableName);
    scanRequest.setAttributesToGet(Arrays.asList(ROW_KEY_ATTR_NAME)); // attributes to get
    if (continuationToken != null) {
        scanRequest.setExclusiveStartKey(makeDDBKey(continuationToken));
    }
    List<String> rowKeys = new ArrayList<>();
    while (rowKeys.size() < count) {
        ScanResult scanResult = scan(scanRequest);
        List<Map<String, AttributeValue>> itemList = scanResult.getItems();
        if (itemList.size() == 0) {
            break;
        }
        for (Map<String, AttributeValue> attributeMap : itemList) {
            AttributeValue rowAttr = attributeMap.get(ROW_KEY_ATTR_NAME);
            rowKeys.add(rowAttr.getS());
            if (rowKeys.size() >= count) {
                break;
            }
        }
        Map<String, AttributeValue> lastEvaluatedKey = scanResult.getLastEvaluatedKey();
        if (lastEvaluatedKey == null) {
            break;
        }
        scanRequest.setExclusiveStartKey(lastEvaluatedKey);
    }
    return rowKeys;
}
 
Example 12
Source File: BlockingQueueWorker.java    From dynamodb-import-export-tool with Apache License 2.0 5 votes vote down vote up
@Override
public Void call() {
    final ScanResult scanResult = result.getScanResult();
    final List<Map<String, AttributeValue>> items = scanResult.getItems();
    final Iterator<Map<String, AttributeValue>> it = items.iterator();
    boolean interrupted = false;
    try {
        do {
            try {
                Map<String, AttributeValue> item = it.next();
                DynamoDBEntryWithSize entryWithSize = new DynamoDBEntryWithSize(
                        item,
                        ItemSizeCalculator.calculateItemSizeInBytes(item));
                queue.put(entryWithSize);
            } catch (InterruptedException e) {
                interrupted = true;
                LOGGER.warn("interrupted when writing item to queue: "
                        + e.getMessage());
            }
        } while (it.hasNext());
    } finally {
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
    }
    return null;
}
 
Example 13
Source File: BookGetAll.java    From serverless with Apache License 2.0 5 votes vote down vote up
@Override
public String handleRequest(Book request, Context context) {
	ScanRequest scanRequest = new ScanRequest().withTableName("Books");
	ScanResult result = DynamoDBUtil.getClient().scan(scanRequest);
    System.out.println("-- books listing start --");
	for (Map<String, AttributeValue> item : result.getItems()){
	    System.out.println(item);
	}
    System.out.println("-- books listing end --");
    return result.getItems().toString();
}
 
Example 14
Source File: DynamoDBIOTestHelper.java    From beam with Apache License 2.0 5 votes vote down vote up
static List<Map<String, AttributeValue>> generateTestData(String tableName, int numOfItems) {
  BatchWriteItemRequest batchWriteItemRequest =
      generateBatchWriteItemRequest(tableName, numOfItems);

  dynamoDBClient.batchWriteItem(batchWriteItemRequest);
  ScanResult scanResult = dynamoDBClient.scan(new ScanRequest().withTableName(tableName));

  List<Map<String, AttributeValue>> items = scanResult.getItems();
  Assert.assertEquals(numOfItems, items.size());
  return items;
}
 
Example 15
Source File: DynamoDBIO.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public List<Map<String, AttributeValue>> apply(@Nullable ScanResult scanResult) {
  if (scanResult == null) {
    return Collections.emptyList();
  }
  return scanResult.getItems();
}
 
Example 16
Source File: DynamoDBServiceImpl2.java    From Serverless-Programming-Cookbook with MIT License 5 votes vote down vote up
@Override
public final Response scan(final Request request) {

    final String projectionExpression = request.getPartitionKey() + ", " + request.getSortKey();
    ScanRequest scanRequest = new ScanRequest()
            .withTableName(request.getTableName())
            .withProjectionExpression(projectionExpression);

    StringBuilder filterExpressionBuilder;
    Map<String, AttributeValue> expressionAttributeValues;
    if (request.getFilterData() != null) {
        filterExpressionBuilder = new StringBuilder();
        expressionAttributeValues = new HashMap<>();
        processFilterData(request, filterExpressionBuilder, expressionAttributeValues);
        // Add to ScanRequest.
        scanRequest.withFilterExpression(filterExpressionBuilder.toString());
        scanRequest.withExpressionAttributeValues(expressionAttributeValues);
    }



    final ScanResult scanResult = dynamoDBClient.scan(scanRequest);

    final StringBuilder response = new StringBuilder();
    response.append("PK of items read with scan (V2): ");
    for (Map<String, AttributeValue> item : scanResult.getItems()) {
        response.append(prepareKeyStr(item, request));
    }

    return new Response(response.toString(), null);
}
 
Example 17
Source File: MultiRowParallelScanInterpreter.java    From dynamodb-janusgraph-storage-backend with Apache License 2.0 4 votes vote down vote up
/**
 * This class relies heavily on the behavior of segmented scans with respect to which hash keys are scanned by each segment.
 * Here's a rough ASCII example to help illustrate:
 *  ___________________________
 * |hk:A         |hk:B         |
 * ----------------------------
 * ^segment 1        ^segment 2
 *
 * Because we are scanning in segments across the entire hash key space, it is possible for the same hash key to appear in two different segments.
 * We are also running all of the scan segments in parallel, so we have no control over which segment returns first.
 *
 * In the example, if segment 2 was the first segment to post a result, we would store hk:B as a "boundary" key. That way when
 * segment 1 eventually reaches hk:B in its scan, we know that another segment has already returned this hash key and we can safely skip returning it.
 *
 * By doing this, we avoid returning a RecordIterator for the same hash key twice and we only need to store at most 2 hash keys per segment.
 *
 */
@Override
public List<SingleKeyRecordIterator> buildRecordIterators(final ScanContext scanContext) {
    final ScanResult dynamoDbResult = scanContext.getScanResult();
    final int segment = scanContext.getScanRequest().getSegment();
    final List<Map<String, AttributeValue>> items = dynamoDbResult.getItems();
    // If the scan returned no results, we need to shortcut and just throw back an empty result set
    if (items.isEmpty()) {
        return Collections.emptyList();
    }

    final List<SingleKeyRecordIterator> recordIterators = Lists.newLinkedList();

    final Iterator<Map<String, AttributeValue>> itemIterator = items.iterator();
    final Map<String, AttributeValue> firstItem = itemIterator.next();
    final StaticBuffer firstKey = new KeyBuilder(firstItem).build(Constants.JANUSGRAPH_HASH_KEY);

    // Computes the full set of boundary keys up to this point. This includes the previous end key for this segment.
    final ImmutableSet<StaticBuffer> boundaryKeys = aggregateBoundaryKeys();

    // The first key in this scan segment might already have been returned by a previous scan segment
    if (!boundaryKeys.contains(firstKey)) {
        recordIterators.add(buildRecordIteratorForHashKey(firstKey));
    }

    StaticBuffer hashKey = firstKey;
    while (itemIterator.hasNext()) {
        final Optional<StaticBuffer> nextKey = findNextHashKey(itemIterator, hashKey);
        if (nextKey.isPresent()) {
            // Found a new hash key. Make a record iterator and look for the next unique hash key
            hashKey = nextKey.get();
            recordIterators.add(buildRecordIteratorForHashKey(hashKey));
        }
    }

    // If we've already seen the final hashKey in a previous scan segment result, we want to avoid returning it again.
    if (!hashKey.equals(firstKey) && boundaryKeys.contains(hashKey)) {
        recordIterators.remove(recordIterators.size() - 1);
    }

    // Update the boundary keys for this segment
    if (scanContext.isFirstResult()) {
        setInitialBoundaryKeys(segment, firstKey, hashKey);
    } else {
        updateLastKey(segment, hashKey);
    }
    return recordIterators;
}
 
Example 18
Source File: LowLevelParallelScan.java    From aws-dynamodb-examples with Apache License 2.0 4 votes vote down vote up
private static void processScanResult(int segment, ScanResult result) {
    for (Map<String, AttributeValue> item : result.getItems()) {
        printItem(segment, item);
    }
}
 
Example 19
Source File: LowLevelParallelScan.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
private static void processScanResult(int segment, ScanResult result) {
    for (Map<String, AttributeValue> item : result.getItems()) {
        printItem(segment, item);
    }
}
 
Example 20
Source File: DynamoDBScanItems.java    From aws-doc-sdk-examples with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {

        if (args.length < 1) {
            System.out.println("Please specify a table name");
            System.exit(1);
        }

        // snippet-start:[dynamodb.java.dynamoDB_scan.main]
        String tableName = args[0];
        AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();

        try {

            ScanRequest scanRequest = new ScanRequest()
                    .withTableName(tableName);

            ScanResult result = client.scan(scanRequest);

            for (Map<String, AttributeValue> item : result.getItems()) {
                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).getS());

                }
            }


        } catch (AmazonDynamoDBException e) {
            e.getStackTrace();
        }

        // snippet-end:[dynamodb.java.dynamoDB_scan.main]
    }