Skip to content

Amazon Athena

Import AthenaClient from rboto. Install the service package following Getting started. Configure the client for your workgroup's region.

Discover workgroups

Workgroups select query settings, including where Athena writes results. Iterate over pages with the typed paginator:

import asyncio

from rboto import AthenaClient


async def main() -> None:
    client = AthenaClient(region="us-east-1")
    async for page in client.paginators.list_work_groups(max_results=10):
        for workgroup in page.work_groups or []:
            print(workgroup.name, workgroup.state)


if __name__ == "__main__":
    asyncio.run(main())

Submit a query and retrieve results

start_query_execution returns a query execution ID. Awaiting this method waits for the submission response; the SQL query continues running in Athena. Poll get_query_execution until it succeeds, fails, or is cancelled, then retrieve results with get_query_results or its paginator.

The following example runs a query without an input table. Set ATHENA_WORKGROUP to an existing workgroup configured with an S3 results location, and set AWS_REGION to its region. The configured credentials need Athena permissions and access to that results bucket. AWS_PROFILE selects a named credential profile.

import asyncio
import os

from rboto import AthenaClient


async def main() -> None:
    client = AthenaClient(region=os.environ.get("AWS_REGION", "us-east-1"))
    started = await client.start_query_execution(
        query_string="SELECT n FROM UNNEST(sequence(1, 12)) AS t(n) ORDER BY n",
        work_group=os.environ["ATHENA_WORKGROUP"],
    )
    query_id = started.query_execution_id
    assert query_id is not None
    try:
        async with asyncio.timeout(60):
            while True:
                result = await client.get_query_execution(query_execution_id=query_id)
                assert result.query_execution is not None
                status = result.query_execution.status
                assert status is not None
                if status.state == "SUCCEEDED":
                    break
                if status.state in {"FAILED", "CANCELLED"}:
                    raise RuntimeError(status.state_change_reason or "Athena query failed")
                await asyncio.sleep(0.5)

        async for page in client.paginators.get_query_results(
            query_execution_id=query_id, max_results=3,
        ):
            assert page.result_set is not None
            for row in page.result_set.rows or []:
                print([cell.var_char_value for cell in row.data or []])
    finally:
        await client.stop_query_execution(query_execution_id=query_id)


if __name__ == "__main__":
    asyncio.run(main())

Query results are native objects: page.result_set.rows contains rows, and each row's data contains cells. var_char_value is a string or None for a null cell; SQL column types are available in result_set_metadata.column_info. SELECT results include a header row. Pagination returns native response pages and keeps the continuation token internally.

The example bounds polling with a timeout and stops the query in finally. The pinned Athena model does not define waiters, so this client has no generated waiter namespace.

Modeled errors

API failures use generated exception classes, such as rboto_athena.exceptions.InvalidRequestError. A query that fails after submission reports its failure through query_execution.status; inspect state and state_change_reason when polling.

See Configuration, Pagination, and Errors for shared client behavior.

Page sizes and Spark engines

Page-size limits vary by operation. list_tags_for_resource requires max_results >= 75, while list_engine_versions accepts at most 10. Omit max_results to use the service default. Invalid values raise a modeled InvalidRequestError.

Spark features depend on the workgroup engine. The legacy PySpark engine version 3 rejects monitoring_configuration on start_session, although this parameter appears in the shared API model for other engines. Check the workgroup's engine before enabling engine-specific settings.

Cancellation states use different spellings: SQL query executions return CANCELLED, while Spark calculations return CANCELING and CANCELED. Poll the appropriate terminal state after a stop request. Explicitly terminate Spark sessions during teardown so their compute resources are released.