Example usage for com.amazonaws.services.dynamodbv2.document Table scan

List of usage examples for com.amazonaws.services.dynamodbv2.document Table scan

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document Table scan.

Prototype

@Beta
    public ItemCollection<ScanOutcome> scan(ScanExpressionSpec scanExpressions) 

Source Link

Usage

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

License:Apache License

private <T extends Item> Collection<T> executeQuery(final AttributeQuery query, final Class<T> itemClass) {
    final ItemConfiguration itemConfiguration = getItemConfiguration(itemClass);
    final String tableName = databaseSchemaHolder.schemaName() + "." + itemConfiguration.tableName();

    final Table table = dynamoDBClient.getTable(tableName);

    final List<T> totalItems = new ArrayList<>();

    if (itemConfiguration.hasIndexOn(query.getAttributeName())
            && query.getCondition().getComparisonOperator() == Operators.EQUALS) {

        final QuerySpec querySpec = generateQuerySpec(query);
        final ItemCollection<QueryOutcome> queryOutcome;

        if (itemConfiguration.primaryKeyDefinition().propertyName().equals(query.getAttributeName())) {
            // if the query is for the has then call query on table
            queryOutcome = table.query(querySpec);
        } else {//from  w  w w.jav  a  2s  .  com
            final Index index = table.getIndex(query.getAttributeName() + "_idx");
            queryOutcome = index.query(querySpec);
        }

        final Iterator<com.amazonaws.services.dynamodbv2.document.Item> iterator = queryOutcome.iterator();
        while (iterator != null && iterator.hasNext()) {
            final com.amazonaws.services.dynamodbv2.document.Item item = iterator.next();
            totalItems.add(stringToItem(item.toJSON(), itemClass));
        }
    } else {
        logger.debug("Performing table scan with query: " + query);
        ScanSpec scanSpec = null;
        try {
            scanSpec = generateScanSpec(query, itemClass);
        } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                | InvocationTargetException | NoSuchMethodException | SecurityException e) {
            throw new PersistenceResourceFailureException("Could not create ScanSpec for query: " + query, e);
        }
        final ItemCollection<ScanOutcome> scanOutcome = table.scan(scanSpec);

        final Iterator<com.amazonaws.services.dynamodbv2.document.Item> iterator = scanOutcome.iterator();
        while (iterator.hasNext()) {
            final com.amazonaws.services.dynamodbv2.document.Item item = iterator.next();
            totalItems.add(stringToItem(item.toJSON(), itemClass));
        }
    }

    return totalItems;
}

From source file:com.github.sdmcraft.slingdynamo.impl.DynamoDBResourceProvider.java

License:Open Source License

/**
 * Returns a resource from this resource provider or null if the resource provider cannot find it.
 * The table-name, id and child-ids are parsed out from the path and queried against dynamodb to fetch the specified resource.
 *
 * @param resolver the ResourceResolver to which the returned Resource is attached.
 * @param req the HttpServletRequest made to get this resource
 * @param path the path of the resource. The path is of the format &lt;table-name&gt;/&lt;id&gt;/[&lt;child-id1&gt;/.../&lt;child-idn&gt;]
 * @return the resource at the specified path if it exists else returns null
 *///from   ww w  .ja v a  2 s .  c om
public Resource getResource(ResourceResolver resolver, HttpServletRequest req, String path) {
    Resource resource = null;

    try {
        Map<String, Object> resourceProps = new HashMap<String, Object>();
        ResourceMetadata resourceMetadata = new ResourceMetadata();
        resourceMetadata.setResolutionPath(path);

        if (!path.contains(".")) {
            if (path.length() > root.length()) {
                String subPath = path.substring(root.length() + 1);
                String[] subPathSplits = subPath.split("/");
                String table = subPathSplits[0];
                resourceMetadata.put("table", table);

                Table dbtable = dynamoDB.getTable(table);

                if (subPathSplits.length == 1) {
                    DescribeTableRequest describeTableRequest = new DescribeTableRequest(table);
                    DescribeTableResult describeTableResult = null;

                    describeTableResult = dynamoDBClient.describeTable(describeTableRequest);

                    Date creationDate = describeTableResult.getTable().getCreationDateTime();
                    long itemCount = describeTableResult.getTable().getItemCount();

                    resourceProps.put("creation-date", creationDate);
                    resourceProps.put("record-count", itemCount);
                    resourceProps.put("table-name", table);
                } else if (subPathSplits.length == 2) {
                    int id = Integer.parseInt(subPathSplits[1]);
                    ScanFilter idFilter = new ScanFilter("id").eq(id);
                    ItemCollection<ScanOutcome> items = dbtable.scan(idFilter);
                    Iterator<Item> itemItr = items.iterator();

                    Item item = itemItr.next();
                    resourceProps = itemToMap(item);
                } else if (subPathSplits.length > 2) {
                    int parent = Integer.parseInt(subPathSplits[1]);
                    Item item = null;

                    for (int i = 2; i < subPathSplits.length; i++) {
                        int child = Integer.parseInt(subPathSplits[i]);
                        ScanFilter parentFilter = new ScanFilter("parent").eq(parent);
                        ScanFilter childFilter = new ScanFilter("child_id").eq(child);
                        ItemCollection<ScanOutcome> items = dbtable.scan(parentFilter, childFilter);
                        Iterator<Item> itemItr = items.iterator();
                        item = itemItr.next();
                        parent = item.getInt("id");
                    }

                    resourceProps = itemToMap(item);
                }
            }

            resourceProps.put("hello", "world");

            ModifiableValueMapDecorator valueMap = new ModifiableValueMapDecorator(resourceProps);

            resource = new DynamoDBResource(resolver, resourceMetadata, valueMap, resourceType);
        }
    } catch (Throwable ex) {
        LOGGER.error(ex.getMessage(), ex);
        throw new RuntimeException(ex);
    }

    return resource;
}

From source file:io.ignitr.dispatchr.manager.core.data.ClientRepository.java

License:Apache License

/**
 * Finds all clients in DynamoDB.// w w w  .ja va  2  s.c  o  m
 *
 * @param offset number of records to offset when paginating
 * @param limit number of records to return when paginating
 * @param sortDir the direction to sort returned records when paginating
 * @param active a boolean flag indicating whether or not to return only "active" clients
 * @return an {@link Observable} that emits all clients
 */
public Observable<Client> findAll(long offset, long limit, String sortDir, boolean active) {
    return Observable.create(subscriber -> {
        Table clientTable = dynamoDB.getTable(CLIENT_TABLE_NAME);

        ItemCollection<ScanOutcome> results = clientTable.scan(new ScanSpec());

        results.forEach(item -> subscriber.onNext(convertFromItem(item)));

        subscriber.onCompleted();
    });
}

From source file:io.ignitr.dispatchr.manager.core.data.TopicRepository.java

License:Apache License

/**
 * Finds the metadata for all of the topics.
 *
 * @return an {@link Observable} of {@link Topic} containing the metadata about the topic
 *//*from   w  w  w. j av  a  2 s .  c  om*/
public Observable<Topic> findAll() {
    return Observable.create(subscriber -> {
        Table table = dynamoDB.getTable(TOPIC_TABLE_NAME);

        ItemCollection<ScanOutcome> results = table.scan(new ScanSpec());

        results.forEach(item -> {
            Topic metadata = new Topic();
            metadata.setName(item.getString("name"));
            metadata.setArn(item.getString("arn"));
            metadata.setRegistered(true);

            subscriber.onNext(metadata);
        });

        subscriber.onCompleted();
    });
}

From source file:vfma.LoadSensorData.java

License:Open Source License

public ArrayList<RawSensorData> getSensorData(String lastTimestamp) throws VFMException {

    ArrayList<RawSensorData> rsdlist = new ArrayList<RawSensorData>();

    try {/*from w  w w  . j a v  a 2  s  . c  o  m*/

        AmazonDynamoDBClient client = new AmazonDynamoDBClient()
                .withEndpoint("https://dynamodb.us-west-2.amazonaws.com");
        DynamoDB dynamoDB = new DynamoDB(client);
        Table table = dynamoDB.getTable("sensorData");
        ScanSpec scanSpec = new ScanSpec();

        System.out.println("Get sensors...");
        ItemCollection<ScanOutcome> items = table.scan(scanSpec);

        String lastTimeValue;

        Iterator<Item> iter = items.iterator();
        while (iter.hasNext()) {
            Item item = iter.next();

            RawSensorData rsd = new RawSensorData();

            rsd.setTimestamp(item.getJSON("pass").replace("\"", ""));
            final JSONObject rawData = new JSONObject(item.getJSON("payload"));
            rsd.setSensorId(rawData.getString("sensor_id_time"));
            rsd.setPassing(rawData.getString("passing"));

            System.out.println("Read " + rsd.getSensorId());

            if (rsd.getTimestamp().compareTo(lastTimestamp) > 0)
                rsdlist.add(rsd);

            lastTimeValue = rsd.getTimestamp();
        }

    } catch (Exception e) {
        System.err.println("Unable to scan the table:");
        System.err.println(e.getMessage());
    }

    return rsdlist;
}