Example usage for com.amazonaws.services.dynamodbv2.document.internal InternalUtils toSimpleMapValue

List of usage examples for com.amazonaws.services.dynamodbv2.document.internal InternalUtils toSimpleMapValue

Introduction

In this page you can find the example usage for com.amazonaws.services.dynamodbv2.document.internal InternalUtils toSimpleMapValue.

Prototype

public static <T> Map<String, T> toSimpleMapValue(Map<String, AttributeValue> values) 

Source Link

Usage

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

License:LGPL

@Test
public void testReplaceSinglePathNumberExtant() throws Exception {
    // setup/*from   w ww  .ja  va  2  s .  co m*/
    table.putItem(Item.fromMap(ImmutableMap.<String, Object>builder().put(KEY_ATTRIBUTE_NAME, VALUE)
            .put("a", "peekaboo").build()));
    String patchExpression = "[ { \"op\": \"replace\", \"path\": \"/a\", \"value\": 1 } ]";
    JsonNode jsonNode = JsonLoader.fromString(patchExpression);
    JsonPatch jsonPatch = JsonPatch.fromJson(jsonNode);
    // exercise
    ExpressionSpecBuilder builder = jsonPatch.get();
    UpdateItemExpressionSpec spec = builder.buildForUpdate();
    UpdateItemOutcome out = table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY_ATTRIBUTE_NAME, VALUE)
            .withExpressionSpec(spec).withReturnValues(ReturnValue.ALL_OLD));

    Item oldItem = Item.fromMap(InternalUtils.toSimpleMapValue(out.getUpdateItemResult().getAttributes()));
    Assert.assertTrue(oldItem.hasAttribute("a"));
    Assert.assertEquals(oldItem.getString("a"), "peekaboo");
    // verify
    Item item = table.getItem(PK);
    Assert.assertTrue(item.hasAttribute("key"));
    Assert.assertEquals(item.getString("key"), "keyValue");
    Assert.assertTrue(item.hasAttribute("a"));
    Assert.assertEquals(item.getNumber("a").longValue(), 1L);
}

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

License:Open Source License

private Chunk<Item> getItemListForGsi(String indexName, QuerySpec spec) {
    Preconditions.checkNotNull(spec, "spec must not be null");
    final ItemCollection<QueryOutcome> outcome = table.getIndex(indexName).query(spec);

    List<Item> results = new ArrayList<>();
    try {/*from  w w  w.j  a  va 2 s .  c  om*/
        outcome.pages().forEach(p -> {
            p.iterator().forEachRemaining(o -> results.add(o));
        });
    } catch (AmazonServiceException e) {
        throw convertDynamoDBException(e, "getting by spec: " + spec.toString(),
                null /*no write condition exception*/);
    }
    Map<String, AttributeValue> lastEvaluatedKey = outcome.getLastLowLevelResult().getQueryResult()
            .getLastEvaluatedKey();
    String lastEvaluatedItemJson = lastEvaluatedKey == null ? null
            : Item.fromMap(InternalUtils.toSimpleMapValue(lastEvaluatedKey)).toJSON();

    return new ChunkImpl<>(results, lastEvaluatedItemJson, null /*chunkable*/);
}

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

License:Open Source License

private List<E> findAll(Iterable<AttributeValue> ids, boolean useParallelBatches) {
    Preconditions.checkNotNull(ids, "ids may not be null");
    List<AttributeValue> idList = Lists.newArrayList(ids);
    if (idList.isEmpty()) {
        return new ArrayList<>();
    }//from  w w  w  .ja  va2s.c  o m
    List<Map<String, AttributeValue>> resultantItems = new ArrayList<>();

    StreamSupport.stream(Iterables.partition(idList, 25).spliterator(), useParallelBatches).forEach(inner -> {
        BatchGetItemRequest req = new BatchGetItemRequest();
        KeysAndAttributes keysAndAttributes = new KeysAndAttributes();
        keysAndAttributes.setConsistentRead(true);
        keysAndAttributes.setKeys(
                inner.stream().map(id -> ImmutableMap.of(hashKeyName, id)).collect(Collectors.toList()));
        String tableName = tableName();
        req.withRequestItems(ImmutableMap.of(tableName, keysAndAttributes));

        BatchGetItemResult result;

        do {
            try {
                result = dynamoDB.batchGetItem(req);
                resultantItems.addAll(result.getResponses().get(tableName));
                req.setRequestItems(result.getUnprocessedKeys());
            } catch (AmazonClientException e) {
                throw this.convertDynamoDBException(e, "batch get", null /*no conditions for reads*/);
            }
        } while (false == result.getUnprocessedKeys().isEmpty());
    });

    return resultantItems.stream().map(legacyItem -> Item.fromMap(InternalUtils.toSimpleMapValue(legacyItem)))
            .map(item -> convertItemToDomain(item)).collect(Collectors.toList());
}