Saurabh Jhunjhunwala
Saurabh Jhunjhunwala

Reputation: 2922

Fetch records from DynamoDb db table

I have a Dynamodb records example

{
  "cDate":"2024-03-04",
  "myList":[
     {
       "M" : {
          "rt":{"N":"1.06"},
          "sDt":{"S":"2024-03-01"},
          "c":{"S":"ABC"},
          "t":{"S":"BCD"}
        }
     },
     {
       "M" : {
          "rt":{"N":"0.96"},
          "sDt":{"S":"2024-03-01"},
          "c":{"S":"CDE"},
          "t":{"S":"DEF"}
        }
     }
   ],
   "type":"inter"
} 

while some records have structure 

{
  "cDate":"2024-03-04",
  "myList":[
     {
        "rt":"1.06",
        "sDt":"2024-03-01",
        "c":"ABC",
        "t":"BCD"
     },
     {
       "rt":"0.96",
       "sDt":"2024-03-01",
       "c":"CDE",
       "t":"DEF"
     }
   ],
   "type":"inter"
} 

As the records structure is not similar I want to standardise it. I want to read all the records from this table, I am trying as

class MyTable {
  String cDate;
  String type;
  List<MyList> myList;
}

class MyList {
  String rt;
  String sDt;
  String c;
  String t;
}

QueryConditional queryConditional = QueryConditional.sortGreaterThanOrEqualTo(qc-> qc.partionValue("inter").sortValue("2024-03-04");

final DynamoDbIndex<MyTable> indexResult = table.index("indexName");

final SdkIterable<Page<MyTable>> pageResult = indexResult.query (q-> q.queryConditional(queryConditional)
.attributesToProject("cDate", "type")
.addNestedAttributesToProject( NestedAttributeName.create("myList","M")
)));

List<MyTable> collectedItems = new ArrayList<>();

pageResult.stream().forEach(page-> page.items().stream().forEach(mt-> {
    log.info(mt.toString());
    collectedItems.add(mt);
}));

using the above code I am able to print

MyTable[cDate=2024-03-04, type=inter, myList=null]

Please help me fetch the values for myList

Upvotes: 0

Views: 75

Answers (1)

Peng
Peng

Reputation: 1314

{
  "cDate":"2024-03-04",
  "myList":[
     {
       "M" : {
          "rt":"1.06",
          "sDt":"2024-03-01",
          "c":"ABC",
          "t":"BCD"
        }
     },
     {
       "M" : {
          "rt":"0.96",
          "sDt":"2024-03-01",
          "c":"CDE",
          "t":"DEF"
        }
     }
   ],
   "type":"inter"
} 

Read the json struct you provide , conclude you may missed there still is a attribute M below myList class , so orm framework might could not found the mapping column, you should make it up , like following code snippet

public class MyTable {
    private String cDate;
    private String type;
    private List<MyListItem> myList;

    // Getters and setters
}

public class MyListItem {
    private MyItem M;

    // Getters and setters for M
}

public class MyItem {
    private String rt;
    private String sDt;
    private String c;
    private String t;

    // Getters and setters
}

and then do you orm query , the question encounter may be processed


Dynamic structure

if you want receive the Dynamic structure try to use additional class for trans;

public class MyAttributeValueItem {
    private Map<String, AttributeValue> M;

    // Getters and setters for M
}
//  above is your code snippet 
List<MyTable> collectedItems = new ArrayList<>();

pageResult.stream().forEach(page -> page.items().stream().forEach(mt -> {
    log.info(mt.toString());
    List<MyList> myList = mt.getMyList().stream()
        .map(myListItem -> {
            if (myListItem.getM() != null) {
                MyItem myItem = myListItem.getM();
                return new MyList(myItem.getRt(), myItem.getsDt(), myItem.getC(), myItem.getT());
            } else {
                MyAttributeValueItem myAttributeValueItem = myListItem.getMAttribute();
                Map<String, AttributeValue> attributes = myAttributeValueItem.getM();
                return new MyList(
                    attributes.get("rt").n(),
                    attributes.get("sDt").s(),
                    attributes.get("c").s(),
                    attributes.get("t").s()
                );
            }
        }).collect(Collectors.toList());
    mt.setMyList(myList);
    collectedItems.add(mt);

above is basic operate code snippet ,its unified the output structure in orm query layer , you can modify it in your own structure.

or that you can receive both structure with M or without M , abstruct a new method to standardise it .

all in all , there must be a abstract layer for standardise query .

Upvotes: 0

Related Questions