DynamoDB + Quarkus - Odd type conversion

Viewed 226

I have a table / entity on DynamoDB that should be mapped with number as fields.

Example:

@RegisterForReflection
Foo {
     Integer myfield;
     ...
}

I noticed that the AWS SDK used by quarkus-amazon-dynamodb dependency is built in a odd way.

For example,the n() method of software.amazon.awssdk.services.dynamodb.model.AttributeValue

return a java String instead of a Number type, so I have to convert the result with a clumsy Integer.parseInt()

public static Foo from(Map<String, AttributeValue> item) {
    final var output = new Foo();
    if (item != null && !item.isEmpty()) {
        output.setMyField(Integer.parseInt(item.get(MY_FIELD).n()));
    }
    return output;
}

The same happens if I have to fetch an item and use

AttributeValue.builder().n()

final Map<String, AttributeValue> key = new HashMap<>();
    key.put(Foo.MY_FIELD, AttributeValue.builder().n(input.toString()).build()); // too bad!!

    return GetItemRequest.builder()
            .tableName(TABLE_NAME)
            .key(key)
            .attributesToGet(Foo.MY_FIELD)
            .build();

Do I miss something ?

PS

The Quarkus dynamoDB documentation is here

1 Answers

It's not odd - that's just how AWS DynamoDB works. Numbers are represented as strings in DynamoDB and Quarkus has nothing to do with that. See the API reference:

N
An attribute of type Number. For example:

"N": "123.45"

Numbers are sent across the network to DynamoDB as strings, to maximize compatibility across languages and libraries. However, DynamoDB treats them as number type attributes for mathematical operations.

Type: String

Related