How to write a list of maps in DDB via AWS/DDB Java SDK?

39 Views Asked by At

Say currently I have a DDB that looks like this:

EMP_DATA {
  empName: String,
  empId: Number
}

Now, I want to add a new List field to this table called iceCreamFlavorsTried. An employee can have tried multiple ice cream flavors. The schema of this field will look like this:

iceCreamFlavorsTried {
  [
  flavor: String,
  rating: Number,
  lastTimeFlavorHasBeenTried: Long, //Long represents Millis timestamp
  outdoorTemperatureWhenLastTried: Number
  ]
}

So, I kind of want the schema to look like:

EMP_DATA {
  empName: String,
  empId: Number,
  iceCreamFlavorsTried: List
}

Things my Java code will need to do via AWS/DDB SDK are:

  1. Each time an employee tries a new ice cream flavor, they should be able to add that flavor to the iceCreamFlavorsTried list associated to their employee record in the DDB table.

  2. Every time an employee tries a flavor they have already previously tried (hence this flavor already exists in the iceCreamFlavorsTried list for that employee's record in the DDB table), the code needs to update that item in the list (instead of creating a new item in the list).

For example, suppose at t=1692204729004, Mike (empId: 1234) has tried his very first flavor of ice cream, which is {flavor: "Double Chocolate", rating: 8.5, lastTimeFlavorHasBeenTried: 1692204729004, outdoorTemperatureWhenLastTried: 64}. This means his record in EMP_DATA should look like:

{empName: "Mike", empId: 1234, iceCreamFlavorsTried: [{flavor: "Double Chocolate", rating: 8.5, lastTimeFlavorHasBeenTried: 1692204729004, outdoorTemperatureWhenLastTried: 64}]} 

Now, at t=1692204739004, he tries a new flavor. He tried the Strawberry flavor. He rated it 9.5 and it was 80 degrees when he tried it. Now, his record in the EMP_DATA table should look like:

{empName: "Mike", empId: 1234, iceCreamFlavorsTried: [{flavor: "Double Chocolate", rating: 8.5, lastTimeFlavorHasBeenTried: 1692204729004, outdoorTemperatureWhenLastTried: 64}, {flavor: "Strawberry", rating: 9.5, lastTimeFlavorHasBeenTried: 1692204739004, outdoorTemperatureWhenLastTried: 80}]}

Finally, at t=1692204749004, he tries the Double Chocolate flavor again. He rated it a 9.1 and it was 97 degrees when he tried it. Now, his record in the EMP_DATA table should look like (notice how the iceCreamFlavorsTried list still only has two items in it, with one of them simply being updated - the Double Chocolate item was updated to reflect the new rating, time flavor was last tried, and the temperature when flavor was last tried):

{empName: "Mike", empId: 1234, iceCreamFlavorsTried: [{flavor: "Double Chocolate", rating: 9.1, lastTimeFlavorHasBeenTried: 1692204749004, outdoorTemperatureWhenLastTried: 97}, {flavor: "Strawberry", rating: 9.5, lastTimeFlavorHasBeenTried: 1692204739004, outdoorTemperatureWhenLastTried: 80}]}

I want to understand how to most simply do this write/update to a DDB table via the DDB/AWS Java SDK. Assume I already have access to a DDB client AmazonDynamoDB dynamoClient. I want to understand how to write this new IceCreamFlavorsTried: List field to my existing table. Which operations of this client should I use? How do I pass these values in as AttributeValue's so that I can write them to DDB? I already have two Java classes called IceCreamFlavorTried and IceCreamFlavorsTried, which look like this:

IceCreamFlavorTried {
  String flavor;
  Integer rating;
  Long lastTimeFlavorHasBeenTried; //Long represents Millis timestamp
  Integer outdoorTemperatureWhenLastTried;
}

IceCreamFlavorsTried {
  List<IceCreamFlavorTried> iceCreamFlavorsTried;
}

I am assuming that IceCreamFlavorTried (for a single flavor) will end up being a Map in DDB. So I am guessing, as far as DDB is concerned, we would be writing a list of maps to DDB?

Any help is appreciated. And if you have any clarifying questions, please do ask!

0

There are 0 best solutions below