Azure Cosmos DB

from the Azure documentation on how to create an Azure CosmosDB account. The database and collection must be created in CosmosDB before Dapr can use it.

Note : The partition key for the collection must be named “/partitionKey”. Note: this is case-sensitive.

In order to setup CosmosDB as a state store, you need the following properties:

The next step is to create a Dapr component for CosmosDB.

Create the following YAML file named :

Warning

Here is an example of what the values could look like:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: statestore
  5. spec:
  6. type: state.azure.cosmosdb
  7. version: v1
  8. metadata:
  9. - name: url
  10. value: https://accountname.documents.azure.com:443
  11. value: thekey==
  12. value: db1
  13. - name: collection
  14. value: c1

The following example uses the Kubernetes secret store to retrieve the secrets:

If you wish to use CosmosDb as an actor store, append the following to the yaml.

  1. - name: actorStateStore
  2. value: "true"

To apply the CosmosDB state store to Kubernetes, use the kubectl CLI:

To run locally, create a YAML file described above and provide the path to the dapr run command with the flag --components-path. See this or run for more information on the path.

If you are using the Dapr SDKs (e.g. ) the SDK will serialize your data to json.

For examples see the curl operations in the Partition keys section.

For non-actor state operations, the Azure Cosmos DB state store will use the key property provided in the requests to the Dapr API to determine the Cosmos DB partition key. This can be overridden by specifying a metadata field in the request with a key of partitionKey and a value of the desired partition.

The following operation will use nihilus as the partition key value sent to CosmosDB:

  1. curl -X POST http://localhost:3500/v1.0/state/<store_name> \
  2. -H "Content-Type: application/json"
  3. -d '[
  4. {
  5. "key": "nihilus",
  6. "value": "darth"
  7. }

For non-actor state operations, if you want to control the CosmosDB partition, you can specify it in metadata. Reusing the example above, here’s how to put it under the mypartition partition