There are more AWS SDK examples available in the AWS Doc SDK Examples
Use GetQueryResults with an AWS SDK or CLI
The following code examples show how to use GetQueryResults.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code examples:
- CLI
-
- AWS CLI
-
To return the results of a query
The following
get-query-resultsexample returns the results of the query that has the specified query ID.aws athena get-query-results \ --query-execution-ida1b2c3d4-5678-90ab-cdef-EXAMPLE11111Output:
{ "ResultSet": { "Rows": [ { "Data": [ { "VarCharValue": "date" }, { "VarCharValue": "location" }, { "VarCharValue": "browser" }, { "VarCharValue": "uri" }, { "VarCharValue": "status" } ] }, { "Data": [ { "VarCharValue": "2014-07-05" }, { "VarCharValue": "SFO4" }, { "VarCharValue": "Safari" }, { "VarCharValue": "/test-image-2.jpeg" }, { "VarCharValue": "200" } ] }, { "Data": [ { "VarCharValue": "2014-07-05" }, { "VarCharValue": "SFO4" }, { "VarCharValue": "Opera" }, { "VarCharValue": "/test-image-2.jpeg" }, { "VarCharValue": "200" } ] }, { "Data": [ { "VarCharValue": "2014-07-05" }, { "VarCharValue": "SFO4" }, { "VarCharValue": "Firefox" }, { "VarCharValue": "/test-image-3.jpeg" }, { "VarCharValue": "200" } ] }, { "Data": [ { "VarCharValue": "2014-07-05" }, { "VarCharValue": "SFO4" }, { "VarCharValue": "Lynx" }, { "VarCharValue": "/test-image-3.jpeg" }, { "VarCharValue": "200" } ] }, { "Data": [ { "VarCharValue": "2014-07-05" }, { "VarCharValue": "SFO4" }, { "VarCharValue": "IE" }, { "VarCharValue": "/test-image-2.jpeg" }, { "VarCharValue": "200" } ] }, { "Data": [ { "VarCharValue": "2014-07-05" }, { "VarCharValue": "SFO4" }, { "VarCharValue": "Opera" }, { "VarCharValue": "/test-image-1.jpeg" }, { "VarCharValue": "200" } ] }, { "Data": [ { "VarCharValue": "2014-07-05" }, { "VarCharValue": "SFO4" }, { "VarCharValue": "Chrome" }, { "VarCharValue": "/test-image-3.jpeg" }, { "VarCharValue": "200" } ] }, { "Data": [ { "VarCharValue": "2014-07-05" }, { "VarCharValue": "SFO4" }, { "VarCharValue": "Firefox" }, { "VarCharValue": "/test-image-2.jpeg" }, { "VarCharValue": "200" } ] }, { "Data": [ { "VarCharValue": "2014-07-05" }, { "VarCharValue": "SFO4" }, { "VarCharValue": "Chrome" }, { "VarCharValue": "/test-image-3.jpeg" }, { "VarCharValue": "200" } ] }, { "Data": [ { "VarCharValue": "2014-07-05" }, { "VarCharValue": "SFO4" }, { "VarCharValue": "IE" }, { "VarCharValue": "/test-image-2.jpeg" }, { "VarCharValue": "200" } ] } ], "ResultSetMetadata": { "ColumnInfo": [ { "CatalogName": "hive", "SchemaName": "", "TableName": "", "Name": "date", "Label": "date", "Type": "date", "Precision": 0, "Scale": 0, "Nullable": "UNKNOWN", "CaseSensitive": false }, { "CatalogName": "hive", "SchemaName": "", "TableName": "", "Name": "location", "Label": "location", "Type": "varchar", "Precision": 2147483647, "Data": [ "Scale": 0, "Nullable": "UNKNOWN", "CaseSensitive": true }, { "CatalogName": "hive", "SchemaName": "", "TableName": "", "Name": "browser", "Label": "browser", "Type": "varchar", "Precision": 2147483647, "Scale": 0, "Nullable": "UNKNOWN", "CaseSensitive": true }, { "CatalogName": "hive", "SchemaName": "", "TableName": "", "Name": "uri", "Label": "uri", "Type": "varchar", "Precision": 2147483647, "Scale": 0, "Nullable": "UNKNOWN", "CaseSensitive": true }, { "CatalogName": "hive", "SchemaName": "", "TableName": "", "Name": "status", "Label": "status", "Type": "integer", "Precision": 10, "Scale": 0, "Nullable": "UNKNOWN", "CaseSensitive": false } ] } }, "UpdateCount": 0 }For more information, see Working with Query Results, Output Files, and Query History in the Amazon Athena User Guide.
-
For API details, see GetQueryResults
in AWS CLI Command Reference.
-
- Python
-
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. def get_query_results(self, query_execution_id: str) -> Dict[str, Any]: """ Retrieves the results of a completed query execution using pagination. :param query_execution_id: The unique ID of the query execution. :return: A dictionary with 'columns' (list of column names) and 'rows' (list of lists of string values). :raises ClientError: If the query results could not be retrieved. """ try: paginator = self.athena_client.get_paginator("get_query_results") page_iterator = paginator.paginate(QueryExecutionId=query_execution_id) columns = list() rows = list() first_page = True for page in page_iterator: result_set = page.get("ResultSet", dict()) # Extract column names from metadata on first page if first_page: column_info = result_set.get("ResultSetMetadata", dict()).get( "ColumnInfo", list() ) columns = [col["Name"] for col in column_info] first_page = False page_rows = result_set.get("Rows", list()) for row in page_rows: data = row.get("Data", list()) row_values = [datum.get("VarCharValue", "") for datum in data] rows.append(row_values) # Athena always returns the column header as the first row of the # result set for SELECT-style queries. Drop it unconditionally when # there are named columns and at least one row, rather than # comparing values (a data row could coincidentally match the # header names). if columns and rows: rows = rows[1:] logger.info( "Retrieved %d result rows for query '%s'.", len(rows), query_execution_id, ) return {"columns": columns, "rows": rows} except ClientError as err: if err.response["Error"]["Code"] == "InvalidRequestException": logger.error( "Invalid request retrieving results for query '%s'. " "The execution ID is invalid or the query has not completed. " "%s: %s", query_execution_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise-
For API details, see GetQueryResults in AWS SDK for Python (Boto3) API Reference.
-