|
| 1 | +import logging |
| 2 | +from collections import namedtuple |
| 3 | + |
| 4 | +import google.oauth2.service_account |
| 5 | +import google_auth_httplib2 |
| 6 | +from googleapiclient.discovery import build |
| 7 | +import httplib2 |
| 8 | +from pyhocon import ConfigTree # noqa: F401 |
| 9 | +from typing import List, Any # noqa: F401 |
| 10 | + |
| 11 | +from databuilder.extractor.base_extractor import Extractor |
| 12 | +from databuilder.models.table_metadata import TableMetadata, ColumnMetadata |
| 13 | + |
| 14 | + |
| 15 | +DatasetRef = namedtuple('DatasetRef', ['datasetId', 'projectId']) |
| 16 | +TableKey = namedtuple('TableKey', ['schema_name', 'table_name']) |
| 17 | + |
| 18 | +LOGGER = logging.getLogger(__name__) |
| 19 | + |
| 20 | + |
| 21 | +class BigQueryMetadataExtractor(Extractor): |
| 22 | + |
| 23 | + """ A metadata extractor for bigquery tables, taking the schema metadata |
| 24 | + from the google cloud bigquery API's. This extractor goes through all visible |
| 25 | + datasets in the project identified by project_id and iterates over all tables |
| 26 | + it finds. A separate account is configurable through the key_path parameter, |
| 27 | + which should point to a valid json file corresponding to a service account. |
| 28 | +
|
| 29 | + This extractor supports nested columns, which are delimited by a dot (.) in the |
| 30 | + column name. |
| 31 | + """ |
| 32 | + |
| 33 | + PROJECT_ID_KEY = 'project_id' |
| 34 | + KEY_PATH_KEY = 'key_path' |
| 35 | + PAGE_SIZE_KEY = 'page_size' |
| 36 | + FILTER_KEY = 'filter' |
| 37 | + _DEFAULT_SCOPES = ('https://www.googleapis.com/auth/bigquery.readonly') |
| 38 | + DEFAULT_PAGE_SIZE = 300 |
| 39 | + NUM_RETRIES = 3 |
| 40 | + |
| 41 | + def init(self, conf): |
| 42 | + # type: (ConfigTree) -> None |
| 43 | + self.key_path = conf.get_string(BigQueryMetadataExtractor.KEY_PATH_KEY, None) |
| 44 | + self.project_id = conf.get_string(BigQueryMetadataExtractor.PROJECT_ID_KEY) |
| 45 | + self.pagesize = conf.get_int( |
| 46 | + BigQueryMetadataExtractor.PAGE_SIZE_KEY, |
| 47 | + BigQueryMetadataExtractor.DEFAULT_PAGE_SIZE) |
| 48 | + self.filter = conf.get_string(BigQueryMetadataExtractor.FILTER_KEY, '') |
| 49 | + |
| 50 | + if self.key_path: |
| 51 | + credentials = ( |
| 52 | + google.oauth2.service_account.Credentials.from_service_account_file( |
| 53 | + self.key_path, scopes=BigQueryMetadataExtractor._DEFAULT_SCOPES)) |
| 54 | + else: |
| 55 | + credentials, _ = google.auth.default(scopes=BigQueryMetadataExtractor._DEFAULT_SCOPES) |
| 56 | + |
| 57 | + http = httplib2.Http() |
| 58 | + authed_http = google_auth_httplib2.AuthorizedHttp(credentials, http=http) |
| 59 | + self.bigquery_service = build('bigquery', 'v2', http=authed_http, cache_discovery=False) |
| 60 | + self.datasets = self._retrieve_datasets() |
| 61 | + self.iter = iter(self._iterate_over_tables()) |
| 62 | + |
| 63 | + def extract(self): |
| 64 | + # type: () -> Any |
| 65 | + try: |
| 66 | + return next(self.iter) |
| 67 | + except StopIteration: |
| 68 | + return None |
| 69 | + |
| 70 | + def _iterate_over_tables(self): |
| 71 | + # type: () -> Any |
| 72 | + for dataset in self.datasets: |
| 73 | + for entry in self._retrieve_tables(dataset): |
| 74 | + yield(entry) |
| 75 | + |
| 76 | + def _retrieve_datasets(self): |
| 77 | + # type: () -> List[DatasetRef] |
| 78 | + datasets = [] |
| 79 | + for page in self._page_dataset_list_results(): |
| 80 | + if 'datasets' not in page: |
| 81 | + continue |
| 82 | + |
| 83 | + for dataset in page['datasets']: |
| 84 | + dataset_ref = dataset['datasetReference'] |
| 85 | + ref = DatasetRef(**dataset_ref) |
| 86 | + datasets.append(ref) |
| 87 | + |
| 88 | + return datasets |
| 89 | + |
| 90 | + def _page_dataset_list_results(self): |
| 91 | + # type: () -> Any |
| 92 | + response = self.bigquery_service.datasets().list( |
| 93 | + projectId=self.project_id, |
| 94 | + all=False, # Do not return hidden datasets |
| 95 | + filter=self.filter, |
| 96 | + maxResults=self.pagesize).execute( |
| 97 | + num_retries=BigQueryMetadataExtractor.NUM_RETRIES) |
| 98 | + |
| 99 | + while response: |
| 100 | + yield response |
| 101 | + |
| 102 | + if 'nextPageToken' in response: |
| 103 | + response = self.bigquery_service.datasets().list( |
| 104 | + projectId=self.project_id, |
| 105 | + all=True, |
| 106 | + filter=self.filter, |
| 107 | + pageToken=response['nextPageToken']).execute( |
| 108 | + num_retries=BigQueryMetadataExtractor.NUM_RETRIES) |
| 109 | + else: |
| 110 | + response = None |
| 111 | + |
| 112 | + def _retrieve_tables(self, dataset): |
| 113 | + # type: () -> Any |
| 114 | + for page in self._page_table_list_results(dataset): |
| 115 | + if 'tables' not in page: |
| 116 | + continue |
| 117 | + |
| 118 | + for table in page['tables']: |
| 119 | + tableRef = table['tableReference'] |
| 120 | + table = self.bigquery_service.tables().get( |
| 121 | + projectId=tableRef['projectId'], |
| 122 | + datasetId=tableRef['datasetId'], |
| 123 | + tableId=tableRef['tableId']).execute(num_retries=BigQueryMetadataExtractor.NUM_RETRIES) |
| 124 | + |
| 125 | + # BigQuery tables also have interesting metadata about partitioning |
| 126 | + # data location (EU/US), mod/create time, etc... Extract that some other time? |
| 127 | + schema = table['schema'] |
| 128 | + cols = [] |
| 129 | + if 'fields' in schema: |
| 130 | + total_cols = 0 |
| 131 | + for column in schema['fields']: |
| 132 | + total_cols = self._iterate_over_cols('', column, cols, total_cols + 1) |
| 133 | + |
| 134 | + table_meta = TableMetadata( |
| 135 | + database='bigquery', |
| 136 | + cluster=tableRef['projectId'], |
| 137 | + schema_name=tableRef['datasetId'], |
| 138 | + name=tableRef['tableId'], |
| 139 | + description=table.get('description', ''), |
| 140 | + columns=cols, |
| 141 | + is_view=table['type'] == 'VIEW') |
| 142 | + |
| 143 | + yield(table_meta) |
| 144 | + |
| 145 | + def _iterate_over_cols(self, parent, column, cols, total_cols): |
| 146 | + # type: (str, str, List[ColumnMetadata()], int) -> int |
| 147 | + if len(parent) > 0: |
| 148 | + col_name = '{parent}.{field}'.format(parent=parent, field=column['name']) |
| 149 | + else: |
| 150 | + col_name = column['name'] |
| 151 | + |
| 152 | + if column['type'] == 'RECORD': |
| 153 | + col = ColumnMetadata( |
| 154 | + name=col_name, |
| 155 | + description=column.get('description', ''), |
| 156 | + col_type=column['type'], |
| 157 | + sort_order=total_cols) |
| 158 | + cols.append(col) |
| 159 | + total_cols += 1 |
| 160 | + for field in column['fields']: |
| 161 | + total_cols = self._iterate_over_cols(col_name, field, cols, total_cols) |
| 162 | + else: |
| 163 | + col = ColumnMetadata( |
| 164 | + name=col_name, |
| 165 | + description=column.get('description', ''), |
| 166 | + col_type=column['type'], |
| 167 | + sort_order=total_cols) |
| 168 | + cols.append(col) |
| 169 | + return total_cols + 1 |
| 170 | + |
| 171 | + def _page_table_list_results(self, dataset): |
| 172 | + # type: (DatasetRef) -> Any |
| 173 | + response = self.bigquery_service.tables().list( |
| 174 | + projectId=dataset.projectId, |
| 175 | + datasetId=dataset.datasetId, |
| 176 | + maxResults=self.pagesize).execute( |
| 177 | + num_retries=BigQueryMetadataExtractor.NUM_RETRIES) |
| 178 | + |
| 179 | + while response: |
| 180 | + yield response |
| 181 | + |
| 182 | + if 'nextPageToken' in response: |
| 183 | + response = self.bigquery_service.datasets().list( |
| 184 | + projectId=dataset.projectId, |
| 185 | + datasetId=dataset.datasetId, |
| 186 | + maxRResults=self.pagesize, |
| 187 | + pageToken=response['nextPageToken']).execute( |
| 188 | + num_retries=BigQueryMetadataExtractor.NUM_RETRIES) |
| 189 | + else: |
| 190 | + response = None |
| 191 | + |
| 192 | + def get_scope(self): |
| 193 | + # type: () -> str |
| 194 | + return 'extractor.bigquery_table_metadata' |
0 commit comments