Skip to content

Commit b28c8fa

Browse files
authored
Merge pull request #2045 from myouju/master
Added 'Use Glue Data Catalog' options in Athena
2 parents 41ec4c8 + 6522325 commit b28c8fa

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

redash/query_runner/athena.py

+28
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import logging
33
import os
4+
import boto3
45

56
from redash.query_runner import *
67
from redash.settings import parse_boolean
@@ -74,6 +75,10 @@ def configuration_schema(cls):
7475
'title': 'Schema Name',
7576
'default': 'default'
7677
},
78+
'glue': {
79+
'type': 'boolean',
80+
'title': 'Use Glue Data Catalog',
81+
},
7782
},
7883
'required': ['region', 's3_staging_dir'],
7984
'order': ['region', 'aws_access_key', 'aws_secret_key', 's3_staging_dir', 'schema'],
@@ -112,7 +117,30 @@ def type(cls):
112117
def __init__(self, configuration):
113118
super(Athena, self).__init__(configuration)
114119

120+
def __get_schema_from_glue(self):
121+
client = boto3.client(
122+
'glue',
123+
aws_access_key_id=self.configuration.get('aws_access_key', None),
124+
aws_secret_access_key=self.configuration.get('aws_secret_key', None),
125+
region_name=self.configuration['region']
126+
)
127+
schema = {}
128+
129+
for database in client.get_databases()['DatabaseList']:
130+
for table in client.get_tables(DatabaseName=database['Name'])['TableList']:
131+
table_name = '%s.%s' % (database['Name'], table['Name'])
132+
if table_name not in schema:
133+
column = [columns['Name'] for columns in table['StorageDescriptor']['Columns']]
134+
schema[table_name] = {'name': table_name, 'columns': column}
135+
for partition in table['PartitionKeys']:
136+
schema[table_name]['columns'].append(partition['Name'])
137+
138+
return schema.values()
139+
115140
def get_schema(self, get_stats=False):
141+
if self.configuration.get('glue', False):
142+
return self.__get_schema_from_glue()
143+
116144
schema = {}
117145
query = """
118146
SELECT table_schema, table_name, column_name

0 commit comments

Comments
 (0)