You are on page 1of 1

import boto3

# Create a session using your AWS credentials


session = boto3.Session(
aws_access_key_id='YOUR_ACCESS_KEY',
aws_secret_access_key='YOUR_SECRET_KEY',
region_name='YOUR_REGION'
)

# Create a client for Athena


athena_client = session.client('athena')

# Set the default workgroup (replace 'your-workgroup' with your actual workgroup name)
athena_client.start_query_execution(
QueryString='USE workgroup your-workgroup',
ResultConfiguration={
'OutputLocation': 's3://your-bucket/results/'
}
)

# Define the query to list all tables


query = "SHOW TABLES IN your_database"

# Execute the query


response = athena_client.start_query_execution(
QueryString=query,
QueryExecutionContext={
'Database': 'your_database'
},
ResultConfiguration={
'OutputLocation': 's3://your-bucket/results/'
}
)

# Get the query execution ID


query_execution_id = response['QueryExecutionId']

# Wait for the query to complete


athena_client.get_waiter('query_execution_completed').wait(
QueryExecutionId=query_execution_id
)

# Get the results of the query


results = athena_client.get_query_results(
QueryExecutionId=query_execution_id
)

# Extract table names from the results


table_names = [row['Data'][0]['VarCharValue'] for row in results['ResultSet']['Rows'][1:]]

# Print the table names


for table_name in table_names:
print(table_name)

You might also like