Last active
June 22, 2022 04:27
-
-
Save antoniocachuan/56cc38dfa9a501ddd9ebc91df66bb337 to your computer and use it in GitHub Desktop.
Example BigQuery Storage Read API
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
| from google.cloud.bigquery_storage import BigQueryReadClient | |
| from google.cloud.bigquery_storage import types | |
| from google.cloud import bigquery_storage | |
| import pandas | |
| import os | |
| os.environ["GOOGLE_APPLICATION_CREDENTIALS"] ='key.json' | |
| project_id_billing = 'YOUR-PROJECT'# A Project where you have biquery.readsession permission | |
| def read_table(): | |
| bqstorageclient = bigquery_storage.BigQueryReadClient() | |
| project_id = "bigquery-public-data" | |
| dataset_id = "google_trends" | |
| table_id = "international_top_rising_terms" | |
| table = f"projects/{project_id}/datasets/{dataset_id}/tables/{table_id}" | |
| read_options = types.ReadSession.TableReadOptions( | |
| selected_fields=["country_name", "region_name"] | |
| ) | |
| parent = "projects/{}".format(project_id_billing) | |
| requested_session = types.ReadSession( | |
| table=table, | |
| data_format=types.DataFormat.ARROW, | |
| read_options=read_options, | |
| ) | |
| read_session = bqstorageclient.create_read_session( | |
| parent=parent, | |
| read_session=requested_session, | |
| max_stream_count=4, | |
| ) | |
| stream = read_session.streams[3] #read every stream from 0 to 3 | |
| reader = bqstorageclient.read_rows(stream.name) | |
| frames = [] | |
| for message in reader.rows().pages: | |
| frames.append(message.to_dataframe()) | |
| dataframe = pandas.concat(frames) | |
| print(dataframe.head()) | |
| return dataframe | |
| read_table() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment