0

I am trying to do a scan of a DynamoDB table and then display user selected fields in a table. The problem is how can I do this if data is returned as AttributeValues, since not all values will be a string, and I have no idea what type each value is before hand.

The code I have:

 ScanRequest request =
            ScanRequest
            .builder()
            .tableName(tableName))
            .build();

    ScanIterable response = client.scanPaginator(request);
    List<Map<String, AttributeValue>> data = new ArrayList<>();

    for (ScanResponse page : response) {
        for (Map<String, AttributeValue> item : page.items()) {
            data.add(item);
        }
    }

then to get the values:

       for (Map<String, AttributeValue> map : items) {
        for (Map.Entry<String, AttributeValue> entry : map.entrySet()) {
            //get the entry.getValue() and append it to a string
        }
    }

If I use:

entry.getValue().toString()

I will get a string containing the attribute value type and value e.g 'N': '127' If I use:

entry.getValue().s()

I will get null values when the type is not 'S'

1 Answer 1

1

In that case as you do not want to deal with the DynamoDB JSON you should use a Document Interface.

Many AWS SDKs provide a document interface, allowing you to perform data plane operations (create, read, update, delete) on tables and indexes. With a document interface, you do not need to specify Data type descriptors. The data types are implied by the semantics of the data itself. These AWS SDKs also provide methods to easily convert JSON documents to and from native Amazon DynamoDB data types.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any examples of this, I had a read through the document API and it still seems like I would have to know the data type beforehand i.e Document.asString() or Document.asNumber()
Or is it just going to require a bunch of if statements to get the type beforehand i.e Document.isString() -> Document.asString()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.