Which .NET SDK is used for connecting to azure cosmos DB?
Microsoft.Azure.Cosmos library is the latest version of the .NET SDK for azure cosmos DB for NoSQL. The library is open source and is hosted online on GitHub at azure/azure-cosmos-dotnet-v3.
Explain the following classes of the .NET SDK?
Microsoft.Azure.Cosmos.CosmosClient – this is a client side logical representation of cosmos DB account and the primary class used for SDK.
Microsoft.Azure.Cosmos.Database – logically represents a database client side and includes common operations for database management.
Microsoft.Azure.Cosmos.Container – Logically represents a container client-side and includes common operation for container management.
Where is the Microsoft.Azure.cosmos library hosted?
Microsoft.azure.cosmos library along with its previous versions are hosted on nuget.
How can we import the NuGet package into .NET application?
To import a NuGet package into a .NET application, you must use the .NET CLI. The CLI includes a dotnet add command to add a resource to a .NET application.
Once the Microsoft.Azure.Cosmos package has been imported, where does it lie?
Once imported, the package specification will be added to the csproj file for the .NET project. The project file uses XML format and a new element named PackageReference is created within the ItemGroup element with the name of the package and version.
How can we create instance of the CosmosClient class?
The two most common ways to create an instance for the CosmosClient class is to instantiate it with one of the following two constructors –
- A constructor that takes single string value representing the connection string for the account.
- A constructor that takes two string values representing the endpoint and a key for the account.
Once the Cosmos class is instantiated, what can be done with the ReadAccountAsync method?
you can asynchronously invoke the ReadAccountAsync method to get an object of type AccountProperties.
What does the following properties of AccountProperties class mean –
id – gets the unique name of the account
ReadableRegions – gets a list of readable regions for the account
WritableRegions – gets a list of writable locations for the account
Consistency – gets the default consistency for the account.
Once a CosmosClient instance has been created, what method is used to create a database, retrieve a database, and check if a database exist, if not create –
GetDatabase – This is done to retrieve an existing database
CreateDatabaseAsync – this is used to create a new database
CreateDatabaseIfNotExistAsync – this is used to create a new database if the old one doe snot exist
Once a CosmosClient instance has been created, what method is used to create a container, retrieve a container, and check if a container exist, if not create –
GetContainer – This is done to retrieve an existing container
CreateContainerAsync – this is used to create a new container
CreateContainerfNotExistAsync – this is used to create a new container if the old one doe snot exist
What features of the CosmosClient are already implemented on your behalf?
- instances are already thread safe
- instances efficiently manage connections
- instance cache addresses when operating in direct mode
What connection options are available with the cosmosClientConnection class?
Within the CosmosClientConnection class, you can set the ConnectionMode property to one of the possible values –
- Gateway – all requests are routed through the azure cosmos DB gateway as proxy
- Direct – the gateway is used only in initialization and to cache addresses for direct connectivity to data nodes. This is the default mode for connectivity.
What are the possible values for the consistency levels?
- Bounded staleness
- Consistent Prefix
- Eventual
- session
- strong
Where can we use the consistency level?
The consistency level can only be used to weaken the consistency levels for the read. it cannot be strengthened or applied to writes.
Can we override the preferred region for write?
By default, the client will use the first writable region for requests. This is typically referred to as the primary region. You can either use CosmosClientOptions.ApplicationRegion or CosmosClientOptions.ApplicationPreferredRegions to override this behavior.
What is the difference in functionality of ApplicationRegion and ApplicationPreferredRegions?
The ApplicationRegion property sets the single preferred region that the client will connect to for operations. If the configured region is not available, the client will default back to the fallback priority list set on the account to determine the next region to use. If your account is not configured for multi-region writes, the client will always use the single writable region for write operation and this setting will only impact the read operations.
If you would like to create custom failover or priority list for the client to use for operations, you can use the ApplicationPreferredRegions property with a list of regions.
Which class should you use to interact with containers in the azure cosmos DB SDK for .NET?
Container
Which CosmosClient constructor should you use when you have two string variables for the account endpoint and key along with a CosmosClientOptions object?
new CosmosClient(string,string,CosmosClientOptions)
What is the name of the Azure Cosmos DB SDK for .NET on NuGet?
Microsoft.Azure.Cosmos
What is used to implement applications locally?
The Azure Cosmos DB Emulator is a local environment that is useful to develop and test applications locally without incurring the cost or complexity of an azure subscription. The emulator is available to run in windows, linux or as a docker container image.
What is the endpoint for emulator?
https://locahost:<port> using ssl with the default port set to 8081. The emulator key is a static well know authentication key. You can start using the emulator using the /Key option to generate a new key instead of the default key.
What is a transient error?
A transient error is an error that has an underlying cause that soon resolves itself. If you are writing an application that experiences a write failure, it is up to your application code to implement retry logic which is considered a best practice.
What is the meaning of the following codes?
- 429 – too many requests
- 449 – concurrency error
- 500 – unexpected service error
- 503 – service unavailable
- 400 – bad error
- 401- not authorized
- 403- forbidden
- 404 – not found
What is the purpose of ToList?
LINQ methods such as ToList will eagerly and synchronously drain a query while blocking any other calls from executing.
What is the purpose of ToFeedIterator<T>?
This method asynchronously retrieves the results of the query without blocking other calls.
What method should we use to do query tuning?
When issuing a query from the SDK, the QueryRequestOptions includes a set of properties to tune a query’s performance.
What method should we use to update the number of items returned in a page when executing a query?
MaxItemCount. The service default is 100 items per page of results. You can set the value to -1 to set a dynamic size. If you do use -1 , you should ensure that total response does not exceeds the service limit for response size, which is 4MB.
Which property is used to set the concurrency?
MaxConcurrency specifies the number of concurrent operations ran client side during parallel query execution. If set to 1, parallelism is effictively disabled, if set to -1, the SDK manages this setting. Ideally, this shoudl be set to the number of physical partitions of your container.
During a parallel execution, which property set the maximum number of items buffered?
The MaxItemBufferedItemCount propety sets the maximum number of items that are bufferred client side during parallel query execution. If set to -1, the SDK manages this setting.
Which class is used to fluently configure a new class intances?
The Microsoft.Azure.Cosmos.Fluent.CosmosClientBuilder class is a builder class that fluently configures a new client instance.
how can logging be done?
To log HTTP requests, you will need to create a new class that inherits from the abstract RequestHandler class. The abstract class includes a SendAysnc method that should be overridden to inject new logic around requests. In SendAsync, there are two parameters of type RequestMessage and CancellationToekn.
Within the SendAsync method, the RequestUri and Method properties of the RequestMessage parameter are printed to the console. Then the base, SendAsync method is invoked to send the actual request and store the response in a local variable.
After the response is stored in a local variable, the StatusCode of the response is printed in both numeric and string format. Once the request handler implementation is ready, invoke the AddCustomHandler method of the CosmosClientBuilder instance passing in a new instance of the custom request handler.
Which class should you inherit from to create a class that intercepts SDK-side HTTP requests and inject extra logic?
RequestHandler
Assuming that you have an Azure Cosmos DB container with 50 partitions, which of the following code will correctly configure the QueryRequestOptions instance to optimize parallelism of requests from the SDK and client?
MaxConcurrency
Foolishly Yours,
AvantikaTanubhrt
Happy Learning 🙂
Leave a comment