Home > database >  DynamdoDB batchget from single table with multiple object models
DynamdoDB batchget from single table with multiple object models

Time:12-26

I'm learning DynamoDB and i've a Single table design stored with various object models like Employee, Company, etc.. If i know pk & sk of a company and employee, will i be able to query two items (Item # 1 & 4) in a single call.

Table name : APP_TABLE

# pk sk
1 EMP_DTL E1
2 EMP_DTL E2
3 EMP_DTL E3
4 CMPNY_DTL C1
5 CMPNY_DTL C2
6 CMPNY_DTL C3

I tried to use BatchGetItem but looks like i can't map the same table with multiple java models as shown below.

DynamoDbEnhancedClient.table("APP_TABLE", TableSchema.fromBean(Company.class)); DynamoDbEnhancedClient.table("APP_TABLE", TableSchema.fromBean(Employee.class));

If i use PartiQL, i'm not sure if this is valid or it would do table scan.

SELECT * FROM APP_TABLE WHERE pk IN ('EMP_DTL','CMPNY_DTL') AND sk IN ('E1','C1')";

What would be the best approach to achieve this?

CodePudding user response:

DynamoDB Enhanced Client does not provide the functionality of a single table design, as the class is mapped directly to the table. You can either break your model into multiple tables or use a low level client that does not map your table to an object.

Block full table Scan

{
   "Version":"2012-10-17",
   "Statement":[
      {
         "Effect":"Deny",
         "Action":[
            "dynamodb:PartiQLSelect"
         ],
         "Resource":[
            "arn:aws:dynamodb:us-west-2:123456789012:table/WatchList"
         ],
         "Condition":{
            "Bool":{
               "dynamodb:FullTableScan":[
                  "true"
               ]
            }
         }
      },
      {
         "Effect":"Allow",
         "Action":[
            "dynamodb:PartiQLSelect"
         ],
         "Resource":[
            "arn:aws:dynamodb:us-west-2:123456789012:table/WatchList"
         ]
      }
   ]
}
  • Related