Cosmos Db Throughput - azure

Is there any option to retrieve Cosmos DB (SQL API) throughput programmatically. I'm using the below code to get the list of DB
DocumentClient client = new DocumentClient(ClientURL, ClientKey,);
var databaseList = client.CreateDatabaseQuery().ToList();
Next I wanted to know the throughput for each of the database.
Please let me know if this is feasible

You could refer to CreateOfferQuery method to get throughout settings of Database or Collections.
Also,please refer to this rest api:https://learn.microsoft.com/en-us/rest/api/cosmos-db/get-an-offer

Related

Multiple CosmosClient for single cosmos db account

I read online that one should keep single instance of CosmosClient for a cosmos db account per application.
In my case, my app & cosmos db is deployed to multiple regions.
Normally the app will read from the cosmos db in the same region.
However, in some scenario I want my app (whichever region it is running) to read from single cosmos db region, e.g. East US always.
Reason is, our cosmos db is on bounded staleness consistency, so data might not be replicated to other read regions instantaneously.
If I always write & read from the same region, I will be guaranteed to see the document there. So I am sacrificing latency for consistency in that scenario.
In order to achieve this, I have to specify which region I want to read from
var clientOptions = new CosmosClientOptions
{
ApplicationRegion = "East US"
};
return new CosmosClient(_cosmosDbDataConnectionOptions.CosmosDbUrl, new DefaultAzureCredential(), clientOptions);
I want to use this CosmosClient for specific scenario.
In normal case, I will set
ApplicationRegion = <app deployed region>
This requires me to have 2 CosmosClient for the same cosmos db account. Does it make sense to have 2 CosmosClient then ? Or is there any other recommended approach to this problem.
I looked up google and found out https://learn.microsoft.com/en-us/azure/cosmos-db/nosql/performance-tips-dotnet-sdk-v3?tabs=trace-net-core#sdk-usage . This recommends me to have 1 cosmos client per app. But in my case, I have to set read region differently per scenario.
If the concern is about Consistency, then the SessionToken might help (Session Token from the Write on a Read call even if they are different client instances). If you have other scenarios where the logic is different and you, for whatever reason, want to change the read endpoint, yes, there is no way to flip it on a running client or say for a particular request, that you want it to go to another reagion.

Is there an Azure cosmos db / Azure SQL database function to subscribe to data changes like in Firestore?

For example to get real-time updates in firestore
db.collection("cities").where("state", "==", "CA")
.onSnapshot((querySnapshot) => {
var cities = [];
querySnapshot.forEach((doc) => {
cities.push(doc.data().name);
});
console.log("Current cities in CA: ", cities.join(", "));
});
How can I do that with Azure cosmos db or Azure SQL database, whichever one is possible?
The thing you're looking for is Azure Cosmos DB Change Feed - https://learn.microsoft.com/en-us/azure/cosmos-db/change-feed.
From the above link:
Change feed in Azure Cosmos DB is a persistent record of changes to a
container in the order they occur. Change feed support in Azure Cosmos
DB works by listening to an Azure Cosmos container for any changes. It
then outputs the sorted list of documents that were changed in the
order in which they were modified. The persisted changes can be
processed asynchronously and incrementally, and the output can be
distributed across one or more consumers for parallel processing.

How to create single partitioned collection in Cosmos DB using Azure portal

I'm working with Azure Cosmos DB, and I need to fetch all the documents of a particular collection of database. So that for executing a stored procedure they ask to enter partition key value, but I need the query result without any filter.
How can I create a collection for a particular database without mentioning the partition key? I'm accessing Cosmos DB in https://portal.azure.com/. I have to create a collection from that UI itself, not from code.
Firstly,about stored procedure execution needs partition key.You could find the below clear statements in the link:
If a stored procedure is associated with an Azure Cosmos container,
then the stored procedure is executed in the transaction scope of a
logical partition key. Each stored procedure execution must include a
logical partition key value that corresponds to the scope of the
transaction. For more information, see Azure Cosmos DB partitioning
article.
Secondly,in the past,you could create non-partitioned collection on the portal.But now, you can't.Please see my previous case:Is it still a good idea to create comos db collection without partition key?. Based on your description,you don't want partitioned collection.So, please create non-partitioned collection by Cosmos DB SDK. Such as:
DocumentCollection collection = new DocumentCollection();
collection.set("id","jay");
ResourceResponse<DocumentCollection> createColl = client.createCollection("dbs/db",collection,null);

CosmosDB How to read replicated data

I'm using CosmosDB and replicating the data globally. (One Write region; multiple Read regions). Using the Portal's Data Explorer, I can see the data in the Write region. How can I query data in the Read regions? I'd like some assurance that it's actually working, and haven't been able to find any info or even an URL for the replicated DBs.
Note: I'm writing to the DB via the CosmosDB "Create or update document" Connector in a Logic App. Given that this is a codeless environment, I'd prefer to validate the replication without having to write code.
How can I query data in the Read regions?
If code is possible, we could access from every region your application is deployed, configure the corresponding preferred regions list for each region via one of the supported SDKs
The following is the demo code for Azure SQL API CosmosDB. For more information, please refer to this tutorial.
ConnectionPolicy usConnectionPolicy = new ConnectionPolicy
{
ConnectionMode = ConnectionMode.Direct,
ConnectionProtocol = Protocol.Tcp
};
usConnectionPolicy.PreferredLocations.Add(LocationNames.WestUS); //first preference
usConnectionPolicy.PreferredLocations.Add(LocationNames.NorthEurope); //second preference
DocumentClient usClient = new DocumentClient(
new Uri("https://contosodb.documents.azure.com"),
"<Fill your Cosmos DB account's AuthorizationKey>",
usConnectionPolicy);
Update:
We can enable Automatic Failover from Azure portal. Then we could drag and drop the read regions items to recorder the failover priorties.

How to create advanced queries for azure table storage in NodeJS?

I am working with Azure table storage in NodeJS using the azure-storage package (https://github.com/Azure/azure-storage-node)
According to the documentation an advanced query can be created and executed like this
var azure = require('azure-storage');
var tableService = azure.createTableService();
var query = new azure.TableQuery()
.top(5)
.where('PartitionKey eq ?', 'part2');
tableSvc.queryEntities('mytable', query, null, function(error, result, response) {
if (!error) {
// result.entries contains entities matching the query
}
});
This code works. However all of the query operators documented are very basic ones (https://msdn.microsoft.com/en-us/library/dd894031.aspx). Does any know any of the other operators or another way to write queries against the Azure table storage within NodeJS? (for instance something similar to inner queries?)
It seems that aggregation query operations are still not supported for Azure Table Storage in service tier but the client SDK tier, you can find the all supported and not supported operations at Query Operators Supported for the Table Service
As if you need to use relational queries, you can refer to traditional database services, like MySQL or SQL Server.

Resources