How to Run ClickHouse Export Queries Asynchronously Without Waiting for Results #92522
-
|
Hey, I wonder if someone know a good way of achieving the following. We are copying data from ClickHouse to GCS with GCS function. Sometimes these queries run for a while. We trigger the queries from processes running on preemtible nodes, so basically the process can be killed whenever. So if we sit and wait for the query to finish and the process is shutdown, the query is cancelled and we would need to retry the whole query. Hence, I would like to do these "exports" asynchronous instead of waiting as I anyway don't expect anything more than 👍 or 👎 for the query. The workflow would be something like this:
Good to know info:
I haven't really understood how (or if) I can trigger a query and then just not wait for the result. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Gemini suggested the following test, but it was blocking |
Beta Was this translation helpful? Give feedback.
-
|
Found this Ran the following and cancelled the request, which worked: Verified by running the following query: Made an implementation in Golang which works as well: func main() {
client := &http.Client{}
params := url.Values{}
id := runID()
queryID := fmt.Sprintf("fire_forget_test_%s", id)
params.Add("query_id", queryID)
query := "SELECT sleep(10) Format Null"
endpoint := fmt.Sprintf("http://localhost:8123/?%s&query=%s", params.Encode(), url.QueryEscape(query))
t.Logf("Running query: %s", query)
ctx, cancelFunc := context.WithCancel(context.Background())
req, _ := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
go func() {
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
}
resp.Body.Close()
}()
for {
time.Sleep(1 * time.Second)
q := "SELECT COUNT(1) FROM system.processes WHERE query_id = '" + queryID + "'"
e := fmt.Sprintf("http://localhost:8123/?query=%s", url.QueryEscape(q))
r, _ := http.NewRequest("GET", e, nil)
resp, _ := client.Do(r)
if resp.StatusCode == 200 {
fmt.Println("Query is running.")
cancelFunc()
break
}
resp.Body.Close()
}
fmt.Println("Success! The Go process is now exiting, but the query should still be running.")
} |
Beta Was this translation helpful? Give feedback.
Found this
Ran the following and cancelled the request, which worked:
Verified by running the following query: