<?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 use rest api to find long running query in databricks in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93170#M38640</link>
    <description>&lt;P&gt;&lt;SPAN&gt;I would like to&amp;nbsp;see if there are any queries running with run time more than 30 minutes or queries pulling more than 1 million&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Oct 2024 17:13:59 GMT</pubDate>
    <dc:creator>slakshmanan</dc:creator>
    <dc:date>2024-10-08T17:13:59Z</dc:date>
    <item>
      <title>how to use rest api to find long running query in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93008#M38608</link>
      <description>&lt;P&gt;how to use rest api to find long running query in databricks from&amp;nbsp;sql/queries/all&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 02:13:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93008#M38608</guid>
      <dc:creator>slakshmanan</dc:creator>
      <dc:date>2024-10-08T02:13:34Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rest api to find long running query in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93012#M38610</link>
      <description>&lt;P&gt;&lt;BR /&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/125216"&gt;@slakshmanan&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To find long-running queries in Databricks using the REST API, specifically from the /sql/queries/all endpoint, you'll need to follow these steps:&lt;/P&gt;&lt;P&gt;Prerequisites:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Databricks Account with REST API enabled.&lt;BR /&gt;Access Token: You need a valid access token for authentication. You can generate it via Databricks UI under the "User Settings" page.&lt;BR /&gt;Workspace URL: Your Databricks workspace URL (e.g., https://&amp;lt;databricks-instance&amp;gt;.cloud.databricks.com).&lt;BR /&gt;Steps:&lt;BR /&gt;1. API Authentication&lt;BR /&gt;You can authenticate using your Bearer Token in the headers of the API request.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Authorization: Bearer &amp;lt;Access Token&amp;gt;&lt;BR /&gt;2. REST API Endpoint&lt;BR /&gt;The API endpoint for listing all SQL queries is:&lt;/P&gt;&lt;P&gt;GET /api/2.0/sql/history/queries&lt;BR /&gt;3. Query Parameters&lt;BR /&gt;page: Page number of the results.&lt;BR /&gt;page_size: The number of queries per page.&lt;/P&gt;&lt;P&gt;This will list the SQL query history from the most recent to the oldest.&lt;/P&gt;&lt;P&gt;4. Filter for Long-Running Queries&lt;BR /&gt;You'll need to programmatically analyze the query results to identify long-running ones. In the response, you'll get details like:&lt;/P&gt;&lt;P&gt;query_id: Unique identifier for the query.&lt;BR /&gt;start_time_ms: Query start time in epoch milliseconds.&lt;BR /&gt;end_time_ms: Query end time in epoch milliseconds.&lt;BR /&gt;duration_ms: Duration of the query in milliseconds.&lt;BR /&gt;query_text: The SQL query text.&lt;BR /&gt;You can use the duration_ms to filter out queries that ran for too long.&lt;/P&gt;&lt;P&gt;Example Response Snippet:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;json&lt;/P&gt;&lt;P&gt;{&lt;BR /&gt;"res": [&lt;BR /&gt;{&lt;BR /&gt;"query_id": "abc123",&lt;BR /&gt;"query_text": "SELECT * FROM example_table",&lt;BR /&gt;"start_time_ms": 1696456800000,&lt;BR /&gt;"end_time_ms": 1696460400000,&lt;BR /&gt;"duration_ms": 3600000&lt;BR /&gt;},&lt;BR /&gt;...&lt;BR /&gt;]&lt;BR /&gt;}&lt;BR /&gt;You can calculate the duration of each query (duration_ms) and flag queries that exceed a certain threshold (e.g., 10 minutes = 600,000 ms).&lt;/P&gt;&lt;P&gt;5. Code Example (Python + requests):&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;python&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests

# Replace with your Databricks instance and access token
databricks_instance = "&amp;lt;databricks-instance&amp;gt;"
access_token = "&amp;lt;your-access-token&amp;gt;"

headers = {
"Authorization": f"Bearer {access_token}"
}

# API URL to fetch SQL query history
url = f"https://{databricks_instance}.cloud.databricks.com/api/2.0/sql/history/queries"

# API request with optional filters (pagination can be added)
response = requests.get(url, headers=headers)

if response.status_code == 200:
queries = response.json().get("res", [])

# Filter for long-running queries (e.g., &amp;gt;10 minutes)
long_running_queries = [q for q in queries if q.get('duration_ms', 0) &amp;gt; 600000]

for query in long_running_queries:
print(f"Query ID: {query['query_id']}, Duration: {query['duration_ms']} ms")
else:
print(f"Error: {response.status_code} - {response.text}")&lt;/LI-CODE&gt;&lt;P&gt;This script will fetch the query history and filter out the queries that have run for more than 10 minutes (600,000 milliseconds).&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 04:10:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93012#M38610</guid>
      <dc:creator>Ajay-Pandey</dc:creator>
      <dc:date>2024-10-08T04:10:00Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rest api to find long running query in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93146#M38628</link>
      <description>&lt;P&gt;Thanks, i will try this and let you know&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 14:38:20 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93146#M38628</guid>
      <dc:creator>slakshmanan</dc:creator>
      <dc:date>2024-10-08T14:38:20Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rest api to find long running query in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93148#M38629</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/71565"&gt;@Ajay-Pandey&lt;/a&gt;&amp;nbsp; to find out if my databricks is using rest api, how do i check it&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 14:45:32 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93148#M38629</guid>
      <dc:creator>slakshmanan</dc:creator>
      <dc:date>2024-10-08T14:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rest api to find long running query in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93170#M38640</link>
      <description>&lt;P&gt;&lt;SPAN&gt;I would like to&amp;nbsp;see if there are any queries running with run time more than 30 minutes or queries pulling more than 1 million&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 17:13:59 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93170#M38640</guid>
      <dc:creator>slakshmanan</dc:creator>
      <dc:date>2024-10-08T17:13:59Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rest api to find long running query in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93178#M38641</link>
      <description>&lt;P&gt;To check for API call logs in Databricks, you can follow these steps:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Log in to your Databricks workspace.&lt;/LI&gt;&lt;LI&gt;If you have the necessary permissions, click on your username in the top right corner and select &lt;STRONG&gt;Admin Console&lt;/STRONG&gt;.&lt;/LI&gt;&lt;LI&gt;In the Admin Console, look for the &lt;STRONG&gt;Audit Logs&lt;/STRONG&gt; section. This section provides detailed logs of actions taken in the workspace, including API calls.&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Tue, 08 Oct 2024 17:39:16 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93178#M38641</guid>
      <dc:creator>Rishabh-Pandey</dc:creator>
      <dc:date>2024-10-08T17:39:16Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rest api to find long running query in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93191#M38647</link>
      <description>&lt;P&gt;&lt;SPAN&gt;how to cancel or kill a long running sql query from databricks python notebook.I have a long running sql query in sql warehouse&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Oct 2024 21:40:36 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93191#M38647</guid>
      <dc:creator>slakshmanan</dc:creator>
      <dc:date>2024-10-08T21:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rest api to find long running query in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93233#M38659</link>
      <description>&lt;P&gt;You can run this query to get the long running queries and then kill the query you wanted to kill.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Step 1: Get active queries
active_queries = spark.sql("SHOW PROCESSLIST")
active_queries.show(truncate=False)

# Step 2: Identify the query ID you want to kill
# (Assume you noted the ID as '12345')

# Step 3: Kill the query
spark.sql("KILL QUERY 12345")&lt;/LI-CODE&gt;&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/125216"&gt;@slakshmanan&lt;/a&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Oct 2024 06:43:54 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/93233#M38659</guid>
      <dc:creator>Rishabh-Pandey</dc:creator>
      <dc:date>2024-10-09T06:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rest api to find long running query in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/98413#M39715</link>
      <description>&lt;P&gt;&lt;A href="https://community.databricks.com/t5/user/viewprofilepage/user-id/71565" target="_blank"&gt;@Ajay-Pandey&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;- Hi .. I want to check the&amp;nbsp;queries that are actively running and taking long time for execution.. please suggest how to check? Thanks.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2024 07:47:43 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/98413#M39715</guid>
      <dc:creator>Srini_ADB</dc:creator>
      <dc:date>2024-11-12T07:47:43Z</dc:date>
    </item>
    <item>
      <title>Re: how to use rest api to find long running query in databricks</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/98748#M39831</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/71565"&gt;@Ajay-Pandey&lt;/a&gt;&amp;nbsp;Thanks. This API works fine. But it is showing only the current day queries. How can we get the all queries which is currently running.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Nov 2024 07:45:03 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-use-rest-api-to-find-long-running-query-in-databricks/m-p/98748#M39831</guid>
      <dc:creator>Srini_ADB</dc:creator>
      <dc:date>2024-11-14T07:45:03Z</dc:date>
    </item>
  </channel>
</rss>

