Skip to content

Commit d4bb6a5

Browse files
committed
add possibility to provide cred via filename
1 parent d4fb0bb commit d4bb6a5

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

dask_bigquery/core.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
from google.api_core import client_info as rest_client_info
1313
from google.api_core.gapic_v1 import client_info as grpc_client_info
1414
from google.cloud import bigquery, bigquery_storage
15+
from google.oauth2 import service_account
1516

1617
import dask_bigquery
1718

1819

1920
@contextmanager
20-
def bigquery_clients(project_id):
21+
def bigquery_clients(project_id, cred_fpath):
2122
"""This context manager is a temporary solution until there is an
2223
upstream solution to handle this.
2324
See googleapis/google-cloud-python#9457
@@ -30,7 +31,14 @@ def bigquery_clients(project_id):
3031
user_agent=f"dask-bigquery/{dask_bigquery.__version__}"
3132
)
3233

33-
with bigquery.Client(project_id, client_info=bq_client_info) as bq_client:
34+
if cred_fpath:
35+
credentials = service_account.Credentials.from_service_account_file(cred_fpath)
36+
else:
37+
credentials = cred_fpath # if no path set to None to try read default
38+
39+
with bigquery.Client(
40+
project_id, credentials=credentials, client_info=bq_client_info
41+
) as bq_client:
3442
bq_storage_client = bigquery_storage.BigQueryReadClient(
3543
credentials=bq_client._credentials,
3644
client_info=bqstorage_client_info,
@@ -53,6 +61,7 @@ def _stream_to_dfs(bqs_client, stream_name, schema, read_kwargs):
5361
def bigquery_read(
5462
make_create_read_session_request: callable,
5563
project_id: str,
64+
cred_fpath: str,
5665
read_kwargs: dict,
5766
stream_name: str,
5867
) -> pd.DataFrame:
@@ -71,7 +80,7 @@ def bigquery_read(
7180
NOTE: Please set if reading from Storage API without any `row_restriction`.
7281
https://cloud.google.com/bigquery/docs/reference/storage/rpc/google.cloud.bigquery.storage.v1beta1#stream
7382
"""
74-
with bigquery_clients(project_id) as (_, bqs_client):
83+
with bigquery_clients(project_id, cred_fpath) as (_, bqs_client):
7584
session = bqs_client.create_read_session(make_create_read_session_request())
7685
schema = pyarrow.ipc.read_schema(
7786
pyarrow.py_buffer(session.arrow_schema.serialized_schema)
@@ -89,6 +98,8 @@ def read_gbq(
8998
dataset_id: str,
9099
table_id: str,
91100
row_filter="",
101+
*,
102+
cred_fpath: str = None,
92103
read_kwargs: dict = None,
93104
):
94105
"""Read table as dask dataframe using BigQuery Storage API via Arrow format.
@@ -104,6 +115,8 @@ def read_gbq(
104115
BigQuery table within dataset
105116
row_filter: str
106117
SQL text filtering statement to pass to `row_restriction`
118+
cred_fpath: str
119+
path for the service account key json file.
107120
read_kwargs: dict
108121
kwargs to pass to read_rows()
109122
@@ -112,7 +125,7 @@ def read_gbq(
112125
Dask DataFrame
113126
"""
114127
read_kwargs = read_kwargs or {}
115-
with bigquery_clients(project_id) as (bq_client, bqs_client):
128+
with bigquery_clients(project_id, cred_fpath) as (bq_client, bqs_client):
116129
table_ref = bq_client.get_table(f"{dataset_id}.{table_id}")
117130
if table_ref.table_type == "VIEW":
118131
raise TypeError("Table type VIEW not supported")
@@ -157,6 +170,7 @@ def make_create_read_session_request(row_filter=""):
157170
bigquery_read,
158171
make_create_read_session_request,
159172
project_id,
173+
cred_fpath,
160174
read_kwargs,
161175
),
162176
label=label,

0 commit comments

Comments
 (0)