Search code examples
pythonamazon-web-servicesamazon-athenapyathena

running aws athena query via pyathena


This query works fine in Athena's front-end:

SELECT * FROM analysisdata."iris" limit 10;

I am using this Python code to run the above query via Python/pyathena

from pyathena import connect

cursor = connect(aws_access_key_id='AKI.DELETED.2Q',
                 aws_secret_access_key='D.DELETED.Al',
                 s3_staging_dir='s3://Bla887342ac-a3ce-4600-94d0-9092f4a6bd20/Iris',
                 region_name='eu-west-1').cursor()

cursor.execute("""SELECT * FROM analysisdata.""iris"" limit 10;""")

print(cursor.description)
print(cursor.fetchall())

Unfortunately I get:

pyathena.error.DatabaseError: An error occurred (InvalidRequestException) when calling the StartQueryExecution operation: line 1:27: mismatched input '.' expecting {<EOF>, ',', 'WHERE', 'GROUP', 'ORDER', 'HAVING', 'LIMIT', 'APPROXIMATE', 'JOIN', 'CROSS', 'INNER', 'LEFT', 'RIGHT', 'FULL', 'NATURAL', 'UNION', 'EXCEPT', 'INTERSECT'}

I presume is my introduction of:

""" and ""

Any ideas?

PS:

I tried:

cursor.execute("SELECT * FROM analysisdata.""iris"" limit 10;")

get:

pyathena.error.OperationalError: com.facebook.presto.hive.DataCatalogException: Namespace analysisdata not found. Please check your query.

Why does facebook appear in this?

I also tried:

cursor.execute("SELECT * FROM analysisdata.iris limit 10;")

same error message ...


Solution

  • As it appears, your catalog in eu-west-1 does not contain analysisdata db.

    You have to specify the region in which you created the database/table in Athena Console, rather than the specified S3 bucket region.

    To solve that, log into the console, and verify which region contains your database/table.