Jupyter Notebooks are in everywhere in these days. You can write chunk of code and run it on a web application without worrying about compiler is a great feeling. C# has been little bit late to the party, but we started to see C# Notebooks lately too. Azure Cosmos DB announced their version if C# Notebook this week.
You can reach all notebook functionalities under the Data Explorer link, There are bunch of sample notebooks you will see under the Notebook link.
Latest Azure Cosmos DB .NET SDK is installed and ready to use it. You can easily create a CosmosClient and start programing without downloading any packages. Click on New Notebook, Change the language to C# and click SAVE before you try to write any code.
In the following example, I created CosmosClient by passing global properties. Cosmos.Endpoint and Cosmos.Key variables contains the current database information and they work only for SQL API. If you have a different API, you need to get the connection string values under the Keys tab and use them here.
In the following example, I read a specific document by passing the id and partition key. You can type the variable name and Cosmos DB will try to visualize the data depends on its type. Since this one is only a Read request, it tells me all the information about the request and data. We have all kind of information about the data and query processor logs.
I wonder what will happen if we try to display bunch of documents. Let's try. I will run the following code to return a collection.
var container = client.GetDatabase("Stackoverflow").GetContainer("Posts");
var query = "SELECT * FROM Posts p where p.OwnerUserId = 1 ";
var queryDefinition = new QueryDefinition(query);
List<Post> coll = new List<Post>();
var queryResultSetIterator = container.GetItemQueryIterator<Post>(queryDefinition);
while (queryResultSetIterator.HasMoreResults)
{
var currentResultSet = await queryResultSetIterator.ReadNextAsync();
foreach (Post current in currentResultSet)
{
coll.Add(current);
}
}
coll
We end up with a fantastic Grid, you can see all the data in this grid. You can filter your data easily. Also, you have all kind of ways to display your data by clicking one of the icons in the right side.
You can use built in commands too, I have covered them in my earlier post. If you will need a custom package from NuGet, you can install it by using #r command. It should be in the following format. You should run this in a different cell before your code.
No comments:
Post a Comment