<?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: how to get schema and catalog name in sql warehouse query history API in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76679#M35296</link>
    <description>&lt;P&gt;True&amp;nbsp; ! try this :&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests
import json

# Define your Databricks workspace URL and API token
databricks_instance = "https://&amp;lt;your-databricks-instance&amp;gt;"
api_token = "dapi&amp;lt;your-api-token&amp;gt;"

# Fetch SQL query history
def get_query_history():
    url = f"{databricks_instance}/api/2.0/sql/history/queries"
    headers = {
        "Authorization": f"Bearer {api_token}"
    }
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    return response.json()

# Retrieve query history
query_history = get_query_history()

# Print retrieved query history for inspection
print(json.dumps(query_history, indent=4))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def extract_query_metadata(query_history):
    for query in query_history.get("res", []):
        query_id = query.get("query_id")
        query_text = query.get("query_text")
        user_name = query.get("user_name")
        # Fetch metadata from your Databricks environment or catalog
        # This is a placeholder function
        catalog, schema = fetch_catalog_and_schema(query_text, user_name)
        print(f"Query ID: {query_id}")
        print(f"Query Text: {query_text}")
        print(f"Catalog: {catalog}")
        print(f"Schema: {schema}")
        print("---")

# Placeholder function to fetch catalog and schema based on query text and user context
def fetch_catalog_and_schema(query_text, user_name):
    # Implement logic to fetch catalog and schema
    # For example, parse the query text to identify table names
    # and cross-reference with known metadata
    # Return default values if necessary
    catalog = "default_catalog"
    schema = "default_schema"
    return catalog, schema

# Extract and print metadata
extract_query_metadata(query_history)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 03 Jul 2024 20:05:29 GMT</pubDate>
    <dc:creator>mtajmouati</dc:creator>
    <dc:date>2024-07-03T20:05:29Z</dc:date>
    <item>
      <title>how to get schema and catalog name in sql warehouse query history API</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76496#M35227</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;we are using SQL history query API by selecting catalog and schema name directly from SQL editor instead of passing it through query, we are not getting the schema name and catalog name in query text for that particular id.&lt;/P&gt;&lt;P&gt;So, how can we get the schema and catalog name for that types of queries because the queryid also does not match with the respective system generated query.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Ayush&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2024 10:33:56 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76496#M35227</guid>
      <dc:creator>ayush25091995</dc:creator>
      <dc:date>2024-07-02T10:33:56Z</dc:date>
    </item>
    <item>
      <title>Re: how to get schema and catalog name in sql warehouse query history API</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76525#M35244</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/109767"&gt;@ayush25091995&lt;/a&gt;&amp;nbsp;, check the "Query Profile" tab in the UI or check the Spark plans through the Spark UI.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2024 14:48:14 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76525#M35244</guid>
      <dc:creator>raphaelblg</dc:creator>
      <dc:date>2024-07-02T14:48:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to get schema and catalog name in sql warehouse query history API</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76538#M35253</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/97998"&gt;@raphaelblg&lt;/a&gt;&amp;nbsp;, we are using REST API for sql warehouse&amp;nbsp; to get the query history and query is being written in sql editor with selected catalog and schema name in drop down of editor, so for that particulars queries, we are just getting table name in response, not the schema and catalog name&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2024 18:44:24 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76538#M35253</guid>
      <dc:creator>ayush25091995</dc:creator>
      <dc:date>2024-07-02T18:44:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to get schema and catalog name in sql warehouse query history API</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76543#M35254</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;You can try :&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Function to extract schema and catalog from query history
def extract_schema_catalog(query_history):
    for query in query_history.get("res", []):
        query_id = query.get("query_id")
        query_text = query.get("query_text")
        catalog = query.get("metadata", {}).get("details", {}).get("catalog")
        schema = query.get("metadata", {}).get("details", {}).get("schema")
        print(f"Query ID: {query_id}")
        print(f"Query Text: {query_text}")
        print(f"Catalog: {catalog}")
        print(f"Schema: {schema}")&lt;/LI-CODE&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>Tue, 02 Jul 2024 20:00:24 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76543#M35254</guid>
      <dc:creator>mtajmouati</dc:creator>
      <dc:date>2024-07-02T20:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to get schema and catalog name in sql warehouse query history API</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76591#M35272</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/109839"&gt;@mtajmouati&lt;/a&gt;,&amp;nbsp;there is no field/key called &lt;STRONG&gt;metadata&lt;/STRONG&gt; in API response for sql query history.&amp;nbsp;&lt;BR /&gt;&lt;A href="https://docs.databricks.com/api/azure/workspace/queryhistory/list" target="_blank" rel="noopener"&gt;https://docs.databricks.com/api/azure/workspace/queryhistory/list&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 08:34:08 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76591#M35272</guid>
      <dc:creator>ayush25091995</dc:creator>
      <dc:date>2024-07-03T08:34:08Z</dc:date>
    </item>
    <item>
      <title>Re: how to get schema and catalog name in sql warehouse query history API</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76679#M35296</link>
      <description>&lt;P&gt;True&amp;nbsp; ! try this :&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests
import json

# Define your Databricks workspace URL and API token
databricks_instance = "https://&amp;lt;your-databricks-instance&amp;gt;"
api_token = "dapi&amp;lt;your-api-token&amp;gt;"

# Fetch SQL query history
def get_query_history():
    url = f"{databricks_instance}/api/2.0/sql/history/queries"
    headers = {
        "Authorization": f"Bearer {api_token}"
    }
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    return response.json()

# Retrieve query history
query_history = get_query_history()

# Print retrieved query history for inspection
print(json.dumps(query_history, indent=4))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def extract_query_metadata(query_history):
    for query in query_history.get("res", []):
        query_id = query.get("query_id")
        query_text = query.get("query_text")
        user_name = query.get("user_name")
        # Fetch metadata from your Databricks environment or catalog
        # This is a placeholder function
        catalog, schema = fetch_catalog_and_schema(query_text, user_name)
        print(f"Query ID: {query_id}")
        print(f"Query Text: {query_text}")
        print(f"Catalog: {catalog}")
        print(f"Schema: {schema}")
        print("---")

# Placeholder function to fetch catalog and schema based on query text and user context
def fetch_catalog_and_schema(query_text, user_name):
    # Implement logic to fetch catalog and schema
    # For example, parse the query text to identify table names
    # and cross-reference with known metadata
    # Return default values if necessary
    catalog = "default_catalog"
    schema = "default_schema"
    return catalog, schema

# Extract and print metadata
extract_query_metadata(query_history)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 20:05:29 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76679#M35296</guid>
      <dc:creator>mtajmouati</dc:creator>
      <dc:date>2024-07-03T20:05:29Z</dc:date>
    </item>
    <item>
      <title>Re: how to get schema and catalog name in sql warehouse query history API</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76683#M35298</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/109839"&gt;@mtajmouati&lt;/a&gt;,&amp;nbsp; i got your point and even thought same but the issue is, lets say, i have got table name after parsing query text, and if i match with metadata to find catalog and schema, there could be possibility that same table might be present in multiple schema and catalog right?&lt;BR /&gt;How will i make sure the query that i run for my catalog and schema is which one? below image if you see, after selecting catalog and schema, i am just running simple query and in query history i am getting without schema and catalog for below query, so how will i make sure i ran query for this particular catalog and schema? what if multiple entries match with same table name right.&lt;/P&gt;&lt;P&gt;SQL_Editor:-&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ayush25091995_0-1720038116765.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/9191iA07BCF4786F83FF6/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="ayush25091995_0-1720038116765.png" alt="ayush25091995_0-1720038116765.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Query history:-&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ayush25091995_1-1720038378281.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/9192i0A7912562274B127/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400" role="button" title="ayush25091995_1-1720038378281.png" alt="ayush25091995_1-1720038378281.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 03 Jul 2024 20:19:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-get-schema-and-catalog-name-in-sql-warehouse-query/m-p/76683#M35298</guid>
      <dc:creator>ayush25091995</dc:creator>
      <dc:date>2024-07-03T20:19:43Z</dc:date>
    </item>
  </channel>
</rss>

