10

Cosmos DB – Retrieve all attributes/fields from the JSON document of all Collect...

 2 years ago
source link: https://sqlwithmanoj.com/2021/04/06/cosmos-db-retrieve-all-attributes-fields-from-the-json-document-of-all-collections-using-net/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Home > Cosmos DB > Cosmos DB – Retrieve all attributes/fields from the JSON document of all Collections (using .net)

Cosmos DB – Retrieve all attributes/fields from the JSON document of all Collections (using .net)

In our [previous post] we saw how to get COUNT of items/records from all Collections in all Databases of a CosmosDB instance by programmatically/dynamically iterating over all these objects.

Here in this post we will extend it and see how to retrieve all attributes/fields from the JSON document/items present in these Collections. (This is some work I’m doing for maintaining a data catalogue and data classification for privacy and compliance purpose.)

On Azure portal go to your Azure Cosmos DB instance, from Overview tab go to Keys tab and copy the “URI” & “PRIMARY READ-ONLY KEY” key values in code below.

We have discussed all the methods used below in our [previous post], please check here. As the Cosmos DB SQL container is a schema-agnostic and the items in a container can have arbitrary schemas, thus we will read the first JSON document/item from the Collection by using FirstOrDefault() method and Deserialize this JSON document Object to a Dictionary, and finally read all the attributes from this Dictionary Keys:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace CosmosDBgetJSONAttributes
{
class Program
{
static void Main(string[] args)
{
string PK = "YourPrimaryKey==";
DocumentClient cosmosClient = new DocumentClient(new Uri(EndPointUri), PK);
string sqlQuery = "SELECT * FROM d";
var dbs = cosmosClient.CreateDatabaseQuery(sqlQuery).ToList();
foreach (var db in dbs)
{
Console.WriteLine("Database: " + db.id);
List<DocumentCollection> collections = cosmosClient.CreateDocumentCollectionQuery((String)db._self).ToList();
foreach (var collection in collections)
{
Console.WriteLine("  - Collection: " + collection.Id);
var docu = cosmosClient.CreateDocumentQuery(collection.SelfLink, $"SELECT TOP 1 * FROM c",
new FeedOptions() { EnableCrossPartitionQuery = true, MaxItemCount = 1, })
.AsEnumerable().FirstOrDefault();
if (docu != null)
{
var docuStr = Convert.ToString(docu);
var data = JsonConvert.DeserializeObject<Dictionary<string, string>>(docuStr);
foreach (var item in data.Keys)
{
Console.WriteLine("    - Attribute: " + item);
}
}
else
{
Console.WriteLine("    - Attribute: no data");
}
}
Console.WriteLine(Environment.NewLine);
}
Console.WriteLine("Finished reading all Collections attributes");
Console.Read();
}
}
}

Related


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK