How to query Cosmos DB (analytical store) in Data Factory using SQL (Change Data Capture)

Christopher Wilkinson 0 Reputation points
2025-01-17T09:02:48.11+00:00

Hi,

I am trying to write a query to interrogate Cosmos DB in Data Factory using the Change Data Capture functionality but I am unsure of the type of SQL I need to do this for a query that unrolls an array. It seems that the variant of SQL is different for queries against the Analytical Store. For example, when writing a query that unrolls an array against the container in the transactional db, the SQL query in the data flow is the same as the variant used to query containers directly in Cosmos db:

SELECT c.id, r.type, r.description FROM c JOIN r in c.requirements

However, this query raises an error when used against the Analytical Store in a Data Flow. I tried using the SQL that is used in a serverless SQL pool but that also produces an error, i.e.:

SELECT c.id, r.type, r.description FROM c CROSS APPLY OPENJSON(c.requirements) WITH ( type INT '$.type', description VARCHAR(500) '$.description' ) r

My question is what is the syntax for a SQL query (in the Source Options section of a data flow) that unrolls an array and that queries the Analytical Store as is necessary for CDC. As can be seen from the queries above, the array is the requirements field which has type and description as fields within it.

Any help would be greatly appreciated.

Regards,

Chris

Azure SQL Database
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,742 questions
Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,183 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sina Salam 16,766 Reputation points
    2025-01-17T14:46:14.55+00:00

    Hello Christopher Wilkinson,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand with your SQL query to interrogate Cosmos DB in Data Factory using Change Data Capture (CDC) functionality to unroll an array in the Analytical Store.

    To unroll an array in the Analytical Store using CDC in Data Factory - https://learn.microsoft.com/en-us/azure/cosmos-db/analytical-store-change-data-capture you should use the following SQL syntax:

    SELECT 
        c.id, 
        r.type, 
        r.description 
    FROM 
        c 
    CROSS APPLY 
        OPENROWSET(
            'CosmosDB',
            'SELECT * FROM c.requirements'
        ) AS r
    
    

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.