Java Code Examples for software.amazon.awssdk.services.dynamodb.model.AttributeValue#n()

The following examples show how to use software.amazon.awssdk.services.dynamodb.model.AttributeValue#n() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FloatAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Float transformTo(AttributeValue input) {
    Float result;
    if (input.n() != null) {
        result = EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    } else {
        result = EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
    }

    ConverterUtils.validateFloat(result);
    return result;
}
 
Example 2
Source File: DynamoUtils.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
public static Long safeGetLong(Map<String, AttributeValue> dynamoRecord, String key) {
    AttributeValue av = dynamoRecord.get(key);
    if (av == null) {
        return null;
    } else {
        return new Long(av.n());
    }
}
 
Example 3
Source File: AWSDynamoDAO.java    From para with Apache License 2.0 5 votes vote down vote up
private boolean updateRow(String key, String appid, Map<String, AttributeValue> row) {
	if (StringUtils.isBlank(key) || StringUtils.isBlank(appid) || row == null || row.isEmpty()) {
		return false;
	}
	String table = getTableNameForAppid(appid);
	try {
		UpdateItemRequest.Builder updateRequest = UpdateItemRequest.builder();
		StringBuilder updateExpression = new StringBuilder("SET ");
		Map<String, String> names = new HashMap<>(row.size() + 1);
		Map<String, AttributeValue> values = new HashMap<>(row.size() + 1);
		boolean isLockingEnabledForRow = false;
		AttributeValue version = row.remove(Config._VERSION); // ignore the version field here
		if (version == null || version.n() == null) {
			version = AttributeValue.builder().n("0").build();
		}
		if (Long.parseLong(version.n()) > 0L) {
			isLockingEnabledForRow = true;
		}
		for (Entry<String, AttributeValue> attr : row.entrySet()) {
			String name = "#" + attr.getKey();
			String value = ":" + attr.getKey();
			updateExpression.append(name).append("=").append(value).append(",");
			names.put(name, attr.getKey());
			values.put(value, attr.getValue());
		}
		updateExpression.setLength(updateExpression.length() - 1); // remove comma at the end

		if (isLockingEnabledForRow) {
			names.put("#" + Config._VERSION, Config._VERSION);
			values.put(":" + Config._VERSION, version);
			values.put(":plusOne", AttributeValue.builder().n("1").build());
			updateRequest.conditionExpression("#" + Config._VERSION + " = :" + Config._VERSION);
			updateExpression.append(" ADD #").append(Config._VERSION).append(" :plusOne");
		}

		updateRequest.tableName(table);
		updateRequest.key(rowKey(key, appid));
		updateRequest.expressionAttributeNames(names);
		updateRequest.expressionAttributeValues(values);
		updateRequest.updateExpression(updateExpression.toString());
		client().updateItem(updateRequest.build());
		return true;
	} catch (ConditionalCheckFailedException ex) {
		logger.warn("Item not updated - versions don't match. table={}, appid={}, key={}.", table, appid, key);
	} catch (Exception e) {
		logger.error("Could not update row in DB - table={}, appid={}, key={}:", table, appid, key, e);
		throwIfNecessary(e);
	}
	return false;
}
 
Example 4
Source File: DirectKmsMaterialsProvider.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts relevant information from {@code context} and uses it to populate fields in
 * {@code kmsEc}. Subclass can override the default implementation to provide an alternative
 * encryption context in calls to KMS. Currently, the default implementation includes these fields:
 * <dl>
 * <dt>{@code HashKeyName}</dt>
 * <dd>{@code HashKeyValue}</dd>
 * <dt>{@code RangeKeyName}</dt>
 * <dd>{@code RangeKeyValue}</dd>
 * <dt>{@link #TABLE_NAME_EC_KEY}</dt>
 * <dd>{@code TableName}</dd>
 */
protected void populateKmsEcFromEc(EncryptionContext context, Map<String, String> kmsEc) {
    final String hashKeyName = context.getHashKeyName();
    if (hashKeyName != null) {
        final AttributeValue hashKey = context.getAttributeValues().get(hashKeyName);
        if (hashKey.n() != null) {
            kmsEc.put(hashKeyName, hashKey.n());
        } else if (hashKey.s() != null) {
            kmsEc.put(hashKeyName, hashKey.s());
        } else if (hashKey.b() != null) {
            kmsEc.put(hashKeyName, Base64.encodeToString(hashKey.b().asByteArray()));
        } else {
            throw new UnsupportedOperationException("DirectKmsMaterialsProvider only supports String, Number, and Binary HashKeys");
        }
    }
    final String rangeKeyName = context.getRangeKeyName();
    if (rangeKeyName != null) {
        final AttributeValue rangeKey = context.getAttributeValues().get(rangeKeyName);
        if (rangeKey.n() != null) {
            kmsEc.put(rangeKeyName, rangeKey.n());
        } else if (rangeKey.s() != null) {
            kmsEc.put(rangeKeyName, rangeKey.s());
        } else if (rangeKey.b() != null) {
            kmsEc.put(rangeKeyName, Base64.encodeToString(rangeKey.b().asByteArray()));
        } else {
            throw new UnsupportedOperationException("DirectKmsMaterialsProvider only supports String, Number, and Binary RangeKeys");
        }
    }

    final String tableName = context.getTableName();
    if (tableName != null) {
        kmsEc.put(TABLE_NAME_EC_KEY, tableName);
    }
}
 
Example 5
Source File: AttributeValueCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(AttributeValue value, OutputStream outStream) throws IOException {
  if (value.s() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.s.toString(), outStream);
    StringUtf8Coder.of().encode(value.s(), outStream);
  } else if (value.n() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.n.toString(), outStream);
    StringUtf8Coder.of().encode(value.n(), outStream);
  } else if (value.bool() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.bool.toString(), outStream);
    BooleanCoder.of().encode(value.bool(), outStream);
  } else if (value.b() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.b.toString(), outStream);
    ByteArrayCoder.of().encode(value.b().asByteArray(), outStream);
  } else if (value.ss() != null && value.ss().size() > 0) {
    StringUtf8Coder.of().encode(AttributeValueType.ss.toString(), outStream);
    LIST_STRING_CODER.encode(value.ss(), outStream);
  } else if (value.ns() != null && value.ns().size() > 0) {
    StringUtf8Coder.of().encode(AttributeValueType.ns.toString(), outStream);
    LIST_STRING_CODER.encode(value.ns(), outStream);
  } else if (value.bs() != null && value.bs().size() > 0) {
    StringUtf8Coder.of().encode(AttributeValueType.bs.toString(), outStream);
    LIST_BYTE_CODER.encode(convertToListByteArray(value.bs()), outStream);
  } else if (value.l() != null && value.l().size() > 0) {
    StringUtf8Coder.of().encode(AttributeValueType.l.toString(), outStream);
    LIST_ATTRIBUTE_CODER.encode(value.l(), outStream);
  } else if (value.m() != null && value.m().size() > 0) {
    StringUtf8Coder.of().encode(AttributeValueType.m.toString(), outStream);
    MAP_ATTRIBUTE_CODER.encode(value.m(), outStream);
  } else if (value.nul() != null) {
    StringUtf8Coder.of().encode(AttributeValueType.nul.toString(), outStream);
    BooleanCoder.of().encode(value.nul(), outStream);
  } else {
    throw new CoderException("Unknown Type");
  }
}
 
Example 6
Source File: IntegerAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Integer transformTo(AttributeValue input) {
    if (input.n() != null) {
        return EnhancedAttributeValue.fromNumber(input.n()).convert(Visitor.INSTANCE);
    }

    return EnhancedAttributeValue.fromAttributeValue(input).convert(Visitor.INSTANCE);
}
 
Example 7
Source File: AtomicIntegerAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public AtomicInteger transformTo(AttributeValue input) {
    if (input.n() != null) {
        return EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    }

    return EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
}
 
Example 8
Source File: OptionalDoubleAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public OptionalDouble transformTo(AttributeValue input) {
    OptionalDouble result;
    if (input.n() != null) {
        result = EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    } else {
        result = EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
    }
    result.ifPresent(ConverterUtils::validateDouble);
    return result;
}
 
Example 9
Source File: EnhancedAttributeValue.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Create an {@link EnhancedAttributeValue} from a generated {@link AttributeValue}.
 *
 * <p>
 * This call will fail with a {@link RuntimeException} if the provided value is null ({@link AttributeValue#nul()} is okay).
 */
public static EnhancedAttributeValue fromAttributeValue(AttributeValue attributeValue) {
    Validate.notNull(attributeValue, "Generated attribute value must not contain null values. " +
                                     "Use AttributeValue#nul() instead.");
    if (attributeValue.s() != null) {
        return EnhancedAttributeValue.fromString(attributeValue.s());
    }
    if (attributeValue.n() != null) {
        return EnhancedAttributeValue.fromNumber(attributeValue.n());
    }
    if (attributeValue.bool() != null) {
        return EnhancedAttributeValue.fromBoolean(attributeValue.bool());
    }
    if (Boolean.TRUE.equals(attributeValue.nul())) {
        return EnhancedAttributeValue.nullValue();
    }
    if (attributeValue.b() != null) {
        return EnhancedAttributeValue.fromBytes(attributeValue.b());
    }
    if (attributeValue.hasM()) {
        return EnhancedAttributeValue.fromMap(attributeValue.m());
    }
    if (attributeValue.hasL()) {
        return EnhancedAttributeValue.fromListOfAttributeValues(attributeValue.l());
    }
    if (attributeValue.hasBs()) {
        return EnhancedAttributeValue.fromSetOfBytes(attributeValue.bs());
    }
    if (attributeValue.hasSs()) {
        return EnhancedAttributeValue.fromSetOfStrings(attributeValue.ss());
    }
    if (attributeValue.hasNs()) {
        return EnhancedAttributeValue.fromSetOfNumbers(attributeValue.ns());
    }

    throw new IllegalStateException("Unable to convert attribute value: " + attributeValue);
}
 
Example 10
Source File: OptionalLongAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public OptionalLong transformTo(AttributeValue input) {
    if (input.n() != null) {
        return EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    }

    return EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
}
 
Example 11
Source File: AtomicLongAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public AtomicLong transformTo(AttributeValue input) {
    if (input.n() != null) {
        return EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    }

    return EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
}
 
Example 12
Source File: DurationAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Duration transformTo(AttributeValue input) {
    if (input.n() != null) {
        return EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    }

    return EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
}
 
Example 13
Source File: DoubleAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Double transformTo(AttributeValue input) {
    Double result;
    if (input.n() != null) {
        result = EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    } else {
        result = EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
    }

    ConverterUtils.validateDouble(result);
    return result;
}
 
Example 14
Source File: BigDecimalAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public BigDecimal transformTo(AttributeValue input) {
    if (input.n() != null) {
        return EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    }
    return EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
}
 
Example 15
Source File: OptionalIntAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public OptionalInt transformTo(AttributeValue input) {
    if (input.n() != null) {
        return EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    }

    return EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
}
 
Example 16
Source File: BigIntegerAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public BigInteger transformTo(AttributeValue input) {
    if (input.n() != null) {
        return EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    }
    return EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
}
 
Example 17
Source File: LongAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Long transformTo(AttributeValue input) {
    if (input.n() != null) {
        return EnhancedAttributeValue.fromNumber(input.n()).convert(VISITOR);
    }

    return EnhancedAttributeValue.fromAttributeValue(input).convert(VISITOR);
}
 
Example 18
Source File: ShortAttributeConverter.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Override
public Short transformTo(AttributeValue input) {
    if (input.n() != null) {
        return EnhancedAttributeValue.fromNumber(input.n()).convert(Visitor.INSTANCE);
    }

    return EnhancedAttributeValue.fromAttributeValue(input).convert(Visitor.INSTANCE);
}