Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing an List of Objects in DynamoDB and retrieving them partially

Tags:

This might two separate questions into a single one. But that's my current use case. I have a POJO which has specific attributes.

It might look like:

class MyObject {
    String id;
    String name;
    int size;

getters and setters () ....
}

Now I have a list of these objects - List which I wish to store in a single in Dynamo. Something like

RecordID            Attributes
abcd123             List<MyObject>

Can I store the data in Dynamo in the above format? My second question is related to the above use case.

Now that I have stored the records, can I retrieve a specific part of the List? Like, I want indices 0 - 5 to be returned from the DyanmoDBQuery Request and not the entire list itself.

Is that possible?

I am pretty new to DynamoDB and I am not sure how well there is support for storing custom objects in Dynamo.

like image 749
chrisrhyno2003 Avatar asked Oct 04 '16 20:10

chrisrhyno2003


People also ask

What are the two ways of retrieving data from a DynamoDB table?

In Amazon DynamoDB, you can use either the DynamoDB API, or PartiQL, a SQL-compatible query language, to query an item from a table. With Amazon DynamoDB the Query action lets you retrieve data in a similar fashion. The Query action provides quick, efficient access to the physical locations where the data is stored.

Can DynamoDB store a list?

DynamoDB supports the Java Set , List , and Map collection types. The following table summarizes how these Java types map to the DynamoDB types.

What is the API call to retrieve multiple items from a DynamoDB table?

The BatchGetItem operation returns the attributes of one or more items from one or more tables. You identify requested items by primary key. A single operation can retrieve up to 16 MB of data, which can contain as many as 100 items.

Which is the most efficient operation to retrieve data from a DynamoDB table?

GetItem – Retrieves a single item from a table. This is the most efficient way to read a single item because it provides direct access to the physical location of the item.


1 Answers

If you are able to massage your schema and move to a Partition-Sort key schema, where the Partition key is RecordID and the Sort key is ItemNumber, where Item number is the index of each MyObject in the abcd123 list above, then you can get sublists with a range query:

Item example:

RecordID (partition key) asdfasdfadsf
ItemNumber (sort key)    0
Item                     an instance of MyObject

Range query parameters:

KeyCondition: RecordId = :rid AND ItemNumber BETWEEN :lower AND :upper
ExpressionAttributeValues = { ":rid": "asdfasdfasdf", ":lower": 0, ":upper": 5 }

Note that the :upper bound is inclusive, so the query example above will return MyObjects with RecordId=asdfasdfasdf with ItemNumbers 0, 1, 2, 3, 4, and 5.

like image 75
Alexander Patrikalakis Avatar answered Sep 26 '22 17:09

Alexander Patrikalakis