Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Supported Client Libraries

Supported Operations

OperationMethods Supported
Create
  • Microsoft.Azure.Documents.Client.DocumentClient.CreateDocumentCollectionAsync
  • Microsoft.Azure.Documents.IDocumentClient.CreateDocumentCollectionAsync
  • Microsoft.Azure.Documents.Client.DocumentClient.CreateDocumentCollectionIfNotExistsAsync
  • Microsoft.Azure.Documents.IDocumentClient.CreateDocumentCollectionIfNotExistsAsync
Insert
  • Microsoft.Azure.Documents.Client.DocumentClient.CreateDocumentAsync


  • Microsoft.Azure.Documents.IDocumentClient.CreateDocumentAsync
  • Microsoft.Azure.Documents.Client.DocumentClient.UpsertDocumentAsync
  • Microsoft.Azure.Documents.IDocumentClient.UpsertDocumentAsync
Update
  • Microsoft.Azure.Documents.Client.DocumentClient.ReplaceDocumentAsync
  • Microsoft.Azure.Documents.IDocumentClient.ReplaceDocumentAsync
  • Microsoft.Azure.Documents.Client.DocumentClient.ReplaceDocumentCollectionAsync
  • Microsoft.Azure.Documents.IDocumentClient.ReplaceDocumentCollectionAsync
  • Microsoft.Azure.Documents.Client.DocumentClient.UpsertDocumentAsync
  • Microsoft.Azure.Documents.IDocumentClient.UpsertDocumentAsync
Select
  • Microsoft.Azure.Documents.Client.DocumentClient.ReadDocumentAsync
  • Microsoft.Azure.Documents.IDocumentClient.ReadDocumentAsync
  • Microsoft.Azure.Documents.Client.DocumentClient.ReadDocumentCollectionAsync
  • Microsoft.Azure.Documents.IDocumentClient.ReadDocumentCollectionAsync
  • Microsoft.Azure.Documents.Client.DocumentClient.CreateDocumentQuery
  • Microsoft.Azure.Documents.Client.DocumentClient.CreateDocumentQuery
Delete
  • Microsoft.Azure.Documents.Client.DocumentClient.DeleteDocumentAsync
  • Microsoft.Azure.Documents.IDocumentClient.DeleteDocumentAsync'
  • Microsoft.Azure.Documents.Client.DocumentClient.DeleteDocumentCollectionAsync
  • Microsoft.Azure.Documents.IDocumentClient.DeleteDocumentCollectionAsync

Objects

IconDescription

DotNet CosmosDB Database

DotNet CosmosDB Collection

 

DotNet CosmosDB Unknown Database 

 

DotNet CosmosDB Unknown Collection 

...

Link typeWhen is this created? Methods Supported
parentLink

Between Azure CosmosDB database object and Azure CosmosDB collection object


useLinkBetween the caller .NET Method objects and Azure CosmosDB collection object


  • CreateDocumentCollectionAsync
  • CreateDocumentCollectionIfNotExistsAsync

useInsertLink

  • CreateDocumentAsync
  •  CreateDocumentAsyncUpsertDocumentAsync
useUpdateLink
  • ReplaceDocumentAsync
  • ReplaceDocumentCollectionAsync
  • UpsertDocumentAsync
useSelectLink
  • ReadDocumentAsync
  • ReadDocumentCollectionAsync
  • CreateDocumentQuery
useDeletetLink
  • DeleteDocumentAsync
  • DeleteDocumentCollectionAsync

...

Azure Cosmos DB Database and Table Creation

Expand
titleDB and Table Creation


Code Block
languagec#
public class CosmosDBApplication { private const string EndpointUrl = .......; private const string PrimaryKey = .........; private DocumentClient client ; private string databaseName = "FamilyDatabase"; private string collName = "FamilyCollection"; public static void Main() { this.client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey); } private async Task CreateDatabase() {
titleCreateDatabase and CreateCollection
private static readonly string databaseName = "samples";
private static readonly string collectionName = "document-samples";

private static async Task Initialize()
        {
            await client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseName });

            // We create a partitioned collection here which needs a partition key. Partitioned collections
            // can be created with very high values of provisioned throughput (up to OfferThroughput = 250,000)
            // and used to store up to 250 GB of data. You can also skip specifying a partition key to create
            // 
Database
single 
db1
partition 
=
collections 
await this.client.CreateDatabaseIfNotExistsAsync(new Database { Id = databaseName }).Result;
that store up to 10 GB of data.
            
Database
DocumentCollection 
db2
collectionDefinition =
this.client.CreateDatabaseIfNotExistsAsync(new Database {
 new DocumentCollection();
            collectionDefinition.Id = 
"FamilyDatabase2" }).Result
collectionName;

       
}
     // For this demo, 
private
we 
async
create 
Task
a 
CreateCollection()
collection to store SalesOrders. We set the partition key 
{
to the account
            //
Creating
 
DocumentCollection
number 
using
so 
UriFactory
that we can retrieve all sales orders for an account efficiently from a 
await this.client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri("FamilyDatabase"), new DocumentCollection {Id =
single partition,
            // and perform transactions across multiple sales order for a single account number. 
            
"FamilyCollection"}
collectionDefinition.PartitionKey.Paths.Add("/AccountNumber");

            //
Creating Document Collection using string Link
 Use the recommended indexing policy which supports range queries/sorting on strings
           
await
 
this.client.CreateDocumentCollectionIfNotExistsAsync("dbs/FamilyDatabase", new DocumentCollection {Id = "FamilyCollection2"});
collectionDefinition.IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.String) { Precision = -1 });

            // Create with a throughput of 1000 RU/
Creating DatabaseUri
s
            await client.CreateDocumentCollectionIfNotExistsAsync(
             
var
 
databaseUri
 
=
 UriFactory.CreateDatabaseUri(
"FamilyDatabase2");
databaseName),
                
this.client.CreateDocumentCollectionIfNotExistsAsync(databaseUri, new DocumentCollection {Id = "FamilyCollectionNewWay"
collectionDefinition,
                new RequestOptions { OfferThroughput = 1000 });
        }
}

...



Image Added

Insert Operation

PutItem
Expand
titleInsert Operation


Code Block
languagec#
title
CreateDocumentAsync
private static async
InsertDocument(Family family){
 Task CreateDocumentsAsync()
        {
            Uri collectionUri = UriFactory.CreateDocumentCollectionUri(databaseName, collectionName);
            Console.WriteLine("\n1.1 - Creating documents");
            SalesOrder salesOrder = GetSalesOrderSample("SalesOrder1");
            await 
this.
client.CreateDocumentAsync(
UriFactory.CreateDocumentCollectionUri(databaseName, collName), family); }

...

collectionUri, salesOrder);
			SalesOrder2 newSalesOrder = GetSalesOrderV2Sample("SalesOrder2");
            await client.CreateDocumentAsync(collectionUri, newSalesOrder);
		}

Image Added

Select Operation

Expand
titleSelect Operation


Code Block
languagec#
titleReadDocumentAsync
private static async Task
ReadDocument(Family family){
 ReadDocumentAsync()
        {
            Console.WriteLine("\n1.2 - Reading Document by Id");

            var response = await 
this.
client.ReadDocumentAsync(
            	UriFactory.CreateDocumentUri(databaseName, collectionName, 
collName, family.Id)); }

...

"SalesOrder1"), 
                new RequestOptions { PartitionKey = new PartitionKey("Account1") });
		}

Image Added

Code Block
languagec#
titleReadDocumentCollectionAsync
private static async Task 
ReadCollection
ReadDocumentCollectionAsync()
        {
            Console.WriteLine("\n1.7 - Reading 
// Using UriFactory object
a document");
            ResourceResponse<Document> response = await 
this.
client.ReadDocumentCollectionAsync(
                UriFactory.
CreateDocumentCollectionUri
CreateDocumentUri(databaseName, collectionName, "SalesOrder3"),
collName)

                new RequestOptions { PartitionKey = new PartitionKey("Account1") });
        }

Image Added

Code Block
languagec#
titleCreateDocumentAsync
private static SalesOrder QueryDocuments()
   
//Using stringLink
     {
           
await
 
this
Console.
client.ReadDocumentCollectionAsync("dbs/FamilyDatabase/colls/FamilyCollection2");
WriteLine("\n1.4 - Querying for a document using its AccountNumber property");

            SalesOrder querySalesOrder 
//Creating DocumentCollectionUri
= client.CreateDocumentQuery<SalesOrder>(
             
var
 
collLink
 
=
 UriFactory.CreateDocumentCollectionUri(
"FamilyDatabase2", "FamilyCollectionNewWay")
databaseName, collectionName))
                .Where(so => so.AccountNumber == "Account1")
                .AsEnumerable()
                .First();

           
await
 
this
Console.
client.ReadDocumentCollectionAsync(collLink); }

...

WriteLine(querySalesOrder.AccountNumber);
            return querySalesOrder;
        }

Image Added

Update Operation

...

CreateDocumentQuery
Expand
titleUpdate Operation


Code Block
languagec#
title
ReplaceDocumentAsync
	private static async Task 
QueryData
ReplaceDocumentAsync(SalesOrder order)
        {
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1 }; IQueryable<Family> familyQuery = this.client.CreateDocumentQuery<Family>( UriFactory.CreateDocumentCollectionUri(databaseName, collName), queryOptions)
            Console.WriteLine("\n1.5 - Replacing a document using its Id");
            order.ShippedDate = DateTime.UtcNow;
            ResourceResponse<Document> response = await client.ReplaceDocumentAsync(
                UriFactory.CreateDocumentUri(databaseName, collectionName, order.Id), 
                order);

            var updated = response.Resource;
            Console.
Where(f => f.LastName == "Andersen"); }

Image Removed

ReplaceDocumentAsync
WriteLine("Request charge of replace operation: {0}", response.RequestCharge);
            Console.WriteLine("Shipped date of updated document: {0}", updated.GetPropertyValue<DateTime>("ShippedDate"));
        }

Image Added

Code Block
languagec#
title
ReplaceCollectionDocumentAsync
private static async Task
ReplaceDocument(Family updatedFamily) { await this.client.ReplaceDocumentAsync("dbs/FamilyDatabase/colls/FamilyCollection/docs/familyName", updatedFamily); }

...

 ReplaceDocumentCollectionAsync()
        {
            Console.WriteLine("\n1.7 - Updating a document");
            ResourceResponse<Document> response = await client.ReplaceDocumentCollectionAsync(
                UriFactory.CreateDocumentUri(databaseName, collectionName, "SalesOrder3"),
                new RequestOptions { PartitionKey = new PartitionKey("Account1") });

            Console.WriteLine("Request charge of update operation: {0}", response.RequestCharge);
            Console.WriteLine("StatusCode of operation: {0}", response.StatusCode);
        }

Image Added

Upsert Operation

ReplaceDocumentCollectionAsync
Expand
titleUpsert Operation


Code Block
languagec#
title
UpsertDocumentAsync
private static async Task
ReplaceCollection(DocumentCollection updatedColl1, DocumentCollection updatedColl2) {
 UpsertDocumentAsync()
        {
            Console.WriteLine("\n1.6 - Upserting a document");

            var upsertOrder = GetSalesOrderSample("SalesOrder3");
            ResourceResponse<Document> response = await 
this.
client.
ReplaceDocumentCollectionAsync
UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName
, collName), updatedColl1
, collectionName), upsertOrder);
            var upserted = response.Resource;

            Console.WriteLine("Request charge of upsert operation: {0}", response.RequestCharge);
            Console.WriteLine("StatusCode of this operation: {0}", response.StatusCode);
            Console.WriteLine("Id of upserted document: {0}", upserted.Id);
            Console.WriteLine("AccountNumber of upserted document: {0}", upserted.GetPropertyValue<string>("AccountNumber"));

         
// Creating DocumentCollectionUri
   upserted.SetPropertyValue("AccountNumber", "updated account number");
           
var
 
collectionLink
response = await client.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(
"FamilyDatabase2", "FamilyCollectionNewWay");
databaseName, collectionName), upserted);
            upserted = response.Resource;

            Console.WriteLine("Request charge of upsert operation: {0}", response.RequestCharge);
            Console.WriteLine("StatusCode of this operation: {0}", response.StatusCode);
            Console.WriteLine("Id of upserted 
await this.client.ReplaceDocumentCollectionAsync(collectionLink, updatedColl2); }

...

document: {0}", upserted.Id);
            Console.WriteLine("AccountNumber of upserted document: {0}", upserted.GetPropertyValue<string>("AccountNumber"));
        }

Image Added


Delete Operation

Expand
titleDelete Operation


Code Block
languagec#
titleDeleteDocumentAsync
 private static async Task
DeleteDocument(string documentName) {
 DeleteDocumentAsync()
        {
            Console.WriteLine("\n1.7 - Deleting a document");
            ResourceResponse<Document> response = await 
this.
client.DeleteDocumentAsync(
                UriFactory.CreateDocumentUri(databaseName, 
collName, documentName)
collectionName, "SalesOrder3"),
                new RequestOptions { PartitionKey = new PartitionKey("Account1") });

            Console.WriteLine("Request charge of delete operation: {0}", response.RequestCharge);
            Console.WriteLine("
Deleted Family
StatusCode of operation: {0}", 
documentName
response.StatusCode);
        }

...

Image Added

Code Block
languagec#
titleDeleteDocumentCollectionAsync
private static async Task 
DeleteCollection
DeleteDocumentCollectionAsync()
        {
            Console.WriteLine("\n1.7 - Deleting a document");
            ResourceResponse<Document> response = await 
this.
client.DeleteDocumentCollectionAsync(
                UriFactory.
CreateDocumentCollectionUri
CreateDocumentUri(databaseName,
collName));
 collectionName, "SalesOrder3"),
                new RequestOptions { PartitionKey = new PartitionKey("Account1") });

            Console.WriteLine("Request charge of 
await this.client.DeleteDocumentCollectionAsync("dbs/FamilyDatabase/colls/FamilyCollection2"); }

Image Removed

Evolution

  • Previously, only  files consisting of relevant Azure CosmosDB imports were processed further for analysing.
  • Support has been evolved to process a  file when it does not contain Azure CosmosDB import but makes use of its methods (APIs). Assumption here is file makes use of same namespace as used in the file containing Azure CosmosDB imports.

Limitations

  • Database and collection names are resolved as known objects only in the following situations:
    • Name is hardcoded
    • Name is defined in the variable in the local method
    • Name is defined as C# field
    • Name is provided in the App.config file
    • Database object is used while creating the database
    • DocumentCollection object is used while creating the collection

Future development

...

delete operation: {0}", response.RequestCharge);
            Console.WriteLine("StatusCode of operation: {0}", response.StatusCode);
        }


Image Added


Evolution

  • Better resolution of Database and Collection names
  • Provided support to additional APIs

Limitations

  • Unknown database and collection objects are created when unable to resolve Database and Collection
  • If the collection name is not resolved in the CRUD API, then link is created between method and Unknown collection object

Future Development

  • Support for Version .NET SDK 3.x