Example usage for com.amazonaws.services.dynamodbv2.document Item getJSON

List of usage examples for com.amazonaws.services.dynamodbv2.document Item getJSON

Introduction

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

Prototype

public String getJSON(String attrName) 

Source Link

Document

Returns the value of the specified attribute in the current item as a JSON string; or null if the attribute either doesn't exist or the attribute value is null.

Usage

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

License:Apache License

/**
 * Finds the metadata for single topic./*www  .j a  v  a  2s  . c  o m*/
 *
 * @param topicName name of the topic
 * @return an {@link Observable} of {@link Topic} containing the metadata about the topic
 */
public Observable<Topic> findOne(String topicName) {
    return Observable.create(subscriber -> {
        Table table = dynamoDB.getTable(TOPIC_TABLE_NAME);

        Item item = table.getItem("name", topicName);

        if (item == null) {
            throw new TopicNotFoundException(topicName);
        } else {
            Topic metadata = new Topic();
            metadata.setName(item.getJSON("name"));
            metadata.setArn(item.getJSON("arn"));
            metadata.setRegistered(true);

            subscriber.onNext(metadata);
            subscriber.onCompleted();
        }
    });
}

From source file:org.apache.nifi.processors.aws.dynamodb.GetDynamoDB.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    List<FlowFile> flowFiles = session
            .get(context.getProperty(BATCH_SIZE).evaluateAttributeExpressions().asInteger());
    if (flowFiles == null || flowFiles.size() == 0) {
        return;/*  ww w  . jav  a  2  s.co  m*/
    }

    Map<ItemKeys, FlowFile> keysToFlowFileMap = new HashMap<>();

    final String table = context.getProperty(TABLE).evaluateAttributeExpressions().getValue();
    TableKeysAndAttributes tableKeysAndAttributes = new TableKeysAndAttributes(table);

    final String hashKeyName = context.getProperty(HASH_KEY_NAME).evaluateAttributeExpressions().getValue();
    final String rangeKeyName = context.getProperty(RANGE_KEY_NAME).evaluateAttributeExpressions().getValue();
    final String jsonDocument = context.getProperty(JSON_DOCUMENT).evaluateAttributeExpressions().getValue();

    for (FlowFile flowFile : flowFiles) {
        final Object hashKeyValue = getValue(context, HASH_KEY_VALUE_TYPE, HASH_KEY_VALUE, flowFile);
        final Object rangeKeyValue = getValue(context, RANGE_KEY_VALUE_TYPE, RANGE_KEY_VALUE, flowFile);

        if (!isHashKeyValueConsistent(hashKeyName, hashKeyValue, session, flowFile)) {
            continue;
        }

        if (!isRangeKeyValueConsistent(rangeKeyName, rangeKeyValue, session, flowFile)) {
            continue;
        }

        keysToFlowFileMap.put(new ItemKeys(hashKeyValue, rangeKeyValue), flowFile);

        if (rangeKeyValue == null || StringUtils.isBlank(rangeKeyValue.toString())) {
            tableKeysAndAttributes.addHashOnlyPrimaryKey(hashKeyName, hashKeyValue);
        } else {
            tableKeysAndAttributes.addHashAndRangePrimaryKey(hashKeyName, hashKeyValue, rangeKeyName,
                    rangeKeyValue);
        }
    }

    if (keysToFlowFileMap.isEmpty()) {
        return;
    }

    final DynamoDB dynamoDB = getDynamoDB();

    try {
        BatchGetItemOutcome result = dynamoDB.batchGetItem(tableKeysAndAttributes);

        // Handle processed items and get the json document
        List<Item> items = result.getTableItems().get(table);
        for (Item item : items) {
            ItemKeys itemKeys = new ItemKeys(item.get(hashKeyName), item.get(rangeKeyName));
            FlowFile flowFile = keysToFlowFileMap.get(itemKeys);

            if (item.get(jsonDocument) != null) {
                ByteArrayInputStream bais = new ByteArrayInputStream(item.getJSON(jsonDocument).getBytes());
                flowFile = session.importFrom(bais, flowFile);
            }

            session.transfer(flowFile, REL_SUCCESS);
            keysToFlowFileMap.remove(itemKeys);
        }

        // Handle unprocessed keys
        Map<String, KeysAndAttributes> unprocessedKeys = result.getUnprocessedKeys();
        if (unprocessedKeys != null && unprocessedKeys.size() > 0) {
            KeysAndAttributes keysAndAttributes = unprocessedKeys.get(table);
            List<Map<String, AttributeValue>> keys = keysAndAttributes.getKeys();

            for (Map<String, AttributeValue> unprocessedKey : keys) {
                Object hashKeyValue = getAttributeValue(context, HASH_KEY_VALUE_TYPE,
                        unprocessedKey.get(hashKeyName));
                Object rangeKeyValue = getAttributeValue(context, RANGE_KEY_VALUE_TYPE,
                        unprocessedKey.get(rangeKeyName));
                sendUnprocessedToUnprocessedRelationship(session, keysToFlowFileMap, hashKeyValue,
                        rangeKeyValue);
            }
        }

        // Handle any remaining items
        for (ItemKeys key : keysToFlowFileMap.keySet()) {
            FlowFile flowFile = keysToFlowFileMap.get(key);
            flowFile = session.putAttribute(flowFile, DYNAMODB_KEY_ERROR_NOT_FOUND,
                    DYNAMODB_KEY_ERROR_NOT_FOUND_MESSAGE + key.toString());
            session.transfer(flowFile, REL_NOT_FOUND);
            keysToFlowFileMap.remove(key);
        }

    } catch (AmazonServiceException exception) {
        getLogger().error("Could not process flowFiles due to service exception : " + exception.getMessage());
        List<FlowFile> failedFlowFiles = processServiceException(session, flowFiles, exception);
        session.transfer(failedFlowFiles, REL_FAILURE);
    } catch (AmazonClientException exception) {
        getLogger().error("Could not process flowFiles due to client exception : " + exception.getMessage());
        List<FlowFile> failedFlowFiles = processClientException(session, flowFiles, exception);
        session.transfer(failedFlowFiles, REL_FAILURE);
    } catch (Exception exception) {
        getLogger().error("Could not process flowFiles due to exception : " + exception.getMessage());
        List<FlowFile> failedFlowFiles = processException(session, flowFiles, exception);
        session.transfer(failedFlowFiles, REL_FAILURE);
    }
}

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  va  2  s  .c  om*/

        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;
}