Created
December 29, 2019 19:01
-
-
Save crowcoder/29ef3bad26607a2a9aac16d12c9628f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.Azure.Documents; | |
using Microsoft.Azure.Documents.Client; | |
using Microsoft.Azure.Documents.Linq; | |
using Newtonsoft.Json.Linq; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace CosmosPartitionIteration | |
{ | |
class Program | |
{ | |
static string key = "your_master_cosmos_key=="; | |
static string db_acct = "your_cosmos_account_name"; | |
static string db_id = "your_database_name"; | |
static string container_id = "your_container_name"; | |
static string cosmos_url = $"https://{db_acct}.documents.azure.com:443/"; | |
static async Task Main(string[] args) | |
{ | |
long totalDocumentCount = 0L; | |
List<string> partitionKeyRanges = await GetPartitionKeyRangesWithDocumentClient(); | |
Console.WriteLine($"number of partitions: {partitionKeyRanges}"); | |
var cosmosUri = UriFactory.CreateDocumentCollectionUri(db_id, container_id); | |
string sql = "SELECT VALUE COUNT(1) FROM c"; | |
foreach (var range in partitionKeyRanges) | |
{ | |
//restrict query to a partition | |
FeedOptions feedOptions = new FeedOptions { PartitionKeyRangeId = range, EnableCrossPartitionQuery = false }; | |
using (var documentClient = new DocumentClient(new Uri(cosmos_url), key)) //typically a static shared instance | |
using (var qry = documentClient.CreateDocumentQuery(cosmosUri, sql, feedOptions).AsDocumentQuery()) | |
{ | |
while (qry.HasMoreResults) | |
{ | |
var batch = await qry.ExecuteNextAsync(); | |
Console.WriteLine($"Request Charge for range {range}: {batch.RequestCharge}"); | |
var count = batch.ToList().First(); | |
totalDocumentCount += Convert.ToInt64(count); | |
} | |
} | |
} | |
Console.WriteLine($"Total documents = {totalDocumentCount}"); | |
Console.ReadKey(); | |
} | |
/// <summary> | |
/// Creates a list of partition ids in a cosmos database | |
/// </summary> | |
/// <returns></returns> | |
static async Task<List<string>> GetPartitionKeyRangesWithDocumentClient() | |
{ | |
Uri partitionKeyRangesUri = UriFactory.CreatePartitionKeyRangesUri(db_id, container_id); | |
FeedResponse<PartitionKeyRange> response = null; | |
List<string> ids = new List<string>(); | |
using (DocumentClient documentClient = new DocumentClient(new Uri(cosmos_url), key)) | |
{ | |
do | |
{ | |
response = await documentClient.ReadPartitionKeyRangeFeedAsync(partitionKeyRangesUri, new FeedOptions { MaxItemCount = 1000 }); | |
foreach (var item in response) | |
{ | |
ids.Add(item.Id); | |
} | |
} | |
while (!string.IsNullOrEmpty(response.ResponseContinuation)); | |
} | |
return ids; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment