Amazon Timestream for LiveAnalytics will no longer be open to new customers starting June 20, 2025. If you would like to use Amazon Timestream for LiveAnalytics, sign up prior to that date. Existing customers can continue to use the service as normal. For more information, see Amazon Timestream for LiveAnalytics availability change.
Cancel query
You can use the following code snippets to cancel a query.
Note
These code snippets are based on full sample applications on GitHub
- Java
-
public void cancelQuery() { System.out.println("Starting query: " + SELECT_ALL_QUERY); QueryRequest queryRequest = new QueryRequest(); queryRequest.setQueryString(SELECT_ALL_QUERY); QueryResult queryResult = queryClient.query(queryRequest); System.out.println("Cancelling the query: " + SELECT_ALL_QUERY); final CancelQueryRequest cancelQueryRequest = new CancelQueryRequest(); cancelQueryRequest.setQueryId(queryResult.getQueryId()); try { queryClient.cancelQuery(cancelQueryRequest); System.out.println("Query has been successfully cancelled"); } catch (Exception e) { System.out.println("Could not cancel the query: " + SELECT_ALL_QUERY + " = " + e); } }
- Java v2
-
public void cancelQuery() { System.out.println("Starting query: " + SELECT_ALL_QUERY); QueryRequest queryRequest = QueryRequest.builder().queryString(SELECT_ALL_QUERY).build(); QueryResponse queryResponse = timestreamQueryClient.query(queryRequest); System.out.println("Cancelling the query: " + SELECT_ALL_QUERY); final CancelQueryRequest cancelQueryRequest = CancelQueryRequest.builder() .queryId(queryResponse.queryId()).build(); try { timestreamQueryClient.cancelQuery(cancelQueryRequest); System.out.println("Query has been successfully cancelled"); } catch (Exception e) { System.out.println("Could not cancel the query: " + SELECT_ALL_QUERY + " = " + e); } }
- Go
-
cancelQueryInput := ×treamquery.CancelQueryInput{ QueryId: aws.String(*queryOutput.QueryId), } fmt.Println("Submitting cancellation for the query") fmt.Println(cancelQueryInput) // submit the query cancelQueryOutput, err := querySvc.CancelQuery(cancelQueryInput) if err != nil { fmt.Println("Error:") fmt.Println(err) } else { fmt.Println("Query has been cancelled successfully") fmt.Println(cancelQueryOutput) }
- Python
-
def cancel_query(self): print("Starting query: " + self.SELECT_ALL) result = self.client.query(QueryString=self.SELECT_ALL) print("Cancelling query: " + self.SELECT_ALL) try: self.client.cancel_query(QueryId=result['QueryId']) print("Query has been successfully cancelled") except Exception as err: print("Cancelling query failed:", err)
- Node.js
-
The following snippet uses the AWS SDK for JavaScript V2 style. It is based on the sample application at Node.js sample Amazon Timestream for LiveAnalytics application on GitHub
. async function tryCancelQuery() { const params = { QueryString: SELECT_ALL_QUERY }; console.log(`Running query: ${SELECT_ALL_QUERY}`); await queryClient.query(params).promise() .then( async (response) => { await cancelQuery(response.QueryId); }, (err) => { console.error("Error while executing select all query:", err); }); } async function cancelQuery(queryId) { const cancelParams = { QueryId: queryId }; console.log(`Sending cancellation for query: ${SELECT_ALL_QUERY}`); await queryClient.cancelQuery(cancelParams).promise() .then( (response) => { console.log("Query has been cancelled successfully"); }, (err) => { console.error("Error while cancelling select all:", err); }); }
- .NET
-
public async Task CancelQuery() { Console.WriteLine("Starting query: " + SELECT_ALL_QUERY); QueryRequest queryRequest = new QueryRequest(); queryRequest.QueryString = SELECT_ALL_QUERY; QueryResponse queryResponse = await queryClient.QueryAsync(queryRequest); Console.WriteLine("Cancelling query: " + SELECT_ALL_QUERY); CancelQueryRequest cancelQueryRequest = new CancelQueryRequest(); cancelQueryRequest.QueryId = queryResponse.QueryId; try { await queryClient.CancelQueryAsync(cancelQueryRequest); Console.WriteLine("Query has been successfully cancelled."); } catch(Exception e) { Console.WriteLine("Could not cancel the query: " + SELECT_ALL_QUERY + " = " + e); } }
Run UNLOAD query
Create batch load task