Trying to Flatten My Json using CosmosDB Spark connector - Azure Databricks

Tomar, Abhishek 1 Reputation point
2022-04-21T15:11:12.35+00:00

Hi,

Using the below cosmos DB query it is possible to achieve the expected output, but how can I do the same with spark SQL in Databricks.

   COSMOSDB QUERY : select c.ReportId,c.ReportName,i.price,p as provider from c join i in in_network  join p in i.provider

Source JSON

{
    "ReportId":"F0001",
    "ReportName":"ALYX_HLT",
    "in_network":[
     {"provider":[1,2,3,4],"price":10},
     {"provider":[1004],"price":100.2},
     {"provider":[39,52],"price":3}
    ]
}

Expected Output

[
 { "ReportId":"F0001","ReportName":"ALYX_HLT","provider":100,"price":10},
 { "ReportId":"F0001","ReportName":"ALYX_HLT","provider":200,"price":10},
 { "ReportId":"F0001","ReportName":"ALYX_HLT","provider":300,"price":1.3},
 { "ReportId":"F0001","ReportName":"ALYX_HLT","provider":400,"price":23.1},
 { "ReportId":"F0001","ReportName":"ALYX_HLT","provider":500,"price":23.1}
]

CosmosDB Spark Connector : https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/cosmos/azure-cosmos-spark_3_2-12/docs/quick-start.md

Question On Databricks: https://community.databricks.com/s/question/0D53f00001sgowcCAA/trying-to-flatten-my-json-using-cosmosdb-spark-connector-azure-databricks

Azure Databricks
Azure Databricks
An Apache Spark-based analytics platform optimized for Azure.
1,938 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,448 questions
.NET Machine learning
.NET Machine learning
.NET: Microsoft Technologies based on the .NET software framework.Machine learning: A type of artificial intelligence focused on enabling computers to use observed data to evolve new behaviors that have not been explicitly programmed.
150 questions
{count} votes

1 answer

Sort by: Most helpful
  1. PRADEEPCHEEKATLA-MSFT 77,751 Reputation points Microsoft Employee
    2022-04-22T07:30:18.42+00:00

    Hello @Tomar, Abhishek ,

    Thanks for the question and using MS Q&A platform.

    I see your question is already answered on the Databricks community forum.

    Please let us know if any further queries.

    ------------------------------

    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you. Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
    • If you are interested in joining the VM program and help shape the future of Q&A: Here is how you can be part of Q&A Volunteer Moderators
    0 comments No comments