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

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

Introduction

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

Prototype

public Object get(String attrName) 

Source Link

Document

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

Usage

From source file:com.eho.fhir.econsult.RestControllers.PatientController.java

@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8")
public ResponseEntity<String> patientGET(@PathVariable String id) throws Exception {
    Item item = DynamoDBConnection.get_item_by_ID(id);
    if (item == null)
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    Integer last_version = item.getInt("version");
    return new ResponseEntity(item.get("text" + last_version.toString()), HttpStatus.OK);
}

From source file:com.eho.fhir.econsult.RestControllers.PractitionerController.java

@RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8")
public ResponseEntity<String> practitionerGET(@PathVariable String id) throws Exception {
    Item item = DynamoDBConnection.get_item_by_ID(id);
    if (item == null)
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    Integer last_version = item.getInt("version");
    return new ResponseEntity(item.get("text" + last_version.toString()), HttpStatus.OK);
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecRemove.java

License:LGPL

@Test
public void test_remove_nestedPath() throws Exception {
    // setup//from  w  w w .ja  v  a  2  s.  c  o  m
    table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE)
            .put("a", ImmutableMap.of("a", 2, "b", true)).build()));

    // setup
    String patchExpression = "[ { \"op\": \"remove\", \"path\": \"/a/a\" } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder actual = jsonPatch.get();
    UpdateItemExpressionSpec actualSpec = actual.buildForUpdate();
    table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, actualSpec);
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertTrue(item.getRawMap("a").containsKey("b"));
    Assert.assertEquals(item.getRawMap("a").get("b"), true);
    Assert.assertFalse(item.getRawMap("a").containsKey("a"));
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java

License:LGPL

@Test
public void test_replace_existingNestedPath_string() throws Exception {
    // setup//from  w w w  .  j  av  a2s  .  c o m
    table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE)
            .put("a", ImmutableMap.of("a", 2L, "b", true)).build()));

    String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a/b\", \"value\": \"bar\" } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder builder = jsonPatch.get();
    UpdateItemExpressionSpec spec = builder.buildForUpdate();
    table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec);
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertTrue(item.getRawMap("a").containsKey("a"));
    Assert.assertEquals(((BigDecimal) item.getRawMap("a").get("a")).longValue(), 2L);
    Assert.assertTrue(item.getRawMap("a").containsKey("b"));
    Assert.assertEquals(item.getRawMap("a").get("b"), "bar");
}

From source file:com.github.fge.jsonpatch.JsonPatchToXSpecReplace.java

License:LGPL

@Test
public void test_replace_singlePath_object() throws Exception {
    table.putItem(Item.fromMap(//www .j av  a2s . c  o  m
            ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE).put("a", 1L).build()));
    // setup
    String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": {\"b\": \"c\", \"d\": 1} } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder builder = jsonPatch.get();
    UpdateItemExpressionSpec spec = builder.buildForUpdate();
    table.updateItem(KEY_ATTRIBUTE_NAME, VALUE, spec);
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertTrue(item.getRawMap("a").containsKey("b"));
    Assert.assertEquals(item.getRawMap("a").get("b"), "c");
    Assert.assertTrue(item.getRawMap("a").containsKey("d"));
    Assert.assertEquals(((BigDecimal) item.getRawMap("a").get("d")).longValue(), 1L);
}

From source file:jp.classmethod.aws.dynamodb.DynamoDbRepository.java

License:Open Source License

private PrimaryKey getPrimaryKeyFromItem(Item item) {
    KeyAttribute[] keyAttributes = schemata.stream().map(keySchemaElement -> {
        final String attrName = keySchemaElement.getAttributeName();
        Preconditions.checkArgument(item.hasAttribute(attrName),
                "must provide keys with " + attrName + " field set");
        return new KeyAttribute(attrName, item.get(attrName));
    }).toArray(KeyAttribute[]::new);
    return new PrimaryKey(keyAttributes);
}

From source file:mx.iteso.desi.cloud.keyvalue.DynamoDBStorage.java

License:Apache License

@Override
public Set<String> get(String search) {
    Set<String> ret = new HashSet<String>();
    Table table = docClient.getTable(dbName);

    QuerySpec spec = new QuerySpec().withKeyConditionExpression("keyword = :v_kw")
            .withValueMap(new ValueMap().withString(":v_kw", search));

    ItemCollection<QueryOutcome> items = table.query(spec);

    Iterator<Item> iterator = items.iterator();
    Item item = null;
    while (iterator.hasNext()) {
        item = iterator.next();/*from  w  ww.  ja va 2s .c  om*/
        ret.add(item.get("value").toString());
    }
    return ret;
}

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;//from w ww .j av a  2 s .c  o 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:org.diksha.common.dyutils.DyDBUtils.java

License:Apache License

public static void listActiveJobs(String optionalExecutionId) {
    DynamoDB dynamoDB = getDynamoDB();//from   w w w  .j a v  a 2 s.com

    Table table = dynamoDB.getTable("SchedulerWorkflowState");
    Index index = table.getIndex("loopStateIndex");

    ItemCollection<QueryOutcome> items = null;
    QuerySpec querySpec = new QuerySpec();

    if ((optionalExecutionId != null) && !optionalExecutionId.isEmpty()) {
        querySpec.withKeyConditionExpression("loopState = :v_state and begins_with(clientId, :v_eid)")
                .withValueMap(new ValueMap().withString(":v_state", "PROCESSING").withString(":v_eid",
                        optionalExecutionId));

    } else {

        querySpec.withKeyConditionExpression("loopState = :v_state")
                .withValueMap(new ValueMap().withString(":v_state", "PROCESSING"));
    }

    items = index.query(querySpec);
    Iterator<Item> iterator = items.iterator();

    System.out.format("%20s %7s %28s %40s\n", "CronExpression", "Loop Count", "Next Scheduled Time       ",
            " ExecutionId");
    while (iterator.hasNext()) {

        Item item = iterator.next();

        System.out.format("%20s %7s %28s %40s\n", item.get("cronExpression"), item.get("loopCount"),
                item.get("lastProposedTimeDate"), item.get("clientId"));

    }

}

From source file:tr.com.serkanozal.samba.cache.impl.SambaGlobalCache.java

License:Open Source License

@Override
public void clear() {
    ItemCollection<ScanOutcome> items = DYNAMO_DB_TABLE.scan();
    IteratorSupport<Item, ScanOutcome> itemsIter = items.iterator();
    while (itemsIter.hasNext()) {
        Item item = itemsIter.next();
        DYNAMO_DB_TABLE.deleteItem("id", item.get("id"));
    }/*from ww w.j  ava  2 s.c o m*/
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Global cache has been cleared");
    }
}