List of usage examples for com.amazonaws.services.dynamodbv2.model AttributeValue withN
public AttributeValue withN(String n)
An attribute of type Number.
From source file:com.clicktravel.infrastructure.persistence.aws.dynamodb.DynamoDbTemplate.java
License:Apache License
private Map<String, AttributeValue> generateKey(final ItemId itemId, final ItemConfiguration itemConfiguration) { final PrimaryKeyDefinition primaryKeyDefinition = itemConfiguration.primaryKeyDefinition(); final AttributeValue keyValue = new AttributeValue(); final ScalarAttributeType keyAttributeType = getAttributeType(primaryKeyDefinition.propertyType()); switch (keyAttributeType) { case N:/*from w w w . java 2 s . co m*/ keyValue.withN(itemId.value()); break; default: keyValue.withS(itemId.value()); break; } final Map<String, AttributeValue> key = new HashMap<>(); key.put(primaryKeyDefinition.propertyName(), keyValue); final AttributeValue supportingKeyValue = new AttributeValue(); if (CompoundPrimaryKeyDefinition.class.isAssignableFrom(primaryKeyDefinition.getClass())) { final CompoundPrimaryKeyDefinition compoundPrimaryKeyDefinition = (CompoundPrimaryKeyDefinition) primaryKeyDefinition; final ScalarAttributeType supportingKeyAttributeType = getAttributeType( compoundPrimaryKeyDefinition.propertyType()); switch (supportingKeyAttributeType) { case N: supportingKeyValue.withN(itemId.supportingValue()); break; default: supportingKeyValue.withS(itemId.supportingValue()); break; } key.put(compoundPrimaryKeyDefinition.supportingPropertyName(), supportingKeyValue); } return key; }
From source file:com.makariev.dynamodb.preferences.UserPreferenceLowLevelAPIService.java
License:Open Source License
@Override public void delete(UserPreference up) { //Key primaryKey = new Key().withHashKeyElement(up); Map<String, AttributeValue> primaryKey = new HashMap<>(); AttributeValue avKey = new AttributeValue(); avKey.withN(String.valueOf(up.getUserNo())); primaryKey.put("userNo", avKey); DeleteItemRequest request = new DeleteItemRequest().withTableName(UserPreference.TABLE_NAME) .withKey(primaryKey);//w w w .j a va 2s .c o m dynamoDb.deleteItem(request); }
From source file:com.makariev.dynamodb.preferences.UserPreferenceLowLevelAPIService.java
License:Open Source License
@Override public UserPreference findByUserNo(int userNo) { Map<String, AttributeValue> primaryKey = new HashMap<>(); AttributeValue avKey = new AttributeValue(); avKey.withN(String.valueOf(userNo)); primaryKey.put("userNo", avKey); //Key primaryKey = new Key().withHashKeyElement(userNoAttr); GetItemRequest request = new GetItemRequest().withTableName(UserPreference.TABLE_NAME).withKey(primaryKey); GetItemResult result = dynamoDb.getItem(request); Map userPreference = result.getItem(); return toUserPreference(userPreference); }
From source file:io.apptik.json.aws.DynamoDb2Item.java
License:Apache License
@Override protected AttributeValue get(JsonElement el, String key) { AttributeValue atVal = new AttributeValue(); if (el.isNumber()) { atVal.withN(el.toString()); } else if (el.isJsonArray()) { boolean isNumberArr = true; ArrayList<String> attrList = new ArrayList<String>(); for (JsonElement arrEl : el.asJsonArray()) { if (isNumberArr && !arrEl.isNumber()) isNumberArr = false;/*from w w w . j av a2 s.co m*/ attrList.add(arrEl.toString()); } if (isNumberArr) { atVal.withNS(attrList); } else { atVal.withSS(attrList); } } else { atVal.withS(el.toString()); } return atVal; }
From source file:jp.classmethod.aws.dynamodb.DynamoDbInternalUtils.java
License:Open Source License
/** * Copied from DynamoDB Document SDK InternalUtils.java * * Converts a simple value into the low-level <code><AttributeValue/code> * representation.//w w w . j a v a 2s.c om * * @param value * the given value which can be one of the followings: * <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> * @return a non-null low level representation of the input object value * * @throws UnsupportedOperationException * if the input object type is not supported */ public static AttributeValue toAttributeValue(Object value) { //NOPMD AttributeValue result = new AttributeValue(); if (value == null) { return result.withNULL(Boolean.TRUE); } else if (value instanceof Boolean) { return result.withBOOL((Boolean) value); } else if (value instanceof String) { return result.withS((String) value); } else if (value instanceof BigDecimal || value instanceof Number) { return result.withN(value instanceof Number ? value.toString() : ((BigDecimal) value).toPlainString()); } else if (value instanceof byte[]) { return result.withB(ByteBuffer.wrap((byte[]) value)); } else if (value instanceof ByteBuffer) { return result.withB((ByteBuffer) value); } else if (value instanceof Set) { // default to an empty string set if there is no element @SuppressWarnings("unchecked") Set<Object> set = (Set<Object>) value; if (set.isEmpty()) { result.setSS(new LinkedHashSet<>()); return result; } Object element = set.iterator().next(); if (element instanceof String) { @SuppressWarnings("unchecked") Set<String> ss = (Set<String>) value; result.setSS(new ArrayList<>(ss)); } else if (element instanceof Number) { @SuppressWarnings("unchecked") Set<Number> in = (Set<Number>) value; List<String> out = new ArrayList<>(set.size()); for (Number n : in) { BigDecimal bd = toBigDecimal(n); out.add(bd.toPlainString()); } result.setNS(out); } else if (element instanceof byte[]) { @SuppressWarnings("unchecked") Set<byte[]> in = (Set<byte[]>) value; List<ByteBuffer> out = new ArrayList<>(set.size()); for (byte[] buf : in) { out.add(ByteBuffer.wrap(buf)); } result.setBS(out); } else if (element instanceof ByteBuffer) { @SuppressWarnings("unchecked") Set<ByteBuffer> bs = (Set<ByteBuffer>) value; result.setBS(bs); } else { throw new UnsupportedOperationException("element type: " + element.getClass()); } } else if (value instanceof List) { @SuppressWarnings("unchecked") List<Object> in = (List<Object>) value; List<AttributeValue> out = new ArrayList<>(); for (Object v : in) { out.add(toAttributeValue(v)); } result.setL(out); } else if (value instanceof Map) { @SuppressWarnings("unchecked") Map<String, Object> in = (Map<String, Object>) value; if (false == in.isEmpty()) { for (Map.Entry<String, Object> e : in.entrySet()) { result.addMEntry(e.getKey(), toAttributeValue(e.getValue())); } } else { // empty map result.setM(new LinkedHashMap<>()); } } else { throw new UnsupportedOperationException("value type: " + value.getClass()); } return result; }
From source file:org.socialsignin.spring.data.dynamodb.repository.query.AbstractDynamoDBQueryCriteria.java
License:Apache License
protected <P> List<AttributeValue> addAttributeValue(List<AttributeValue> attributeValueList, Object attributeValue, String propertyName, Class<P> propertyType, boolean expandCollectionValues) { AttributeValue attributeValueObject = new AttributeValue(); if (ClassUtils.isAssignable(String.class, propertyType)) { List<String> attributeValueAsList = getAttributeValueAsList(attributeValue); if (expandCollectionValues && attributeValueAsList != null) { attributeValueObject.withSS(attributeValueAsList); } else {/*from w w w .j a v a 2s .co m*/ attributeValueObject.withS((String) attributeValue); } } else if (ClassUtils.isAssignable(Number.class, propertyType)) { List<Number> attributeValueAsList = getAttributeValueAsList(attributeValue); if (expandCollectionValues && attributeValueAsList != null) { List<String> attributeValueAsStringList = getNumberListAsStringList(attributeValueAsList); attributeValueObject.withNS(attributeValueAsStringList); } else { attributeValueObject.withN(attributeValue.toString()); } } else if (ClassUtils.isAssignable(Boolean.class, propertyType)) { List<Boolean> attributeValueAsList = getAttributeValueAsList(attributeValue); if (expandCollectionValues && attributeValueAsList != null) { List<String> attributeValueAsStringList = getBooleanListAsStringList(attributeValueAsList); attributeValueObject.withNS(attributeValueAsStringList); } else { boolean boolValue = ((Boolean) attributeValue).booleanValue(); attributeValueObject.withN(boolValue ? "1" : "0"); } } else if (ClassUtils.isAssignable(Date.class, propertyType)) { List<Date> attributeValueAsList = getAttributeValueAsList(attributeValue); if (expandCollectionValues && attributeValueAsList != null) { List<String> attributeValueAsStringList = getDateListAsStringList(attributeValueAsList); attributeValueObject.withSS(attributeValueAsStringList); } else { Date date = (Date) attributeValue; String marshalledDate = new Date2IsoDynamoDBMarshaller().marshall(date); attributeValueObject.withS(marshalledDate); } } else if (ClassUtils.isAssignable(Instant.class, propertyType)) { List<Instant> attributeValueAsList = getAttributeValueAsList(attributeValue); if (expandCollectionValues && attributeValueAsList != null) { List<String> attributeValueAsStringList = getInstantListAsStringList(attributeValueAsList); attributeValueObject.withSS(attributeValueAsStringList); } else { Instant date = (Instant) attributeValue; String marshalledDate = new Instant2IsoDynamoDBMarshaller().marshall(date); attributeValueObject.withS(marshalledDate); } } else { throw new RuntimeException("Cannot create condition for type:" + attributeValue.getClass() + " property conditions must be String,Number or Boolean, or have a DynamoDBMarshaller configured"); } attributeValueList.add(attributeValueObject); return attributeValueList; }