DynamoDB Scan & JSON Serialization Flashcards

1
Q

DynamoDB scan() method…

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Serialization

A

allows you to store, share, and persist data across networks and runtime sessions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

limitation on query() method

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

limitation on the scan() method

A
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

example of scan() retrieving all data

A
  1. declare mapper - DynamoDBMapper
  2. declare scanExpression - DynamoDBScanExpression
  3. mapper.scan(.class, scanExpression)
    ^^PaginatedScanList
  4. DynamoDBMapper mapper = new DynamoDBMapper(DynamoDbClientProvider.getDnamoDBClient);
  5. DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
  6. PaginatedScanList orderList = mapper.scan(Order.class, scanExpression);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

example of scanExpression with filter

A
//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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

what is serialization?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Once you convert an object to JSON…

A

…you can store it in a file, memory, or database

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is persistence?

A

Persistence is the ability to maintain the state of an object after a runtime session ends.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what is deserialization?

A

deserialization is the process of retrieving the state of an object from an earlier save.

also called unmarshalling

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

examples of serialization

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Jackson library ObjectMapper class

A

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);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

serialize a character, using the class Character

A
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly