Search code examples
azureazure-cosmosdbdatabase-partitioningdocument-databaseazure-cosmosdb-tables

CosmosDb Partition Key For Nested Java Object/Json (Spring Boot)


I'm trying to partition a collection based on a nested Java object and the corresponding Json. I know it might seem unreasonable to do this, but I am working on a project far into development, and the layout expected by our client team is not flexible at the moment. Ex.) The equivalent Json for an object called receiptItem looks like this:

{
   "id": "1",
   "item": { 
      "itemId": "1",
      "name": "itemName",
      "value": 4.98,
      ...
   }, 
   "tax": {
      "rate": 0.15,
      "state": "FL",
      ...
   },
   ...
}

Is it possible to create a partition key in Azure with a syntax along the lines of "/item.itemId"? Any other way to go about this while utilizing itemId as the partition key for the receiptItem collection?

If there is an answer for how to do this in Azure, I would also be interested in figuring out what the corresponding Java code would look like. Unlike C#, the @PartitionKey annotation does not seem to have an attribute to specify a nested object, (as in [ParitionKey(item.id)]), and I do not see a way to do this in my configurations (in Spring boot) either.

In Java, would look like:

@Data
@NoArgsConstructor
@FullArgsContructor
@ToString
@Document(collection="receiptItems")
public class ReceiptItem {
   @Id
   int id;
   // @PartitionKey???
   Item item;
   Tax tax;
   ...
}

Solution

  • For now it is certainly possible to have a 'nested' partition key in Azure. For example, /item/id would be used to make the id field in item the partition key. The corresponding Json doc would look like this:

    {
       "id": "1",
       "item": { 
          "itemId": "1",
          "name": "itemName",
          "value": 4.98,
          ...
       }, 
       "tax": {
          "rate": 0.15,
          "state": "FL",
          ...
       },
       ...
    }
    

    Unfortunately, nested partition keys are not currently supported in Spring. Thanks very much to ChrisAnderson-MSFT for getting involved in looking for a solution, and for opening up the issue on github, which can be found here: https://github.com/Microsoft/spring-data-cosmosdb/issues/350.