<?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: Is it possible to write a pyspark dataframe to a custom log table in Log Analytics workspace? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/is-it-possible-to-write-a-pyspark-dataframe-to-a-custom-log/m-p/5038#M1568</link>
    <description>&lt;P&gt;Thanks a lot @Suteja Kanuri​&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And the opposite, do you know how I can read those tables and using as a Pyspark DataFrames ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Once again thank you very much !! &lt;/P&gt;</description>
    <pubDate>Mon, 15 May 2023 08:01:21 GMT</pubDate>
    <dc:creator>frank7</dc:creator>
    <dc:date>2023-05-15T08:01:21Z</dc:date>
    <item>
      <title>Is it possible to write a pyspark dataframe to a custom log table in Log Analytics workspace?</title>
      <link>https://community.databricks.com/t5/data-engineering/is-it-possible-to-write-a-pyspark-dataframe-to-a-custom-log/m-p/5036#M1566</link>
      <description>&lt;P&gt;I have a pyspark dataframe that contains information about the tables that I have on sql database (creation date, number of rows, etc)&lt;/P&gt;&lt;P&gt;Sample data:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt; {
   "Day":"2023-04-28",
   "Environment":"dev",
   "DatabaseName":"default",
   "TableName":"discount",
   "CountRows":31253
}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and I want to write this dataframe to a custom log table that I created on Log Analytics workspace, is it possible?&lt;/P&gt;&lt;P&gt;Thank you !&lt;/P&gt;</description>
      <pubDate>Fri, 28 Apr 2023 19:25:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/is-it-possible-to-write-a-pyspark-dataframe-to-a-custom-log/m-p/5036#M1566</guid>
      <dc:creator>frank7</dc:creator>
      <dc:date>2023-04-28T19:25:13Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to write a pyspark dataframe to a custom log table in Log Analytics workspace?</title>
      <link>https://community.databricks.com/t5/data-engineering/is-it-possible-to-write-a-pyspark-dataframe-to-a-custom-log/m-p/5037#M1567</link>
      <description>&lt;P&gt;@Bruno Simoes​&amp;nbsp;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Yes, it is possible to write a PySpark DataFrame to a custom log table in Log Analytics workspace using the Azure Log Analytics Workspace API.&lt;/P&gt;&lt;P&gt;Here's a high-level overview of the steps you can follow:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Create an Azure Log Analytics Workspace and obtain the Workspace ID and Primary Key.&lt;/LI&gt;&lt;LI&gt;Install the 'azure-loganalytics' library using pip.&lt;/LI&gt;&lt;LI&gt;Create a new instance of the 'LogAnalyticsWorkspace' class from the 'azure.loganalytics' module using the Workspace ID and Primary Key.&lt;/LI&gt;&lt;LI&gt;Convert your PySpark DataFrame to a Pandas DataFrame using the 'toPandas()' method.&lt;/LI&gt;&lt;LI&gt;Convert the Pandas DataFrame to a JSON string using the 'to_json()' method.&lt;/LI&gt;&lt;LI&gt;Use the 'LogAnalyticsWorkspace' instance to send the JSON string to the custom log table using the 'post_data()' method.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;Here's some example code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from azure.loganalytics import LogAnalyticsWorkspace
import pandas as pd
&amp;nbsp;
# Replace with your Workspace ID and Primary Key
workspace_id = 'YOUR_WORKSPACE_ID'
primary_key = 'YOUR_PRIMARY_KEY'
&amp;nbsp;
# Create a new instance of the LogAnalyticsWorkspace class
workspace = LogAnalyticsWorkspace(workspace_id, primary_key)
&amp;nbsp;
# Convert PySpark DataFrame to Pandas DataFrame
pandas_df = spark_df.toPandas()
&amp;nbsp;
# Convert Pandas DataFrame to JSON string
json_str = pandas_df.to_json(orient='records')
&amp;nbsp;
# Send JSON string to custom log table in Log Analytics workspace
workspace.post_data('CUSTOM_LOG_TABLE_NAME', json_str)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Replace 'YOUR_WORKSPACE_ID', 'YOUR_PRIMARY_KEY', and 'CUSTOM_LOG_TABLE_NAME' with your own values.&lt;/P&gt;</description>
      <pubDate>Sat, 13 May 2023 16:55:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/is-it-possible-to-write-a-pyspark-dataframe-to-a-custom-log/m-p/5037#M1567</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-05-13T16:55:43Z</dc:date>
    </item>
    <item>
      <title>Re: Is it possible to write a pyspark dataframe to a custom log table in Log Analytics workspace?</title>
      <link>https://community.databricks.com/t5/data-engineering/is-it-possible-to-write-a-pyspark-dataframe-to-a-custom-log/m-p/5038#M1568</link>
      <description>&lt;P&gt;Thanks a lot @Suteja Kanuri​&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And the opposite, do you know how I can read those tables and using as a Pyspark DataFrames ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Once again thank you very much !! &lt;/P&gt;</description>
      <pubDate>Mon, 15 May 2023 08:01:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/is-it-possible-to-write-a-pyspark-dataframe-to-a-custom-log/m-p/5038#M1568</guid>
      <dc:creator>frank7</dc:creator>
      <dc:date>2023-05-15T08:01:21Z</dc:date>
    </item>
  </channel>
</rss>

