user1932923
user1932923

Reputation: 384

Azure Change Feed and Querying based on Partition

When we fetch data from Document Db in the change feed, we only want it per partition and have tried adding PatitionKey to the code.

do
        {
            FeedResponse<PartitionKeyRange> pkRangesResponse = await client.ReadPartitionKeyRangeFeedAsync(
                collectionUri,
                new FeedOptions
                {
                    RequestContinuation = pkRangesResponseContinuation,
                    PartitionKey = new PartitionKey("KEY"),
                });

            partitionKeyRanges.AddRange(pkRangesResponse);
            pkRangesResponseContinuation = pkRangesResponse.ResponseContinuation;
        }
        while (pkRangesResponseContinuation != null);

It returns single range and when we go perform the second query

IDocumentQuery<Document> query = client.CreateDocumentChangeFeedQuery(
                collectionUri,
                new ChangeFeedOptions
                {
                    PartitionKeyRangeId = pkRange.Id,
                    StartFromBeginning = true,
                    RequestContinuation = continuation,
                    MaxItemCount = -1,
                });

It returns all the results from all partitions. Is there a way to restrict the results from single partition only?

Upvotes: 2

Views: 1418

Answers (1)

KranthiKiran
KranthiKiran

Reputation: 145

Changefeed works at a PartitionKey Range level.

What are partition key ranges?

Document Db currently has 10 GB Physical partitions. The partition key that you specify is the Logical Partition Key. Document Db internally maps this logical partition key to a Physical Partition using a hash. So its possible that a bunch of logical partitions are sharing the same physical partition. So a physical partition is assigned for a range of these hashes.

The minimum grain that is allowed to read from changefeed would be Partition key ranges. So for the you would have to query the partition key range id for the partition that you are interested in. Then query the Changefeed for that range id and filter out the data that is not associated to the partition id.

Note: Document db transparently creates new physical partitions if a particular partition gets full. So the partition key range id for a given logical partition could change over time.

This link explains this in good detail: https://learn.microsoft.com/en-us/azure/cosmos-db/partition-data#partitioning-in-azure-cosmos-db

Upvotes: 1

Related Questions