Query Cosmos Document

 

This Tutorial explains and lists all the necessary steps to query a document from Cosmos nosql database using its API in Mule 4. Here is the link for cosmos query document API query documents

Authorization behavior for all the calls will be similar but we have to change verb as per our need, so we have to update Dataweave and HTTP request call to Azure.

Mule flow will look like this

Query Cosmos Document

 

Dataweave

%dw 2.0
import java!cosmosdb::GenerateCosmosDBAuthToken
output application/java
var verb = "POST"
var resourceType = "docs" 
var resourceId = "dbs/hr/colls/employee"
var key = "0SZd1Zs3hySH915hKBTQCbewWnzIEI4S2YK8yHDaRwMrnzrZSwaa0hHy8IW21x4qbsQoswWIh3FW8TOd0fpSYw=="
var keyType = "master"
var tokenVersion = "1.0"		 
var serverDateTime = GenerateCosmosDBAuthToken::getServerTime() as String
---
{
	serverDateTime: serverDateTime,
	authKey: GenerateCosmosDBAuthToken::generate(verb, resourceType, resourceId, key, keyType, tokenVersion, serverDateTime)
}

Please change the resource id and key as per your Azure details

 

HTTP request

<http:request method="POST" doc:name="Cosmos DB API" doc:id="d41bbcee-aa87-455c-9bda-9ed4e990395e" config-ref="HTTP_Request_configuration" path="dbs/hr/colls/employee/docs" >
	<http:headers ><![CDATA[#[output application/java
	---
	{
		"Accept" : "application/json",
		"x-ms-version" : "2016-07-11",
		"Authorization" : vars.cosmosHeader.authKey,
		"x-ms-date" : vars.cosmosHeader.serverDateTime,
		"Content-Type" : "application/query+json",
		"x-ms-documentdb-isquery" : "True" ,
		"x-ms-documentdb-query-enablecrosspartition": "True"
	}]]]></http:headers>
</http:request>

Please see the path and different HTTP header we are passing in HTTP request call, please change the same as per your need

Test it through SOAP UI

Query Cosmos Document

 

Documents in Azure

Query Cosmos Document

 

Sample application – cosmos-document-query

Sample SOAP UI project – Cosmos-db-document-query-soapui-project

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments