I'm trying to put an item in a DynamoDB table. I'm basically swapping usage of update item with put item which has the exact same logic as below, but for put item, I am getting errors:
ValidationException: ExpressionAttributeNames can only be specified when using expressions
Below is the code:
// Puts/updates item into DynamoDB table
public boolean setSessionString(String sessionId, String clientDomain, Map<String, String> keyValueMap,
long ttl) {
// TTL since epoch calculation
long timeToLiveInSeconds = ttl == 0 ? DEFAULT_TTL : ttl;
long expirationTime = System.currentTimeMillis() / 1000L + timeToLiveInSeconds;
try {
// Construct putItemSpec
PutItemSpec putItemSpec = createPutItemSpec(
createPrimaryKey(sessionId, clientDomain), keyValueMap, expirationTime);
table.putItem(putItemSpec);
return true;
} catch (final Exception e) {
throw DependencyException.builder()
.withMessage("DynamoDB update/put item error: " + e.getMessage())
.withCause(e.getCause())
.build();
}
}
// Enumerate key-value pairs to escape DynamoDB reserved keyword
private PutItemSpec createPutItemSpec(PrimaryKey primaryKey, Map<String, String> keyValueMap, long expirationTime) {
Item item = new Item().withPrimaryKey(primaryKey);
PutItemSpec putItemSpec = new PutItemSpec();
List<String> keyList = new ArrayList<String>(keyValueMap.keySet());
NameMap nameMap = new NameMap();
ValueMap valueMap = new ValueMap();
// Enumerating key-value entries
for (int keyValueIndex = 0; keyValueIndex < keyList.size(); keyValueIndex++) {
String keyEnum = "#k" + keyValueIndex;
String valueEnum = ":v" + keyValueIndex;
String key = keyList.get(keyValueIndex);
nameMap.with(keyEnum, key);
valueMap.with(valueEnum, keyValueMap.get(key));
}
// Appending ttl attribute
String ttlAttributeEscape = "#" + RESERVED_TTL_ATTRIBUTE;
String ttlValueEscape = ":" + RESERVED_TTL_ATTRIBUTE;
nameMap.with(ttlAttributeEscape, RESERVED_TTL_ATTRIBUTE);
valueMap.withLong(ttlValueEscape, expirationTime);
return putItemSpec.withNameMap(nameMap)
.withValueMap(valueMap)
.withItem(item);
}
Any suggestions on what I might be missing? There could be a key difference between update and put item spec perhaps.