Example usage for com.amazonaws.services.dynamodbv2.model AttributeValue getS

List of usage examples for com.amazonaws.services.dynamodbv2.model AttributeValue getS

Introduction

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

Prototype


public String getS() 

Source Link

Document

An attribute of type String.

Usage

From source file:awslabs.lab51.SolutionCode.java

License:Open Source License

@Override
public void addItemsToPage(AmazonS3Client s3Client, List<Map<String, AttributeValue>> items) {
    for (Map<String, AttributeValue> item : items) {
        AttributeValue key, bucket;
        if (item.containsKey("Key") && item.containsKey("Bucket")) {
            key = item.get("Key");
            bucket = item.get("Bucket");
            String itemUrl = getUrlForItem(s3Client, key.getS(), bucket.getS());
            labController.addImageToPage(itemUrl, bucket.getS(), key.getS());
        }//from w ww  .j av a  2  s. c o m
    }
}

From source file:com.amazon.janusgraph.diskstorage.dynamodb.builder.AbstractBuilder.java

License:Open Source License

protected StaticBuffer decodeValue(final AttributeValue val) {
    if (null == val) {
        return null;
    }/*from  w  w w .j ava  2s . c o  m*/
    // Dynamo does not allow empty binary values, so we use a placeholder
    // for empty values
    if (Constants.EMPTY_BUFFER_PLACEHOLDER.equals(val.getS())) {
        return BufferUtil.emptyBuffer();
    }
    return StaticArrayBuffer.of(val.getB());
}

From source file:com.amazon.janusgraph.diskstorage.dynamodb.builder.AbstractBuilder.java

License:Open Source License

protected StaticBuffer decodeKey(final Map<String, AttributeValue> key, final String name) {
    if (null == key || !key.containsKey(name)) {
        return null;
    }//from   w  w w .  j  a va  2  s .c o  m
    final AttributeValue attributeValue = key.get(name);
    final String value = attributeValue.getS();
    return decodeKey(value);
}

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

License:Open Source License

/**
 * Helper method that can clone an Attribute Value
 *
 * @param val the AttributeValue to copy
 * @param sourceDestinationMap used to avoid loops by keeping track of references
 * @return a copy of val/*  w ww .  j a  va 2  s  .c o  m*/
 */
public static AttributeValue clone(final AttributeValue val,
        final IdentityHashMap<AttributeValue, AttributeValue> sourceDestinationMap) {
    if (val == null) {
        return null;
    }

    if (sourceDestinationMap.containsKey(val)) {
        return sourceDestinationMap.get(val);
    }

    final AttributeValue clonedVal = new AttributeValue();
    sourceDestinationMap.put(val, clonedVal);
    if (val.getN() != null) {
        clonedVal.setN(val.getN());
    } else if (val.getS() != null) {
        clonedVal.setS(val.getS());
    } else if (val.getB() != null) {
        clonedVal.setB(val.getB());
    } else if (val.getNS() != null) {
        clonedVal.setNS(val.getNS());
    } else if (val.getSS() != null) {
        clonedVal.setSS(val.getSS());
    } else if (val.getBS() != null) {
        clonedVal.setBS(val.getBS());
    } else if (val.getBOOL() != null) {
        clonedVal.setBOOL(val.getBOOL());
    } else if (val.getNULL() != null) {
        clonedVal.setNULL(val.getNULL());
    } else if (val.getL() != null) {
        final List<AttributeValue> list = new ArrayList<>(val.getL().size());
        for (AttributeValue listItemValue : val.getL()) {
            if (!sourceDestinationMap.containsKey(listItemValue)) {
                sourceDestinationMap.put(listItemValue, clone(listItemValue, sourceDestinationMap));
            }
            list.add(sourceDestinationMap.get(listItemValue));
        }
        clonedVal.setL(list);
    } else if (val.getM() != null) {
        final Map<String, AttributeValue> map = new HashMap<>(val.getM().size());
        for (Entry<String, AttributeValue> pair : val.getM().entrySet()) {
            if (!sourceDestinationMap.containsKey(pair.getValue())) {
                sourceDestinationMap.put(pair.getValue(), clone(pair.getValue(), sourceDestinationMap));
            }
            map.put(pair.getKey(), sourceDestinationMap.get(pair.getValue()));
        }
        clonedVal.setM(map);
    }
    return clonedVal;
}

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

License:Open Source License

/**Calculate attribute value size*/
private static int calculateAttributeSizeInBytes(final AttributeValue value) {
    int attrValSize = 0;
    if (value == null) {
        return attrValSize;
    }//w  w w . j av  a  2  s.com

    if (value.getB() != null) {
        final ByteBuffer b = value.getB();
        attrValSize += b.remaining();
    } else if (value.getS() != null) {
        final String s = value.getS();
        attrValSize += s.getBytes(UTF8).length;
    } else if (value.getN() != null) {
        attrValSize += MAX_NUMBER_OF_BYTES_FOR_NUMBER;
    } else if (value.getBS() != null) {
        final List<ByteBuffer> bs = value.getBS();
        for (ByteBuffer b : bs) {
            if (b != null) {
                attrValSize += b.remaining();
            }
        }
    } else if (value.getSS() != null) {
        final List<String> ss = value.getSS();
        for (String s : ss) {
            if (s != null) {
                attrValSize += s.getBytes(UTF8).length;
            }
        }
    } else if (value.getNS() != null) {
        final List<String> ns = value.getNS();
        for (String n : ns) {
            if (n != null) {
                attrValSize += MAX_NUMBER_OF_BYTES_FOR_NUMBER;
            }
        }
    } else if (value.getBOOL() != null) {
        attrValSize += 1;
    } else if (value.getNULL() != null) {
        attrValSize += 1;
    } else if (value.getM() != null) {
        for (Map.Entry<String, AttributeValue> entry : value.getM().entrySet()) {
            attrValSize += entry.getKey().getBytes(UTF8).length;
            attrValSize += calculateAttributeSizeInBytes(entry.getValue());
            attrValSize += BASE_LOGICAL_SIZE_OF_NESTED_TYPES;
        }
        attrValSize += LOGICAL_SIZE_OF_EMPTY_DOCUMENT;
    } else if (value.getL() != null) {
        final List<AttributeValue> list = value.getL();
        for (Integer i = 0; i < list.size(); i++) {
            attrValSize += calculateAttributeSizeInBytes(list.get(i));
            attrValSize += BASE_LOGICAL_SIZE_OF_NESTED_TYPES;
        }
        attrValSize += LOGICAL_SIZE_OF_EMPTY_DOCUMENT;
    }
    return attrValSize;
}

From source file:com.aws.sampleImage.url.LambdaFunctionImageURLCrawlerHandler.java

private void handleEachRecord(String eventName, StreamRecord streamRecord) {
    try {/*from ww w.j a  v  a  2 s  .c  o m*/

        //Only if Insert then start processing
        if (INSERT_EVENT.equalsIgnoreCase(eventName)) {

            Map<String, AttributeValue> newImageMap = streamRecord.getNewImage();

            AttributeValue keywordsAttrVal = newImageMap.get("keywords");
            AttributeValue synsetCodeAttrVal = newImageMap.get("synset_code");

            String keywords = keywordsAttrVal.getS();
            String synsetCode = synsetCodeAttrVal.getS();

            /*System.out.println("Keywords : " + keywords);
            System.out.println("SynsetCode : " + synsetCode);*/

            String[] keywordFragments = keywords.split(",");

            for (int i = 0; i < keywordFragments.length; i++) {

                //System.out.println("Calling Flickr API for getting Image URL :: " + keywordFragments[i]);
                callFlickrAPIForEachKeyword(keywordFragments[i], synsetCode);

            }

        }

    } catch (AmazonServiceException ase) {
        System.out.println(ase.getMessage());
        //ace.printStackTrace();

    } catch (AmazonClientException ace) {
        System.out.println(ace.getMessage());
        //ace.printStackTrace();

    } catch (Exception ex) {
        System.out.println(ex.getMessage());
        //ex.printStackTrace();
    }
}

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

License:Apache License

protected final <T extends Item> Collection<PropertyDescriptor> createUniqueConstraintIndexes(final T item,
        final ItemConfiguration itemConfiguration,
        final Collection<PropertyDescriptor> constraintPropertyDescriptors) {
    final Set<PropertyDescriptor> createdConstraintPropertyDescriptors = new HashSet<>();
    ItemConstraintViolationException itemConstraintViolationException = null;
    for (final UniqueConstraint uniqueConstraint : itemConfiguration.uniqueConstraints()) {
        final String uniqueConstraintPropertyName = uniqueConstraint.propertyName();
        final PropertyDescriptor uniqueConstraintPropertyDescriptor = uniqueConstraint.propertyDescriptor();
        if (constraintPropertyDescriptors.contains(uniqueConstraintPropertyDescriptor)) {
            final AttributeValue uniqueConstraintAttributeValue = DynamoDbPropertyMarshaller.getValue(item,
                    uniqueConstraintPropertyDescriptor);
            if (uniqueConstraintAttributeValue == null) {
                continue;
            }// w  w w .  j  a  va  2  s .c  o m
            if (uniqueConstraintAttributeValue.getS() != null) {
                uniqueConstraintAttributeValue.setS(uniqueConstraintAttributeValue.getS().toUpperCase());
            }
            final Map<String, AttributeValue> attributeMap = new HashMap<>();
            attributeMap.put("property", new AttributeValue(uniqueConstraintPropertyName));
            attributeMap.put("value", uniqueConstraintAttributeValue);
            final Map<String, ExpectedAttributeValue> expectedResults = new HashMap<>();
            expectedResults.put("value", new ExpectedAttributeValue(false));
            final String indexTableName = databaseSchemaHolder.schemaName() + "-indexes."
                    + itemConfiguration.tableName();
            final PutItemRequest itemRequest = new PutItemRequest().withTableName(indexTableName)
                    .withItem(attributeMap).withExpected(expectedResults);
            try {
                amazonDynamoDbClient.putItem(itemRequest);
                createdConstraintPropertyDescriptors.add(uniqueConstraintPropertyDescriptor);
            } catch (final ConditionalCheckFailedException e) {
                itemConstraintViolationException = new ItemConstraintViolationException(
                        uniqueConstraintPropertyName,
                        "Unique constraint violation on property '" + uniqueConstraintPropertyName + "' ('"
                                + uniqueConstraintAttributeValue + "') of item " + item.getClass());
                break;
            } catch (final AmazonServiceException e) {
                throw new PersistenceResourceFailureException(
                        "Failure while attempting DynamoDb put (creating unique constraint index entry)", e);
            }
        }
    }
    if (itemConstraintViolationException != null) {
        try {
            deleteUniqueConstraintIndexes(item, itemConfiguration, createdConstraintPropertyDescriptors);
        } catch (final Exception e) {
            logger.error(e.getMessage(), e);
        }
        throw itemConstraintViolationException;
    }
    return createdConstraintPropertyDescriptors;
}

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

License:Apache License

protected final <T extends Item> void deleteUniqueConstraintIndexes(final T item,
        final ItemConfiguration itemConfiguration,
        final Collection<PropertyDescriptor> constraintPropertyDescriptors) {
    if (constraintPropertyDescriptors.isEmpty()) {
        return;/* w  ww.  j  av  a 2s. co m*/
    }
    for (final UniqueConstraint uniqueConstraint : itemConfiguration.uniqueConstraints()) {
        final String uniqueConstraintPropertyName = uniqueConstraint.propertyName();
        final PropertyDescriptor uniqueConstraintPropertyDescriptor = uniqueConstraint.propertyDescriptor();
        if (constraintPropertyDescriptors.contains(uniqueConstraintPropertyDescriptor)) {
            final AttributeValue uniqueConstraintAttributeValue = DynamoDbPropertyMarshaller.getValue(item,
                    uniqueConstraintPropertyDescriptor);
            if (uniqueConstraintAttributeValue != null) {
                if (uniqueConstraintAttributeValue.getS() != null) {
                    uniqueConstraintAttributeValue.setS(uniqueConstraintAttributeValue.getS().toUpperCase());
                }
                final Map<String, AttributeValue> key = new HashMap<>();
                key.put("property", new AttributeValue(uniqueConstraintPropertyName));
                key.put("value", uniqueConstraintAttributeValue);
                final String indexTableName = databaseSchemaHolder.schemaName() + "-indexes."
                        + itemConfiguration.tableName();
                final DeleteItemRequest itemRequest = new DeleteItemRequest().withTableName(indexTableName)
                        .withKey(key);
                try {
                    amazonDynamoDbClient.deleteItem(itemRequest);
                } catch (final AmazonServiceException e) {
                    throw new PersistenceResourceFailureException(
                            "Failed while attempting to perform DynamoDb Delete (for unique constraints)", e);
                }
            }
        }
    }
}

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

License:Apache License

private <T extends Item> T marshallIntoObject(final Class<T> itemClass,
        final Map<String, AttributeValue> itemAttributeMap) throws ItemClassDiscriminatorMismatchException {
    ItemConfiguration itemConfiguration = getItemConfiguration(itemClass);
    Class<? extends T> actualItemClass = itemClass;
    if (ParentItemConfiguration.class.isAssignableFrom(itemConfiguration.getClass())) {
        final ParentItemConfiguration parentItemConfiguration = (ParentItemConfiguration) itemConfiguration;
        final AttributeValue discriminatorAttribute = itemAttributeMap
                .get(parentItemConfiguration.discriminator());
        if (discriminatorAttribute != null) {
            actualItemClass = parentItemConfiguration.getVariantItemClass(discriminatorAttribute.getS());
            itemConfiguration = getItemConfiguration(actualItemClass);
        }/* w w  w  .j  av a  2 s  .co m*/
    } else if (VariantItemConfiguration.class.isAssignableFrom(itemConfiguration.getClass())) {
        final VariantItemConfiguration variantItemConfiguration = (VariantItemConfiguration) itemConfiguration;
        final AttributeValue discriminatorAttribute = itemAttributeMap
                .get(variantItemConfiguration.parentItemConfiguration().discriminator());
        if (discriminatorAttribute == null || !((VariantItemConfiguration) itemConfiguration)
                .discriminatorValue().equals(discriminatorAttribute.getS())) {
            throw new ItemClassDiscriminatorMismatchException();
        }
    }
    try {
        final T item = actualItemClass.newInstance();
        for (final PropertyDescriptor propertyDescriptor : itemConfiguration.propertyDescriptors()) {
            final AttributeValue attributeValue = itemAttributeMap.get(propertyDescriptor.getName());
            DynamoDbPropertyMarshaller.setValue(item, propertyDescriptor, attributeValue);
        }
        return item;
    } catch (final Exception e) {
        throw new IllegalStateException(e);
    }
}

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

License:Apache License

public <T extends Item> Collection<UniqueConstraint> getUpdatedUniqueConstraints(final T item,
        final T previousItem, final ItemConfiguration itemConfiguration) {
    final Map<String, AttributeValue> previousItemAttributeMap = getAttributeMap(previousItem,
            itemConfiguration, item.getVersion());
    if (!previousItemAttributeMap.get(VERSION_ATTRIBUTE).getN().equals(String.valueOf(item.getVersion()))) {
        throw new ConditionalCheckFailedException("Version attribute has changed: Conflict!");
    }/*from   w ww.  j av  a 2s. c  om*/
    final Map<String, AttributeValue> updateItemAttributeMap = getAttributeMap(item, itemConfiguration,
            item.getVersion());
    final Collection<String> updatedProperties = getUpdateProperties(previousItemAttributeMap,
            updateItemAttributeMap);
    final Collection<UniqueConstraint> updatedUniqueConstraints = new HashSet<>();
    for (final UniqueConstraint uniqueConstraint : itemConfiguration.uniqueConstraints()) {
        final String propertyName = uniqueConstraint.propertyDescriptor().getName();
        if (updatedProperties.contains(propertyName)) {
            final AttributeValue previousAttributeValue = previousItemAttributeMap.get(propertyName);
            final AttributeValue updatedAttributeValue = updateItemAttributeMap.get(propertyName);
            if (previousAttributeValue == null || updatedAttributeValue == null
                    || !previousAttributeValue.getS().equalsIgnoreCase(updatedAttributeValue.getS())) {
                updatedUniqueConstraints.add(uniqueConstraint);
            }
        }
    }
    return updatedUniqueConstraints;
}