Example usage for com.amazonaws.services.dynamodbv2.model ScanRequest ScanRequest

List of usage examples for com.amazonaws.services.dynamodbv2.model ScanRequest ScanRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.model ScanRequest ScanRequest.

Prototype

public ScanRequest() 

Source Link

Document

Default constructor for ScanRequest object.

Usage

From source file:NYSEScan.java

License:Open Source License

private static void scan(String tableName) {
    Table table = null;/*from  w  w  w. ja  v  a  2  s .  c o  m*/
    try {
        // Create table if it does not exist yet
        if (!Tables.doesTableExist(dynamoDB, tableName)) {
            System.out.println("Table " + tableName + " is does not exist");
        } else {
            table = dynamo.getTable(tableName);
        }
        // select * from stock_eod where stockTicker = 'HCA' and v > 1000000;
        // http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.SpecifyingConditions.html
        Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
        expressionAttributeValues.put(":val", new AttributeValue().withN("1000000"));

        //Below is not recommended as stockTicker is part of the key
        expressionAttributeValues.put(":st", new AttributeValue().withS("HCA"));

        ScanRequest scanRequest = new ScanRequest().withTableName(tableName)
                .withFilterExpression("v > :val and stockTicker = :st")
                // select stockTicker, tradeDate, v from stock_eod where stockTicker = 'HCA' and v > 1000000;
                .withProjectionExpression("stockTicker,tradeDate,v")
                .withExpressionAttributeValues(expressionAttributeValues);

        ScanResult result = dynamoDB.scan(scanRequest);

        for (Map<String, AttributeValue> item : result.getItems()) {
            System.out.println(item);
        }

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to AWS, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with AWS, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:com.amazon.janusgraph.diskstorage.dynamodb.AbstractDynamoDbStore.java

License:Open Source License

protected ScanRequest createScanRequest() {
    return new ScanRequest().withTableName(tableName).withConsistentRead(forceConsistentRead)
            .withLimit(client.scanLimit(tableName)).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);
}

From source file:com.amazon.janusgraph.diskstorage.dynamodb.DynamoDbDelegate.java

License:Open Source License

public static ScanRequest copyScanRequest(final ScanRequest request) {
    return new ScanRequest().withAttributesToGet(request.getAttributesToGet())
            .withScanFilter(request.getScanFilter()).withConditionalOperator(request.getConditionalOperator())
            .withExclusiveStartKey(request.getExclusiveStartKey())
            .withExpressionAttributeNames(request.getExpressionAttributeNames())
            .withExpressionAttributeValues(cloneItem(request.getExpressionAttributeValues()))
            .withFilterExpression(request.getFilterExpression()).withIndexName(request.getIndexName())
            .withLimit(request.getLimit()).withProjectionExpression(request.getProjectionExpression())
            .withReturnConsumedCapacity(request.getReturnConsumedCapacity())
            .withScanFilter(request.getScanFilter()).withSelect(request.getSelect())
            .withTableName(request.getTableName()).withTotalSegments(request.getTotalSegments())
            .withSegment(request.getSegment());
}

From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.DynamoDbTemplate.java

License:Apache License

private <T extends Item> Collection<T> executeQuery(final AttributeQuery query, final Class<T> itemClass) {
    final ItemConfiguration itemConfiguration = getItemConfiguration(itemClass);
    final com.amazonaws.services.dynamodbv2.model.Condition condition = new com.amazonaws.services.dynamodbv2.model.Condition();

    if (query.getCondition().getComparisonOperator() == Operators.NULL) {
        condition.setComparisonOperator(ComparisonOperator.NULL);
    } else if (query.getCondition().getComparisonOperator() == Operators.NOT_NULL) {
        condition.setComparisonOperator(ComparisonOperator.NOT_NULL);
    } else {//w w w  . java 2  s .  c  om
        if (query.getCondition().getComparisonOperator() == Operators.EQUALS) {
            condition.setComparisonOperator(ComparisonOperator.EQ);
        } else if (query.getCondition().getComparisonOperator() == Operators.LESS_THAN_OR_EQUALS) {
            condition.setComparisonOperator(ComparisonOperator.LE);
        } else if (query.getCondition().getComparisonOperator() == Operators.GREATER_THAN_OR_EQUALS) {
            condition.setComparisonOperator(ComparisonOperator.GE);
        }

        final Collection<AttributeValue> attributeValueList = new ArrayList<>();

        for (final String value : query.getCondition().getValues()) {
            if (value != null && !value.isEmpty()) {
                attributeValueList.add(new AttributeValue(value));
            }
        }

        if (attributeValueList.size() == 0) {
            return new ArrayList<>();
        }

        condition.setAttributeValueList(attributeValueList);
    }

    final Map<String, com.amazonaws.services.dynamodbv2.model.Condition> conditions = new HashMap<>();
    conditions.put(query.getAttributeName(), condition);
    final List<T> totalItems = new ArrayList<>();
    Map<String, AttributeValue> lastEvaluatedKey = null;
    final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName();
    if (itemConfiguration.hasIndexOn(query.getAttributeName())) {
        do {
            final String queryAttributeName = query.getAttributeName();
            final PrimaryKeyDefinition primaryKeyDefinition = itemConfiguration.primaryKeyDefinition();
            final String primaryKeyPropertyName = primaryKeyDefinition.propertyName();
            final boolean isPrimaryKeyQuery = queryAttributeName.equals(primaryKeyPropertyName);
            final QueryRequest queryRequest = new QueryRequest().withTableName(tableName)
                    .withKeyConditions(conditions).withExclusiveStartKey(lastEvaluatedKey);
            if (!isPrimaryKeyQuery) {
                queryRequest.withIndexName(queryAttributeName + "_idx");
            }

            final QueryResult queryResult;
            try {
                queryResult = amazonDynamoDbClient.query(queryRequest);
            } catch (final AmazonServiceException e) {
                throw new PersistenceResourceFailureException("Failure while attempting DynamoDb Query", e);
            }
            totalItems.addAll(marshallIntoObjects(itemClass, queryResult.getItems()));
            lastEvaluatedKey = queryResult.getLastEvaluatedKey();
        } while (lastEvaluatedKey != null);

    } else {
        logger.debug("Performing table scan with query: " + query);
        do {
            final ScanRequest scanRequest = new ScanRequest().withTableName(tableName)
                    .withScanFilter(conditions).withExclusiveStartKey(lastEvaluatedKey);
            final ScanResult scanResult;
            try {
                scanResult = amazonDynamoDbClient.scan(scanRequest);
            } catch (final AmazonServiceException e) {
                throw new PersistenceResourceFailureException("Failure while attempting DynamoDb Scan", e);
            }
            totalItems.addAll(marshallIntoObjects(itemClass, scanResult.getItems()));
            lastEvaluatedKey = scanResult.getLastEvaluatedKey();
        } while (lastEvaluatedKey != null);
    }

    return totalItems;
}

From source file:com.eho.dynamodb.DynamoDBConnection.java

public static ScanResult scan_dynamodb(Map<String, String> strings, Map<String, String> numbers) {
    Map<String, AttributeValue> expression_values = new HashMap<>();
    Map<String, String> expression_names = new HashMap<>();

    if (strings.keySet().contains("#jsondocument.#name[0].given[0]")
            | strings.keySet().contains("#jsondocument.#name[0].#family[0]"))
        expression_names.put("#name", "name");

    if (strings.keySet().contains("#jsondocument.#name[0].#family[0]"))
        expression_names.put("#family", "family");

    expression_names.put("#jsondocument", "json-document");

    ScanRequest scanRequest = new ScanRequest().withTableName(DynamoDBConnection.PATIENT_TABLE);

    int i = 0;/*from   w  w w . j a v  a2  s  . c o m*/
    String filter_expression = "";
    for (String path : strings.keySet()) {
        String thisVal = strings.get(path);
        expression_values.put(":stringval" + i, new AttributeValue().withS(thisVal));
        if (filter_expression.equals(""))
            filter_expression += "(" + path + " = :stringval" + i++ + ")";
        else
            filter_expression += " AND (" + path + " = :stringval" + i++ + ")";
    }
    scanRequest.withExpressionAttributeNames(expression_names).withExpressionAttributeValues(expression_values)
            .withFilterExpression(filter_expression);
    return dynamoDBClient.scan(scanRequest);

}

From source file:com.erudika.para.persistence.AWSDynamoDAO.java

License:Apache License

private <P extends ParaObject> String readPageFromTable(String appid, Pager pager, LinkedList<P> results) {
    ScanRequest scanRequest = new ScanRequest().withTableName(getTableNameForAppid(appid))
            .withLimit(pager.getLimit()).withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL);

    if (!StringUtils.isBlank(pager.getLastKey())) {
        scanRequest = scanRequest.withExclusiveStartKey(
                Collections.singletonMap(Config._KEY, new AttributeValue(pager.getLastKey())));
    }//w  w  w  .  j  a va  2  s  .  c om

    ScanResult result = client().scan(scanRequest);
    for (Map<String, AttributeValue> item : result.getItems()) {
        P obj = fromRow(item);
        if (obj != null) {
            results.add(obj);
        }
    }

    if (result.getLastEvaluatedKey() != null) {
        return result.getLastEvaluatedKey().get(Config._KEY).getS();
    } else {
        return null;
    }
}

From source file:com.facebook.presto.dynamodb.DynamodbClient.java

License:Apache License

public Iterator<List<Map<String, AttributeValue>>> getTableData(String name,
        Optional<Entry<String, AttributeValue>> hashKeyCondition,
        Optional<Entry<String, Condition>> rangeKeyCondition) {
    AtomicReference<Map<String, AttributeValue>> lastKeyEvaluated = new AtomicReference<>();
    AtomicBoolean firstRun = new AtomicBoolean(true);

    return new Iterator<List<Map<String, AttributeValue>>>() {
        @Override/*w  w  w .  j  av  a2s. c om*/
        public boolean hasNext() {
            return firstRun.get() && lastKeyEvaluated.get() != null;
        }

        @Override
        public List<Map<String, AttributeValue>> next() {
            firstRun.set(false);
            if (hashKeyCondition.isPresent()) {
                ImmutableMap.Builder<String, Condition> builder = ImmutableMap.builder();
                builder.put(hashKeyCondition.get().getKey(), new Condition()
                        .withAttributeValueList(hashKeyCondition.get().getValue()).withComparisonOperator(EQ));

                if (rangeKeyCondition.isPresent()) {
                    Entry<String, Condition> rangeEntry = rangeKeyCondition.get();
                    if (rangeEntry.getValue().getComparisonOperator() == EQ.name()
                            && rangeEntry.getValue().getAttributeValueList().size() == 1) {
                        GetItemResult item = dynamoDBClient.getItem(name,
                                ImmutableMap.of(hashKeyCondition.get().getKey(),
                                        hashKeyCondition.get().getValue(), rangeEntry.getKey(),
                                        rangeEntry.getValue().getAttributeValueList().get(0)));
                        return ImmutableList.of(item.getItem());
                    } else {
                        builder.put(rangeKeyCondition.get().getKey(), rangeKeyCondition.get().getValue());
                    }
                }

                QueryResult query = dynamoDBClient.query(
                        new QueryRequest().withTableName(name).withExclusiveStartKey(lastKeyEvaluated.get())
                                .withKeyConditions(builder.build()).withLimit(100000));

                lastKeyEvaluated.set(query.getLastEvaluatedKey());

                return query.getItems();
            } else {
                ScanResult scan = dynamoDBClient.scan(new ScanRequest()
                        .withExclusiveStartKey(lastKeyEvaluated.get()).withLimit(100000).withTableName(name));

                lastKeyEvaluated.set(scan.getLastEvaluatedKey());
                return scan.getItems();
            }
        }
    };
}

From source file:com.innoq.hagmans.bachelor.DynamoDBUtils.java

License:Open Source License

/**
 * Returns a @HashMap with all temperatures for all sensors
 * /*from  w  ww . j a  v  a 2 s . co  m*/
 * @param tableName
 * @return @HashMap, which key is the name of the sensor. The values are
 *         another @HashMap, which has timestamps of the date that the
 *         temperatures were created as keys, and the values are a list of
 *         temperatures of the sensor at the given timestamp
 */
public HashMap<String, HashMap<String, HashMap<String, Object>>> getAllSensorTemperatures(String tableName) {
    ScanRequest scanRequest = new ScanRequest().withTableName(tableName);

    ScanResult result = client.scan(scanRequest);
    HashMap<String, HashMap<String, HashMap<String, Object>>> allTemperatures = new HashMap<>();
    for (Map<String, AttributeValue> item : result.getItems()) {
        String sensorName = item.get(ATTRIBUTE_NAME_HASH_KEY).getS();
        HashMap<String, HashMap<String, Object>> currentHashMap = getTemperaturesForSensor(sensorName,
                tableName);
        allTemperatures.put(sensorName, currentHashMap);
    }

    return allTemperatures;
}

From source file:com.intuit.tank.persistence.databases.AmazonDynamoDatabaseDocApi.java

License:Open Source License

/**
 * @{inheritDoc/*from  w w w . j a va 2 s  .co  m*/
 */
@SuppressWarnings("unchecked")
@Override
public PagedDatabaseResult getPagedItems(String tableName, Object nextToken, String minRange, String maxRange,
        String instanceId, String jobId) {
    List<Item> ret = new ArrayList<Item>();
    Map<String, AttributeValue> lastKeyEvaluated = (Map<String, AttributeValue>) nextToken;
    ScanRequest scanRequest = new ScanRequest().withTableName(tableName);
    Map<String, Condition> conditions = new HashMap<String, Condition>();
    if (jobId != null) {
        Condition jobIdCondition = new Condition();
        jobIdCondition.withComparisonOperator(ComparisonOperator.EQ)
                .withAttributeValueList(new AttributeValue().withS(jobId));

        conditions.put(DatabaseKeys.JOB_ID_KEY.getShortKey(), jobIdCondition);
    }
    if (StringUtils.isNotBlank(instanceId)) {
        // add a filter
        Condition filter = new Condition();
        filter.withComparisonOperator(ComparisonOperator.EQ)
                .withAttributeValueList(new AttributeValue().withS(instanceId));
        scanRequest.addScanFilterEntry(DatabaseKeys.INSTANCE_ID_KEY.getShortKey(), filter);
    }
    Condition rangeKeyCondition = new Condition();
    if (minRange != null && maxRange != null) {
        rangeKeyCondition.withComparisonOperator(ComparisonOperator.BETWEEN.toString())
                .withAttributeValueList(new AttributeValue().withS(minRange))
                .withAttributeValueList(new AttributeValue().withS(maxRange));

    } else if (minRange != null) {
        rangeKeyCondition.withComparisonOperator(ComparisonOperator.GE.toString())
                .withAttributeValueList(new AttributeValue().withS(minRange));
    } else if (maxRange != null) {
        rangeKeyCondition.withComparisonOperator(ComparisonOperator.LT.toString())
                .withAttributeValueList(new AttributeValue().withS(maxRange));
    } else {
        rangeKeyCondition = null;
    }
    if (rangeKeyCondition != null) {
        conditions.put(DatabaseKeys.REQUEST_NAME_KEY.getShortKey(), rangeKeyCondition);
    }
    scanRequest.withScanFilter(conditions);
    scanRequest.withExclusiveStartKey(lastKeyEvaluated);

    ScanResult result = dynamoDb.scan(scanRequest);
    for (Map<String, AttributeValue> item : result.getItems()) {
        ret.add(getItemFromResult(item));
    }
    return new PagedDatabaseResult(ret, result.getLastEvaluatedKey());
}

From source file:com.lvl6.mobsters.dynamo.setup.TransactionExamples.java

License:Open Source License

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.
    // /*from ww w  . ja  va  2 s. c om*/
    //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);
        }
    }

}