List of usage examples for com.amazonaws.services.dynamodbv2.model AttributeValue getM
public java.util.Map<String, AttributeValue> getM()
An attribute of type Map.
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/*from w w w. j a va2s. 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; }// ww w . j ava 2s . c om 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.rapid7.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 w w .ja v a 2 s . c om*/ */ public static AttributeValue clone(AttributeValue val, IdentityHashMap<AttributeValue, AttributeValue> sourceDestinationMap) { if (val == null) { return null; } if (sourceDestinationMap.containsKey(val)) { return sourceDestinationMap.get(val); } 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) { 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) { 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.rapid7.diskstorage.dynamodb.DynamoDBDelegate.java
License:Open Source License
/**Calculate attribute value size*/ private static int calculateAttributeSizeInBytes(AttributeValue value) { int attrValSize = 0; if (value == null) { return attrValSize; }/*from w w w. j ava2 s . c o m*/ if (value.getB() != null) { ByteBuffer b = value.getB(); attrValSize += b.remaining(); } else if (value.getS() != null) { 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) { List<ByteBuffer> bs = value.getBS(); for (ByteBuffer b : bs) { if (b != null) { attrValSize += b.remaining(); } } } else if (value.getSS() != null) { List<String> ss = value.getSS(); for (String s : ss) { if (s != null) { attrValSize += s.getBytes(UTF8).length; } } } else if (value.getNS() != null) { 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) { 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:dynamok.sink.DynamoDbSinkTask.java
License:Apache License
private void insert(ValueSource valueSource, Schema schema, Object value, PutRequest put) { final AttributeValue attributeValue; try {// www. j ava 2 s . com attributeValue = schema == null ? AttributeValueConverter.toAttributeValueSchemaless(value) : AttributeValueConverter.toAttributeValue(schema, value); } catch (DataException e) { log.error("Failed to convert record with schema={} value={}", schema, value, e); throw e; } final String topAttributeName = valueSource.topAttributeName(config); if (!topAttributeName.isEmpty()) { put.addItemEntry(topAttributeName, attributeValue); } else if (attributeValue.getM() != null) { put.setItem(attributeValue.getM()); } else { throw new ConnectException("No top attribute name configured for " + valueSource + ", and it could not be converted to Map: " + attributeValue); } }
From source file:jp.classmethod.aws.dynamodb.DynamoDbInternalUtils.java
License:Open Source License
private static AttributeValue clone(AttributeValue val, //NOPMD IdentityHashMap<AttributeValue, AttributeValue> sourceDestinationMap, boolean filterEmptyStrings) { if (val == null) { return null; }/* w w w. j av a 2s .c om*/ if (sourceDestinationMap.containsKey(val)) { return sourceDestinationMap.get(val); } 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) { List<AttributeValue> list = new ArrayList<>(val.getL().size()); for (AttributeValue listItemValue : val.getL()) { if (!sourceDestinationMap.containsKey(listItemValue)) { sourceDestinationMap.put(listItemValue, clone(listItemValue, sourceDestinationMap, filterEmptyStrings)); } AttributeValue destination = sourceDestinationMap.get(listItemValue); if (false == (filterEmptyStrings && destination.getS() != null && destination.getS().isEmpty())) { list.add(destination); } } clonedVal.setL(list); } else if (val.getM() != null) { Map<String, AttributeValue> map = new HashMap<>(val.getM().size()); for (Map.Entry<String, AttributeValue> pair : val.getM().entrySet()) { if (!sourceDestinationMap.containsKey(pair.getValue())) { sourceDestinationMap.put(pair.getValue(), clone(pair.getValue(), sourceDestinationMap, filterEmptyStrings)); } AttributeValue destination = sourceDestinationMap.get(pair.getValue()); if (false == (filterEmptyStrings && destination.getS() != null && destination.getS().isEmpty())) { map.put(pair.getKey(), destination); } } clonedVal.setM(map); } return clonedVal; }
From source file:jp.classmethod.aws.dynamodb.DynamoDbInternalUtils.java
License:Open Source License
/** * Copied from DynamoDB Document SDK InternalUtils.java * * Converts a low-level <code>AttributeValue</code> into a simple value, * which can be one of the followings://from www .j a v a 2 s .c o m * * <ul> * <li>String</li> * <li>Set<String></li> * <li>Number (including any subtypes and primitive types)</li> * <li>Set<Number></li> * <li>byte[]</li> * <li>Set<byte[]></li> * <li>ByteBuffer</li> * <li>Set<ByteBuffer></li> * <li>Boolean or boolean</li> * <li>null</li> * <li>Map<String,T>, where T can be any type on this list but must not * induce any circular reference</li> * <li>List<T>, where T can be any type on this list but must not induce * any circular reference</li> * </ul> * * @throws IllegalArgumentException * if an empty <code>AttributeValue</code> value is specified */ @SuppressWarnings("unchecked") private static <T> T toSimpleValue(AttributeValue value) { //NOPMD if (value == null) { return null; } if (Boolean.FALSE.equals(value.getNULL())) { throw new UnsupportedOperationException("False-NULL is not supported in DynamoDB"); } final T t; if (Boolean.TRUE.equals(value.getNULL())) { t = null; } else if (value.getBOOL() != null) { t = (T) value.getBOOL(); } else if (value.getS() != null) { t = (T) value.getS(); } else if (value.getN() != null) { t = (T) new BigDecimal(value.getN()); } else if (value.getB() != null) { t = (T) copyAllBytesFrom(value.getB()); } else if (value.getSS() != null) { t = (T) new LinkedHashSet<>(value.getSS()); } else if (value.getNS() != null) { Set<BigDecimal> set = new LinkedHashSet<>(value.getNS().size()); set.addAll(value.getNS().stream().map(BigDecimal::new).collect(Collectors.toList())); t = (T) set; } else if (value.getBS() != null) { Set<byte[]> set = new LinkedHashSet<>(value.getBS().size()); set.addAll(value.getBS().stream().map(BinaryUtils::copyAllBytesFrom).collect(Collectors.toList())); t = (T) set; } else if (value.getL() != null) { t = (T) toSimpleList(value.getL()); } else if (value.getM() != null) { t = (T) toSimpleMapValue(value.getM()); } else { throw new IllegalArgumentException("Attribute value must not be empty: " + value); } return t; }
From source file:org.apache.beam.sdk.io.aws.dynamodb.AttributeValueCoder.java
License:Apache License
@Override public void encode(AttributeValue value, OutputStream outStream) throws IOException { if (value.getS() != null) { StringUtf8Coder.of().encode(AttributeValueType.s.toString(), outStream); StringUtf8Coder.of().encode(value.getS(), outStream); } else if (value.getN() != null) { StringUtf8Coder.of().encode(AttributeValueType.n.toString(), outStream); StringUtf8Coder.of().encode(value.getN(), outStream); } else if (value.getBOOL() != null) { StringUtf8Coder.of().encode(AttributeValueType.bOOL.toString(), outStream); BooleanCoder.of().encode(value.getBOOL(), outStream); } else if (value.getB() != null) { StringUtf8Coder.of().encode(AttributeValueType.b.toString(), outStream); ByteArrayCoder.of().encode(convertToByteArray(value.getB()), outStream); } else if (value.getSS() != null) { StringUtf8Coder.of().encode(AttributeValueType.sS.toString(), outStream); LIST_STRING_CODER.encode(value.getSS(), outStream); } else if (value.getNS() != null) { StringUtf8Coder.of().encode(AttributeValueType.nS.toString(), outStream); LIST_STRING_CODER.encode(value.getNS(), outStream); } else if (value.getBS() != null) { StringUtf8Coder.of().encode(AttributeValueType.bS.toString(), outStream); LIST_BYTE_CODER.encode(convertToListByteArray(value.getBS()), outStream); } else if (value.getL() != null) { StringUtf8Coder.of().encode(AttributeValueType.l.toString(), outStream); LIST_ATTRIBUTE_CODER.encode(value.getL(), outStream); } else if (value.getM() != null) { StringUtf8Coder.of().encode(AttributeValueType.m.toString(), outStream); MAP_ATTRIBUTE_CODER.encode(value.getM(), outStream); } else if (value.getNULL() != null) { StringUtf8Coder.of().encode(AttributeValueType.nULLValue.toString(), outStream); BooleanCoder.of().encode(value.getNULL(), outStream); } else {//w ww. ja v a 2 s.com throw new CoderException("Unknown Type"); } }
From source file:org.apache.metamodel.dynamodb.DynamoDbUtils.java
License:Apache License
public static Object toValue(AttributeValue a) { if (a == null || Boolean.TRUE == a.isNULL()) { return null; }//from w w w .j av a 2s . c om // dynamo is a bit funky this way ... it has a getter for each possible // data type. return firstNonNull(a.getB(), a.getBOOL(), a.getBS(), a.getL(), a.getM(), a.getN(), a.getNS(), a.getS(), a.getSS()); }
From source file:org.iternine.jeppetto.dao.dynamodb.ConversionUtil.java
License:Apache License
public static Object fromAttributeValue(final AttributeValue attributeValue, final Class targetType, final Class collectionType) { // TODO: byte[] if (String.class.isAssignableFrom(targetType)) { return attributeValue.getS(); } else if (Integer.class.isAssignableFrom(targetType) || int.class.isAssignableFrom(targetType)) { return Integer.valueOf(attributeValue.getN()); } else if (Long.class.isAssignableFrom(targetType) || long.class.isAssignableFrom(targetType)) { return Long.valueOf(attributeValue.getN()); } else if (Double.class.isAssignableFrom(targetType) || double.class.isAssignableFrom(targetType)) { return Double.valueOf(attributeValue.getN()); } else if (Float.class.isAssignableFrom(targetType) || float.class.isAssignableFrom(targetType)) { return Float.valueOf(attributeValue.getN()); } else if (Boolean.class.isAssignableFrom(targetType) || boolean.class.isAssignableFrom(targetType)) { return attributeValue.getBOOL(); } else if (Character.class.isAssignableFrom(targetType) || char.class.isAssignableFrom(targetType)) { return attributeValue.getS().charAt(0); } else if (Byte.class.isAssignableFrom(targetType) || byte.class.isAssignableFrom(targetType)) { return Byte.valueOf(attributeValue.getN()); } else if (Short.class.isAssignableFrom(targetType) || short.class.isAssignableFrom(targetType)) { return Short.valueOf(attributeValue.getN()); } else if (Date.class.isAssignableFrom(targetType)) { return new Date(Long.parseLong(attributeValue.getN())); } else if (Enum.class.isAssignableFrom(targetType)) { //noinspection unchecked return Enum.valueOf((Class<Enum>) targetType, attributeValue.getS()); } else if (Set.class.isAssignableFrom(targetType)) { Set result = new HashSet(); List<String> strings; if (attributeValue.getSS() != null) { strings = attributeValue.getSS(); } else {/* w ww . ja va2s. c om*/ strings = attributeValue.getNS(); } for (String string : strings) { //noinspection unchecked result.add(fromString(string, collectionType)); } return result; } else if (List.class.isAssignableFrom(targetType)) { List<AttributeValue> attributeValues = attributeValue.getL(); List result = new PersistableList(attributeValues.size()); for (AttributeValue value : attributeValues) { // NB: We currently only pick up the first type level for an attribute value. So if, for example, // someone had a List<List<Integer>>, we would not be able to rebuild it. The solution is // TODO: change the way this works to handle nested types? //noinspection unchecked result.add(fromAttributeValue(value, collectionType, null)); } return result; } else if (Map.class.isAssignableFrom(targetType)) { Map<String, AttributeValue> attributeValues = attributeValue.getM(); Map result = new PersistableMap(attributeValues.size()); for (Map.Entry<String, AttributeValue> entry : attributeValues.entrySet()) { //noinspection unchecked result.put(entry.getKey(), fromAttributeValue(entry.getValue(), collectionType, null)); } return result; } else { return getObjectFromItem(attributeValue.getM(), targetType); } }