View a markdown version of this page

Use GetNamedQuery with an AWS SDK or CLI - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use GetNamedQuery with an AWS SDK or CLI

The following code examples show how to use GetNamedQuery.

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 a named query

The following get-named-query example returns information about the query that has the specified ID.

aws athena get-named-query \ --named-query-id a1b2c3d4-5678-90ab-cdef-EXAMPLE11111

Output:

{ "NamedQuery": { "Name": "CloudFront Logs - SFO", "Description": "Shows successful GET request data for SFO", "Database": "default", "QueryString": "select date, location, browser, uri, status from cloudfront_logs where method = 'GET' and status = 200 and location like 'SFO%' limit 10", "NamedQueryId": "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "WorkGroup": "AthenaAdmin" } }

For more information, see Running SQL Queries Using Amazon Athena in the Amazon Athena User Guide.

  • For API details, see GetNamedQuery 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_named_query(self, named_query_id: str) -> Dict[str, Any]: """ Returns the details of a single named (saved) query, including its SQL query string. :param named_query_id: The unique ID of the named query. :return: A dictionary containing the named query details. :raises ClientError: If the named query could not be retrieved. """ try: response = self.athena_client.get_named_query(NamedQueryId=named_query_id) named_query = response["NamedQuery"] logger.info("Retrieved named query '%s'.", named_query_id) return named_query except ClientError as err: if err.response["Error"]["Code"] == "InvalidRequestException": logger.error( "Invalid request retrieving named query '%s'. " "The named query ID was not found or is invalid. %s: %s", named_query_id, err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • For API details, see GetNamedQuery in AWS SDK for Python (Boto3) API Reference.