-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(ingest): add classification to bigquery, redshift #10031
Merged
hsheth2
merged 2 commits into
datahub-project:master
from
mayurinehate:master+ing-547-classification-bigquery-redshift
Mar 14, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
metadata-ingestion/src/datahub/ingestion/source/bigquery_v2/bigquery_data_reader.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import logging | ||
from collections import defaultdict | ||
from typing import Dict, List, Optional | ||
|
||
from google.cloud import bigquery | ||
|
||
from datahub.ingestion.source.sql.data_reader import DataReader | ||
from datahub.utilities.perf_timer import PerfTimer | ||
|
||
logger = logging.Logger(__name__) | ||
|
||
|
||
class BigQueryDataReader(DataReader): | ||
@staticmethod | ||
def create( | ||
client: bigquery.Client, | ||
) -> "BigQueryDataReader": | ||
return BigQueryDataReader(client) | ||
|
||
def __init__( | ||
self, | ||
client: bigquery.Client, | ||
) -> None: | ||
self.client = client | ||
|
||
def get_sample_data_for_table( | ||
self, | ||
table_id: List[str], | ||
sample_size: int, | ||
*, | ||
sample_size_percent: Optional[float] = None, | ||
filter: Optional[str] = None, | ||
) -> Dict[str, list]: | ||
""" | ||
table_id should be in the form [project, dataset, schema] | ||
""" | ||
|
||
assert len(table_id) == 3 | ||
project = table_id[0] | ||
dataset = table_id[1] | ||
table_name = table_id[2] | ||
|
||
column_values: Dict[str, list] = defaultdict(list) | ||
if sample_size_percent is None: | ||
return column_values | ||
# Ideally we always know the actual row count. | ||
# The alternative to perform limit query scans entire BQ table | ||
# and is never a recommended option due to cost factor, unless | ||
# additional filter clause (e.g. where condition on partition) is available. | ||
|
||
logger.debug( | ||
f"Collecting sample values for table {project}.{dataset}.{table_name}" | ||
) | ||
with PerfTimer() as timer: | ||
sample_pc = sample_size_percent * 100 | ||
# TODO: handle for sharded+compulsory partitioned tables | ||
sql = ( | ||
f"SELECT * FROM `{project}.{dataset}.{table_name}` " | ||
+ f"TABLESAMPLE SYSTEM ({sample_pc:.8f} percent)" | ||
) | ||
# Ref: https://cloud.google.com/bigquery/docs/samples/bigquery-query-results-dataframe | ||
df = self.client.query_and_wait(sql).to_dataframe() | ||
time_taken = timer.elapsed_seconds() | ||
logger.debug( | ||
f"Finished collecting sample values for table {project}.{dataset}.{table_name};" | ||
f"{df.shape[0]} rows; took {time_taken:.3f} seconds" | ||
) | ||
|
||
return df.to_dict(orient="list") | ||
|
||
def close(self) -> None: | ||
self.client.close() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we extract out this 1.2 constant in a follow up PR?