I'm trying to write a Query in DynamoDB with logic similar to a GROUP BY in SQL. Let's consider the following table with partition key simulation_id
:
Simulation (Table):
simulation_id_1 - type_1 - ...
simulation_id_1 - type_1 - ...
simulation_id_1 - type_2 - ...
simulation_id_1 - type_2 - ...
simulation_id_1 - type_2 - ...
simulation_id_2 - type_2 - ...
simulation_id_2 - type_2 - ...
simulation_id_2 - type_2 - ...
simulation_id_2 - type_2 - ...
simulation_id_2 - type_3 - ...
The result would be:
simulation_id_1 - type_1 - 2 (count of entries)
simulation_id_1 - type_2 - 3
simulation_id_2 - type_2 - 4
simulation_id_2 - type_3 - 1
What is the best way for achieving that using DynamoDbEnhancedClient?
GROUP BY or SUM or any other type of math metric is not supported by DynamoDB and is considered somewhat of an anti-pattern.
With that said, there is 2 ways to achieve your use-case needs.
Query
operation and set theSelect
parameter toCOUNT
DOCS. This will return the count of all the items which match a given simulationId, however, this will require theQuery
to read all of the items which relate to that simulationId, only returning the count. It will also require you to use a lower level client I believe.