<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Querying the Azure cosmos for Mongo DB JSON data in Microsoft fabric for analytics in Data Engineering</title>
    <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4367610#M6163</link>
    <description>&lt;P&gt;Hello&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Use notebook to make this simple , as multiple Dataflow will be needed for different table write. Here is the sample code which you can use, this will work for any number of levels and will write to different tables for each level.&lt;BR /&gt;&amp;nbsp;Please mark the question as solved and give kudos if this is helpful.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;json_path = "path_to_json_file"&lt;BR /&gt;df = spark.read.option("multiline", "true").json(json_path)&lt;/P&gt;&lt;P&gt;# Define a function to flatten and write each level&lt;BR /&gt;def flatten_and_write(df, level, parent_field=None):&lt;BR /&gt;"""&lt;BR /&gt;Flattens a specific level of the DataFrame and writes it to a Delta table.&lt;BR /&gt;&lt;BR /&gt;Args:&lt;BR /&gt;df (DataFrame): Input DataFrame to process.&lt;BR /&gt;level (int): Current hierarchy level.&lt;BR /&gt;parent_field (str): Field to explode or flatten.&lt;BR /&gt;&lt;BR /&gt;Returns:&lt;BR /&gt;DataFrame: Flattened DataFrame for the next level.&lt;BR /&gt;"""&lt;BR /&gt;if parent_field:&lt;BR /&gt;# Explode or select nested fields&lt;BR /&gt;df = df.select(&lt;BR /&gt;*[col(c) for c in df.columns if c != parent_field], # Keep all other columns&lt;BR /&gt;explode(col(parent_field)).alias(f"level{level}_item") # Explode nested field&lt;BR /&gt;)&lt;BR /&gt;&lt;BR /&gt;# Write the current level to a table&lt;BR /&gt;table_name = f"table_name_level{level}" # Name tables dynamically based on level&lt;BR /&gt;df.write.format("delta").mode("overwrite").saveAsTable(table_name)&lt;BR /&gt;&lt;BR /&gt;return df&lt;/P&gt;&lt;P&gt;#Loop through levels and flatten iteratively&lt;BR /&gt;current_df = df # Start with the original DataFrame&lt;BR /&gt;nested_fields = ["nested_array", "nested_item.deep_nested_field"] # Replace with actual nested fields&lt;/P&gt;&lt;P&gt;for level, field in enumerate(nested_fields, start=1):&lt;BR /&gt;current_df = flatten_and_write(current_df, level, parent_field=field)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 16 Jan 2025 14:49:03 GMT</pubDate>
    <dc:creator>nilendraFabric</dc:creator>
    <dc:date>2025-01-16T14:49:03Z</dc:date>
    <item>
      <title>Querying the Azure cosmos for Mongo DB JSON data in Microsoft fabric for analytics</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4363697#M6091</link>
      <description>&lt;P&gt;HI Team,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;we have a solution (POC) where we query the Mongo DB to do anlytics in Azure synapse analytics using synapse link ,we are in the process of implementing the similar fucntionality in Microsoft fabric but as we dont have any synapse link in Microsoft fabric what would be the best approach to implement this in fabric.&lt;/P&gt;&lt;P&gt;input:&lt;/P&gt;&lt;P&gt;The data in Azure cosmos for Mongo DB is multi level Json data&lt;/P&gt;&lt;P&gt;The data continuulsy updated or inserted with new records from the system&lt;/P&gt;&lt;P&gt;we need to load and flatten the json data and store it in tables to write th queries for analysis.&lt;/P&gt;&lt;P&gt;Note : we dont have mirroring for Azure cosmos DB for Mongo&amp;nbsp;&lt;/P&gt;&lt;P&gt;can some one provide&amp;nbsp; optimized solution to implement in microsoft fabric.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jan 2025 17:04:37 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4363697#M6091</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-01-14T17:04:37Z</dc:date>
    </item>
    <item>
      <title>Re: Querying the Azure cosmos for Mongo DB JSON data in Microsoft fabric for analytics</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4365354#M6113</link>
      <description>&lt;P&gt;&lt;SPAN&gt;Hi Anonymous&lt;/LI-USER&gt;&amp;nbsp;,You can implement continuous ingestion and flattening of multi-level JSON data stored in Azure Cosmos DB for Mongo via pipelines or Spark notebooks, then store the flattened results in a Fabric lakehouse or warehouse for analytics.Create a pipeline or use a notebook to pull data from the Cosmos DB for Mongo API at scheduled intervals. Although Fabric doesn’t currently offer mirroring or Synapse Link equivalents for Mongo, you can still connect to the database using the Mongo integration in a pipeline or with Spark code to query the source.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;If you need further help please let me know , we have done the same thing in one of the project.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Jan 2025 14:54:08 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4365354#M6113</guid>
      <dc:creator>nilendraFabric</dc:creator>
      <dc:date>2025-01-15T14:54:08Z</dc:date>
    </item>
    <item>
      <title>Re: Querying the Azure cosmos for Mongo DB JSON data in Microsoft fabric for analytics</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4367503#M6161</link>
      <description>&lt;P&gt;HI &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="913195" data-lia-user-login="nilendraFabric" class="lia-mention lia-mention-user"&gt;nilendraFabric&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks you so much for the input, I have few question regarding flattening the data.&lt;/P&gt;&lt;P&gt;My json has 10 to 12 levels of hirearchy and they need to stored in different tables, can you guide me the better way to flatten the data into multiple levels and store them accordingly to the respective tables in Dataware house.&lt;/P&gt;&lt;P&gt;I tried using dataflow gen2 I can flatten the data into multiple levels but when storing the data we can point to single table.&lt;/P&gt;&lt;P&gt;If i use dataflowgen2 should i use multiple dataflowgen2 to flatten at different levels and store them to the respective tables.&lt;/P&gt;&lt;P&gt;what would be the optimizes solution.&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 14:14:12 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4367503#M6161</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-01-16T14:14:12Z</dc:date>
    </item>
    <item>
      <title>Re: Querying the Azure cosmos for Mongo DB JSON data in Microsoft fabric for analytics</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4367610#M6163</link>
      <description>&lt;P&gt;Hello&amp;nbsp;Anonymous&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Use notebook to make this simple , as multiple Dataflow will be needed for different table write. Here is the sample code which you can use, this will work for any number of levels and will write to different tables for each level.&lt;BR /&gt;&amp;nbsp;Please mark the question as solved and give kudos if this is helpful.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;json_path = "path_to_json_file"&lt;BR /&gt;df = spark.read.option("multiline", "true").json(json_path)&lt;/P&gt;&lt;P&gt;# Define a function to flatten and write each level&lt;BR /&gt;def flatten_and_write(df, level, parent_field=None):&lt;BR /&gt;"""&lt;BR /&gt;Flattens a specific level of the DataFrame and writes it to a Delta table.&lt;BR /&gt;&lt;BR /&gt;Args:&lt;BR /&gt;df (DataFrame): Input DataFrame to process.&lt;BR /&gt;level (int): Current hierarchy level.&lt;BR /&gt;parent_field (str): Field to explode or flatten.&lt;BR /&gt;&lt;BR /&gt;Returns:&lt;BR /&gt;DataFrame: Flattened DataFrame for the next level.&lt;BR /&gt;"""&lt;BR /&gt;if parent_field:&lt;BR /&gt;# Explode or select nested fields&lt;BR /&gt;df = df.select(&lt;BR /&gt;*[col(c) for c in df.columns if c != parent_field], # Keep all other columns&lt;BR /&gt;explode(col(parent_field)).alias(f"level{level}_item") # Explode nested field&lt;BR /&gt;)&lt;BR /&gt;&lt;BR /&gt;# Write the current level to a table&lt;BR /&gt;table_name = f"table_name_level{level}" # Name tables dynamically based on level&lt;BR /&gt;df.write.format("delta").mode("overwrite").saveAsTable(table_name)&lt;BR /&gt;&lt;BR /&gt;return df&lt;/P&gt;&lt;P&gt;#Loop through levels and flatten iteratively&lt;BR /&gt;current_df = df # Start with the original DataFrame&lt;BR /&gt;nested_fields = ["nested_array", "nested_item.deep_nested_field"] # Replace with actual nested fields&lt;/P&gt;&lt;P&gt;for level, field in enumerate(nested_fields, start=1):&lt;BR /&gt;current_df = flatten_and_write(current_df, level, parent_field=field)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Jan 2025 14:49:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4367610#M6163</guid>
      <dc:creator>nilendraFabric</dc:creator>
      <dc:date>2025-01-16T14:49:03Z</dc:date>
    </item>
    <item>
      <title>Re: Querying the Azure cosmos for Mongo DB JSON data in Microsoft fabric for analytics</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4371033#M6218</link>
      <description>&lt;P&gt;Hello&amp;nbsp;Anonymous&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for reaching out to the Microsoft fabric cummunity forum&amp;nbsp;regarding your query about flattening multi-level JSON data from Azure Cosmos DB for Mongo in Microsoft Fabric.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The suggestions provided by &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="913195" data-lia-user-login="nilendraFabric" class="lia-mention lia-mention-user"&gt;nilendraFabric&lt;/a&gt;, including the use of Spark notebooks and pipelines for continuous ingestion, were aimed at helping you implement the required functionality effectively.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please let us know if the proposed solution resolved your issue? &lt;SPAN data-teams="true"&gt; If so, please mark the helpful reply and accept it as the solution. This will be helpful for other community members who have similar problems to solve it faster.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thankyou.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jan 2025 03:57:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4371033#M6218</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-01-20T03:57:53Z</dc:date>
    </item>
    <item>
      <title>Re: Querying the Azure cosmos for Mongo DB JSON data in Microsoft fabric for analytics</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4371485#M6233</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="913195" data-lia-user-login="nilendraFabric" class="lia-mention lia-mention-user"&gt;nilendraFabric&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your time in providing the solution.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jan 2025 09:08:18 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Querying-the-Azure-cosmos-for-Mongo-DB-JSON-data-in-Microsoft/m-p/4371485#M6233</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-01-20T09:08:18Z</dc:date>
    </item>
  </channel>
</rss>

