Created
March 25, 2026 19:37
-
-
Save ducnh1022/9972ae70b31f81d7fa31b6c23ab634f3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def get_table_info(project_id, dataset_id, table_id, sample_size=5): | |
| table_ref = f"{project_id}.{dataset_id}.{table_id}" | |
| # Get schema | |
| table = client.get_table(table_ref) | |
| schema = table.schema | |
| # Get sample data | |
| query = f""" | |
| SELECT * | |
| FROM `{table_ref}` | |
| LIMIT {sample_size} | |
| """ | |
| rows = client.query(query).result() | |
| sample_rows = [dict(row.items()) for row in rows] | |
| # Build result | |
| result = {} | |
| for field in schema: | |
| col_name = field.name | |
| col_type = field.field_type | |
| # Extract sample values for this column | |
| samples = [row.get(col_name) for row in sample_rows] | |
| result[col_name] = { | |
| "type": col_type, | |
| "samples": samples | |
| } | |
| return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment