- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-28-2023 12:25 PM
I have a pyspark dataframe that contains information about the tables that I have on sql database (creation date, number of rows, etc)
Sample data:
{
"Day":"2023-04-28",
"Environment":"dev",
"DatabaseName":"default",
"TableName":"discount",
"CountRows":31253
}
and I want to write this dataframe to a custom log table that I created on Log Analytics workspace, is it possible?
Thank you !
- Labels:
-
Azure
-
Log Analytics
-
Pyspark
-
Synapse
Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2023 09:55 AM
@Bruno Simoes :
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.
Here's a high-level overview of the steps you can follow:
- Create an Azure Log Analytics Workspace and obtain the Workspace ID and Primary Key.
- Install the 'azure-loganalytics' library using pip.
- Create a new instance of the 'LogAnalyticsWorkspace' class from the 'azure.loganalytics' module using the Workspace ID and Primary Key.
- Convert your PySpark DataFrame to a Pandas DataFrame using the 'toPandas()' method.
- Convert the Pandas DataFrame to a JSON string using the 'to_json()' method.
- Use the 'LogAnalyticsWorkspace' instance to send the JSON string to the custom log table using the 'post_data()' method.
Here's some example code:
from azure.loganalytics import LogAnalyticsWorkspace
import pandas as pd
# Replace with your Workspace ID and Primary Key
workspace_id = 'YOUR_WORKSPACE_ID'
primary_key = 'YOUR_PRIMARY_KEY'
# Create a new instance of the LogAnalyticsWorkspace class
workspace = LogAnalyticsWorkspace(workspace_id, primary_key)
# Convert PySpark DataFrame to Pandas DataFrame
pandas_df = spark_df.toPandas()
# Convert Pandas DataFrame to JSON string
json_str = pandas_df.to_json(orient='records')
# Send JSON string to custom log table in Log Analytics workspace
workspace.post_data('CUSTOM_LOG_TABLE_NAME', json_str)
Replace 'YOUR_WORKSPACE_ID', 'YOUR_PRIMARY_KEY', and 'CUSTOM_LOG_TABLE_NAME' with your own values.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2023 09:55 AM
@Bruno Simoes :
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.
Here's a high-level overview of the steps you can follow:
- Create an Azure Log Analytics Workspace and obtain the Workspace ID and Primary Key.
- Install the 'azure-loganalytics' library using pip.
- Create a new instance of the 'LogAnalyticsWorkspace' class from the 'azure.loganalytics' module using the Workspace ID and Primary Key.
- Convert your PySpark DataFrame to a Pandas DataFrame using the 'toPandas()' method.
- Convert the Pandas DataFrame to a JSON string using the 'to_json()' method.
- Use the 'LogAnalyticsWorkspace' instance to send the JSON string to the custom log table using the 'post_data()' method.
Here's some example code:
from azure.loganalytics import LogAnalyticsWorkspace
import pandas as pd
# Replace with your Workspace ID and Primary Key
workspace_id = 'YOUR_WORKSPACE_ID'
primary_key = 'YOUR_PRIMARY_KEY'
# Create a new instance of the LogAnalyticsWorkspace class
workspace = LogAnalyticsWorkspace(workspace_id, primary_key)
# Convert PySpark DataFrame to Pandas DataFrame
pandas_df = spark_df.toPandas()
# Convert Pandas DataFrame to JSON string
json_str = pandas_df.to_json(orient='records')
# Send JSON string to custom log table in Log Analytics workspace
workspace.post_data('CUSTOM_LOG_TABLE_NAME', json_str)
Replace 'YOUR_WORKSPACE_ID', 'YOUR_PRIMARY_KEY', and 'CUSTOM_LOG_TABLE_NAME' with your own values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2023 01:01 AM
Thanks a lot @Suteja Kanuri 🙂
And the opposite, do you know how I can read those tables and using as a Pyspark DataFrames ?
Once again thank you very much !!

