# Cosmos DB NoSQL

The Cosmos DB NoSQL connector is supported both as a source and a destination. It runs as a separate gRPC service that dsync/dsynct connects to, using the Java SDK optimized for bulk operations.

## Running the Connector

Set the Cosmos DB account URL and key. These can be found under "Settings" -> "Keys" in the Azure Portal.

```bash
export COSMOS_URI="..."
export COSMOS_KEY="..."
```

```bash
docker run \
  --network mynet --name cosmosnosqlconnector \
  markadiom/cosmosnosqlconnector 8089 $COSMOS_URI $COSMOS_KEY
```

## gRPC Configuration

The connector exposes a gRPC endpoint. When specifying it as a source or destination, use the `grpc://` scheme with the `--insecure` flag (TLS is not enabled by default):

```
grpc://cosmosnosqlconnector:8089 --insecure
```

When running outside of Docker networking, replace the container name with the appropriate host (e.g. `grpc://localhost:8089`).

## Data Type

The Cosmos DB NoSQL connector uses **JSON**.

## ID Format

The document ID is composed of the shard key followed by the `id` field. The `id` field is always the last part of the ID. If `id` is itself part of the shard key, it is not duplicated.

For example, with a shard key of `/region`:

| `id`    | `region`    | Document ID          |
| ------- | ----------- | -------------------- |
| `"123"` | `"us-east"` | `["us-east", "123"]` |

With a shard key of `/id` (where `id` is the shard key):

| `id`    | Document ID |
| ------- | ----------- |
| `"123"` | `["123"]`   |

With a hierarchical shard key of `/region/tenant`:

| `id`    | `region`    | `tenant` | Document ID                  |
| ------- | ----------- | -------- | ---------------------------- |
| `"123"` | `"us-east"` | `"acme"` | `["us-east", "acme", "123"]` |
