DynamoDB Scan & JSON Serialization Flashcards
DynamoDB scan() method…
grants you maximum flexibility over the data you can access from a table. It searches the table and retrieves the values that meet a condition on any attribute.
note:
But cost in runtime is HUGE, therefore not very practical - in practical terms, devs use query
Serialization
allows you to store, share, and persist data across networks and runtime sessions
limitation on query() method
you can only perform a query on the table’s key attributes:
The partition key must always be equal to some value (one value), and you can additionally set a condition on the sort key.
You cannot use query() to retrieve data based on non-key vaues, which limits the questions you can ask.
We can only retrieve results that have the same partition key.
We cannot return all the orders on the table.
You need to define both partition and sort key on the table.
limitation on the scan() method
The scan() method reads every item in a table or "secondary index". The filter is applied after the scan finishes, but before the results are returned.
You can use it on tables that have just the partition key (which is good)
Does not require any key values, by default returns every item in the table.
example of scan() retrieving all data
- declare mapper - DynamoDBMapper
- declare scanExpression - DynamoDBScanExpression
- mapper.scan(.class, scanExpression)
^^PaginatedScanList - DynamoDBMapper mapper = new DynamoDBMapper(DynamoDbClientProvider.getDnamoDBClient);
- DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
- PaginatedScanList orderList = mapper.scan(Order.class, scanExpression);
example of scanExpression with filter
//first create a Map for the filter AttributeValue Map valueMap = new HashMap<>();
//next populate the map with named conditions, the //name follows the “:”
valueMap.put(“:minCost”, new
AttributeValue().withN(“30.00));
valueMap.put(“:maxCost”, new
AttributeValue().withN(“70.00”));
//finally, set the scanExpression using:
a) .withFilterExpression
b) .withExpressionAttributeValues
DynamoDBExpression scanExpression = new DynamoDBExpression().
.withFilterExpression(“totalCost between :minCost and :maxCost”)
.withExpressionAttributeValues(valueMap);
//now call it PaginatedScanList orderList = mapper.scan(Order.class, scanExpression);
what is serialization?
serialization is the mechanism for converting the state of an object to another representation.
Serialization is used to persist an object.
(we focus on serializing objects to JSON)
also called marshalling
Once you convert an object to JSON…
…you can store it in a file, memory, or database
What is persistence?
Persistence is the ability to maintain the state of an object after a runtime session ends.
what is deserialization?
deserialization is the process of retrieving the state of an object from an earlier save.
also called unmarshalling
examples of serialization
serialize a word “undo” list, so when you reload a document you don’t lose your editing history.
a call to an API across the network might serialize the request in order to send it to the service
data from a Java application can be serialized and then read by a different type of application - e.g. Java serializes objects to a file, a python script can open that file and deserialize the data! (shared language)
most languages have libraries that serialize and deserialize to / from JSON. In Java we use the Jackson library.
Jackson library ObjectMapper class
writeValueAsString(Object value) - create JSON from Java objects (serialize) - serialize an Java value as a String
readValue(String content, Class valueType - accepts a String and a class and returns an instance of that class. parse JSON from a String and create a Java object representing the parsed JSON (deserialize);
serialize a character, using the class Character
Character character = new Character(); character.setCharacterName("junebug"); character.setHitPoints(41); List inventory = new ArrayList<>(); inventory.add("sword"); inventory.add("shield"); character.setInventory(inventory);
ObjectMapper objectMapper = new ObjectMapper(); String serializedCharacter = objectMapper.writeValueAsString(character);
–>
the serialized JSON string is produced:
{ “characterName”:”junebug”, “hitPoints”:41, “inventory”:[“sword”,”shield”]}
note: if hitPoints and inventory were not set, we would see “hitPoints”:0, “inventory”:null
- ->the unset fields are included, but set to their default values