/
bigquery.py
650 lines (580 loc) · 27.6 KB
/
bigquery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
from pathlib import Path
from typing import List, Union
from google.cloud import bigquery
from google.cloud.exceptions import NotFound
from prefect.core import Task
from prefect.engine.signals import SUCCESS
from prefect.utilities.gcp import get_bigquery_client
from prefect.utilities.tasks import defaults_from_attrs
class BigQueryTask(Task):
"""
Task for executing queries against a Google BigQuery table and (optionally) returning
the results. Note that _all_ initialization settings can be provided / overwritten at runtime.
Args:
- query (str, optional): a string of the query to execute
- query_params (list[tuple], optional): a list of 3-tuples specifying BigQuery query
parameters; currently only scalar query parameters are supported. See [the Google
documentation](https://cloud.google.com/bigquery/docs/parameterized-queries#bigquery-query-params-python)
for more details on how both the query and the query parameters should be formatted
- project (str, optional): the project to initialize the BigQuery Client with; if not
provided, will default to the one inferred from your credentials
- location (str, optional): location of the dataset that will be queried; defaults to "US"
- dry_run_max_bytes (int, optional): if provided, the maximum number of bytes the query
is allowed to process; this will be determined by executing a dry run and raising a
`ValueError` if the maximum is exceeded
- dataset_dest (str, optional): the optional name of a destination dataset to write the
query results to, if you don't want them returned; if provided, `table_dest` must
also be provided
- table_dest (str, optional): the optional name of a destination table to write the
query results to, if you don't want them returned; if provided, `dataset_dest` must also be
provided
- to_dataframe (bool, optional): if provided, returns the results of the query as a pandas
dataframe instead of a list of `bigquery.table.Row` objects. Defaults to False
- job_config (dict, optional): an optional dictionary of job configuration parameters; note that
the parameters provided here must be pickleable (e.g., dataset references will be rejected)
- **kwargs (optional): additional kwargs to pass to the `Task` constructor
"""
def __init__(
self,
query: str = None,
query_params: List[tuple] = None, # 3-tuples
project: str = None,
location: str = "US",
dry_run_max_bytes: int = None,
dataset_dest: str = None,
table_dest: str = None,
to_dataframe: bool = False,
job_config: dict = None,
**kwargs,
):
self.query = query
self.query_params = query_params
self.project = project
self.location = location
self.dry_run_max_bytes = dry_run_max_bytes
self.dataset_dest = dataset_dest
self.table_dest = table_dest
self.to_dataframe = to_dataframe
self.job_config = job_config or {}
super().__init__(**kwargs)
@defaults_from_attrs(
"query",
"query_params",
"project",
"location",
"dry_run_max_bytes",
"dataset_dest",
"table_dest",
"to_dataframe",
"job_config",
)
def run(
self,
query: str = None,
query_params: List[tuple] = None,
project: str = None,
location: str = "US",
dry_run_max_bytes: int = None,
credentials: dict = None,
dataset_dest: str = None,
table_dest: str = None,
to_dataframe: bool = False,
job_config: dict = None,
):
"""
Run method for this Task. Invoked by _calling_ this Task within a Flow context, after
initialization.
Args:
- query (str, optional): a string of the query to execute
- query_params (list[tuple], optional): a list of 3-tuples specifying BigQuery
query parameters; currently only scalar query parameters are supported. See
[the Google
documentation](https://cloud.google.com/bigquery/docs/parameterized-queries#bigquery-query-params-python)
for more details on how both the query and the query parameters should be
formatted
- project (str, optional): the project to initialize the BigQuery Client with; if
not provided, will default to the one inferred from your credentials
- location (str, optional): location of the dataset that will be queried; defaults
to "US"
- dry_run_max_bytes (int, optional): if provided, the maximum number of bytes the
query is allowed to process; this will be determined by executing a dry run and
raising a `ValueError` if the maximum is exceeded
- credentials (dict, optional): a JSON document containing Google Cloud credentials.
You should provide these at runtime with an upstream Secret task. If not
provided, Prefect will first check `context` for `GCP_CREDENTIALS` and lastly
will use default Google client logic.
- dataset_dest (str, optional): the optional name of a destination dataset to write the
query results to, if you don't want them returned; if provided, `table_dest`
must also be provided
- table_dest (str, optional): the optional name of a destination table to write the
query results to, if you don't want them returned; if provided, `dataset_dest` must also
be provided
- to_dataframe (bool, optional): if provided, returns the results of the query as a pandas
dataframe instead of a list of `bigquery.table.Row` objects. Defaults to False
- job_config (dict, optional): an optional dictionary of job configuration parameters; note
that the parameters provided here must be pickleable (e.g., dataset references will be
rejected)
Raises:
- ValueError: if the `query` is `None`
- ValueError: if only one of `dataset_dest` / `table_dest` is provided
- ValueError: if the query will execeed `dry_run_max_bytes`
Returns:
- list: a fully populated list of Query results, with one item per row
"""
# check for any argument inconsistencies
if query is None:
raise ValueError("No query provided.")
if sum([dataset_dest is None, table_dest is None]) == 1:
raise ValueError(
"Both `dataset_dest` and `table_dest` must be provided if writing to a "
"destination table."
)
# create client
client = get_bigquery_client(project=project, credentials=credentials)
# setup jobconfig
job_config = bigquery.QueryJobConfig(**job_config)
if query_params is not None:
hydrated_params = [
bigquery.ScalarQueryParameter(*qp) for qp in query_params
]
job_config.query_parameters = hydrated_params
# perform dry_run if requested
if dry_run_max_bytes is not None:
old_info = dict(
dry_run=job_config.dry_run, use_query_cache=job_config.use_query_cache
)
job_config.dry_run = True
job_config.use_query_cache = False
self.logger.debug("Performing a dry run...")
query_job = client.query(query, location=location, job_config=job_config)
if query_job.total_bytes_processed > dry_run_max_bytes:
msg = (
"Query will process {0} bytes which is above the set maximum of {1} "
"for this task."
).format(query_job.total_bytes_processed, dry_run_max_bytes)
raise ValueError(msg)
job_config.dry_run = old_info["dry_run"]
job_config.use_query_cache = old_info["use_query_cache"]
# if writing to a destination table
if dataset_dest is not None:
table_ref = client.dataset(dataset_dest).table(table_dest)
job_config.destination = table_ref
query_job = client.query(query, location=location, job_config=job_config)
# if returning the results as a dataframe
if to_dataframe:
return query_job.result().to_dataframe()
# else if returning as a list of bigquery.table.Row objects (default)
else:
return list(query_job.result())
class BigQueryStreamingInsert(Task):
"""
Task for insert records in a Google BigQuery table via [the streaming
API](https://cloud.google.com/bigquery/streaming-data-into-bigquery). Note that all of
these settings can optionally be provided or overwritten at runtime.
Args:
- dataset_id (str, optional): the id of a destination dataset to write the
records to
- table (str, optional): the name of a destination table to write the
records to
- project (str, optional): the project to initialize the BigQuery Client with; if not
provided, will default to the one inferred from your credentials
- location (str, optional): location of the dataset that will be written to; defaults
to "US"
- **kwargs (optional): additional kwargs to pass to the `Task` constructor
"""
def __init__(
self,
dataset_id: str = None,
table: str = None,
project: str = None,
location: str = "US",
**kwargs,
):
self.dataset_id = dataset_id
self.table = table
self.project = project
self.location = location
super().__init__(**kwargs)
@defaults_from_attrs("dataset_id", "table", "project", "location")
def run(
self,
records: List[dict],
dataset_id: str = None,
table: str = None,
project: str = None,
location: str = "US",
credentials: dict = None,
**kwargs,
):
"""
Run method for this Task. Invoked by _calling_ this Task within a Flow context, after
initialization.
Args:
- records (list[dict]): the list of records to insert as rows into the BigQuery
table; each item in the list should be a dictionary whose keys correspond to
columns in the table
- dataset_id (str, optional): the id of a destination dataset to write the records
to; if not provided here, will default to the one provided at initialization
- table (str, optional): the name of a destination table to write the
records to; if not provided here, will default to the one provided at initialization
- project (str, optional): the project to initialize the BigQuery Client with; if
not provided, will default to the one inferred from your credentials
- location (str, optional): location of the dataset that will be written to;
defaults to "US"
- credentials (dict, optional): a JSON document containing Google Cloud
credentials. You should provide these at runtime with an upstream Secret task.
If not provided, Prefect will first check `context` for `GCP_CREDENTIALS` and
lastly will use default Google client logic.
- **kwargs (optional): additional kwargs to pass to the `insert_rows_json` method;
see the documentation here:
https://googleapis.github.io/google-cloud-python/latest/bigquery/generated/google.cloud.bigquery.client.Client.html
Raises:
- ValueError: if all required arguments haven't been provided
- ValueError: if any of the records result in errors
Returns:
- the response from `insert_rows_json`
"""
# check for any argument inconsistencies
if dataset_id is None or table is None:
raise ValueError("Both dataset_id and table must be provided.")
# create client
client = get_bigquery_client(project=project, credentials=credentials)
# get table reference
table_ref = client.dataset(dataset_id).table(table)
# stream data in
response = client.insert_rows_json(table=table_ref, json_rows=records, **kwargs)
errors = []
output = []
for row in response:
output.append(row)
if "errors" in row:
errors.append(row["errors"])
if errors:
raise ValueError(errors)
return output
class BigQueryLoadGoogleCloudStorage(Task):
"""
Task for insert records in a Google BigQuery table via a [load
job](https://cloud.google.com/bigquery/docs/loading-data). Note that all of these settings
can optionally be provided or overwritten at runtime.
Args:
- uri (str, optional): GCS path to load data from
- dataset_id (str, optional): the id of a destination dataset to write the
records to
- table (str, optional): the name of a destination table to write the
records to
- project (str, optional): the project to initialize the BigQuery Client with; if not
provided, will default to the one inferred from your credentials
- schema (List[bigquery.SchemaField], optional): the schema to use when creating the table
- location (str, optional): location of the dataset that will be queried; defaults to "US"
- **kwargs (optional): additional kwargs to pass to the `Task` constructor
"""
def __init__(
self,
uri: str = None,
dataset_id: str = None,
table: str = None,
project: str = None,
schema: List[bigquery.SchemaField] = None,
location: str = "US",
**kwargs,
):
self.uri = uri
self.dataset_id = dataset_id
self.table = table
self.project = project
self.schema = schema
self.location = location
super().__init__(**kwargs)
@defaults_from_attrs("uri", "dataset_id", "table", "project", "location")
def run(
self,
uri: str = None,
dataset_id: str = None,
table: str = None,
project: str = None,
schema: List[bigquery.SchemaField] = None,
location: str = "US",
credentials: dict = None,
**kwargs,
):
"""
Run method for this Task. Invoked by _calling_ this Task within a Flow context, after
initialization.
Args:
- uri (str, optional): GCS path to load data from
- dataset_id (str, optional): the id of a destination dataset to write the
records to; if not provided here, will default to the one provided at initialization
- table (str, optional): the name of a destination table to write the
records to; if not provided here, will default to the one provided at initialization
- project (str, optional): the project to initialize the BigQuery Client with; if
not provided, will default to the one inferred from your credentials
- schema (List[bigquery.SchemaField], optional): the schema to use when creating
the table
- location (str, optional): location of the dataset that will be written to;
defaults to "US"
- credentials (dict, optional): a JSON document containing Google Cloud
credentials. You should provide these at runtime with an upstream Secret task.
If not provided, Prefect will first check `context` for `GCP_CREDENTIALS` and
lastly will use default Google client logic.
- **kwargs (optional): additional kwargs to pass to the `bigquery.LoadJobConfig`;
see the documentation here:
https://googleapis.github.io/google-cloud-python/latest/bigquery/generated/google.cloud.bigquery.client.Client.html
Raises:
- ValueError: if all required arguments haven't been provided
- ValueError: if the load job results in an error
Returns:
- google.cloud.bigquery.job.LoadJob: the response from `load_table_from_uri`
"""
# check for any argument inconsistencies
if dataset_id is None or table is None:
raise ValueError("Both dataset_id and table must be provided.")
# create client
client = get_bigquery_client(project=project, credentials=credentials)
# get table reference
table_ref = client.dataset(dataset_id).table(table)
# load data
autodetect = kwargs.pop("autodetect", True)
job_config = bigquery.LoadJobConfig(autodetect=autodetect, **kwargs)
if schema:
job_config.schema = schema
load_job = client.load_table_from_uri(
uri,
table_ref,
location=location,
job_config=job_config,
)
load_job.result() # block until job is finished
# remove unpickleable attributes
load_job._client = None
load_job._completion_lock = None
return load_job
class BigQueryLoadFile(Task):
"""
Task for insert records in a Google BigQuery table via a [load
job](https://cloud.google.com/bigquery/docs/loading-data). Note that all of these settings
can optionally be provided or overwritten at runtime.
Args:
- file (Union[str, path-like object], optional): A string or path-like object of the
file to be loaded
- rewind (bool, optional): if True, seek to the beginning of the file handle before
reading the file
- size (int, optional): the number of bytes to read from the file handle. If size is
None or large, resumable upload will be used. Otherwise, multipart upload will be
used.
- num_retries (int, optional): the number of max retries for loading the bigquery table from
file. Defaults to `6`
- dataset_id (str, optional): the id of a destination dataset to write the records to
- table (str, optional): the name of a destination table to write the records to
- project (str, optional): the project to initialize the BigQuery Client with; if not
provided, will default to the one inferred from your credentials
- schema (List[bigquery.SchemaField], optional): the schema to use when creating the
table
- location (str, optional): location of the dataset that will be queried; defaults to
"US"
- **kwargs (optional): additional kwargs to pass to the `Task` constructor
"""
def __init__(
self,
file: Union[str, Path] = None,
rewind: bool = False,
size: int = None,
num_retries: int = 6,
dataset_id: str = None,
table: str = None,
project: str = None,
schema: List[bigquery.SchemaField] = None,
location: str = "US",
**kwargs,
):
self.file = file
self.rewind = rewind
self.size = size
self.num_retries = num_retries
self.dataset_id = dataset_id
self.table = table
self.project = project
self.schema = schema
self.location = location
super().__init__(**kwargs)
@defaults_from_attrs(
"file",
"rewind",
"size",
"num_retries",
"dataset_id",
"table",
"project",
"location",
)
def run(
self,
file: Union[str, Path] = None,
rewind: bool = False,
size: int = None,
num_retries: int = 6,
dataset_id: str = None,
table: str = None,
project: str = None,
schema: List[bigquery.SchemaField] = None,
location: str = "US",
credentials: dict = None,
**kwargs,
):
"""
Run method for this Task. Invoked by _calling_ this Task within a Flow context, after
initialization.
Args:
- file (Union[str, path-liike object], optional): A string or path-like object of
the file to be loaded
- rewind (bool, optional): if True, seek to the beginning of the file handle before
reading the file
- size (int, optional): the number of bytes to read from the file handle. If size
is None or large, resumable upload will be used. Otherwise, multipart upload
will be used.
- num_retries (int, optional): the number of max retries for loading the bigquery table from
file. Defaults to `6`
- dataset_id (str, optional): the id of a destination dataset to write the records
to; if not provided here, will default to the one provided at initialization
- table (str, optional): the name of a destination table to write the records to;
if not provided here, will default to the one provided at initialization
- project (str, optional): the project to initialize the BigQuery Client with; if
not provided, will default to the one inferred from your credentials
- schema (List[bigquery.SchemaField], optional): the schema to use when creating
the table
- location (str, optional): location of the dataset that will be written to;
defaults to "US"
- credentials (dict, optional): a JSON document containing Google Cloud
credentials. You should provide these at runtime with an upstream Secret task.
- **kwargs (optional): additional kwargs to pass to the `bigquery.LoadJobConfig`;
see the documentation here:
https://googleapis.github.io/google-cloud-python/latest/bigquery/generated/google.cloud.bigquery.client.Client.html
Raises:
- ValueError: if all required arguments haven't been provided or file does not exist
- IOError: if file can't be opened and read
- ValueError: if the load job results in an error
Returns:
- google.cloud.bigquery.job.LoadJob: the response from `load_table_from_file`
"""
# check for any argument inconsistencies
if dataset_id is None or table is None:
raise ValueError("Both dataset_id and table must be provided.")
try:
path = Path(file)
except Exception as value_error:
raise ValueError(
"A string or path-like object must be provided."
) from value_error
if not path.is_file():
raise ValueError(f"File {path.as_posix()} does not exist.")
# create client
client = get_bigquery_client(project=project, credentials=credentials)
# get table reference
table_ref = client.dataset(dataset_id).table(table)
# configure job
autodetect = kwargs.pop("autodetect", True)
job_config = bigquery.LoadJobConfig(autodetect=autodetect, **kwargs)
if schema:
job_config.schema = schema
# load data
try:
with open(file, "rb") as file_obj:
load_job = client.load_table_from_file(
file_obj,
table_ref,
rewind,
size,
num_retries,
location=location,
job_config=job_config,
)
except IOError as IO_error:
raise IOError(f"Can't open and read from {path.as_posix()}.") from IO_error
load_job.result() # block until job is finished
# remove unpickleable attributes
load_job._client = None
load_job._completion_lock = None
return load_job
class CreateBigQueryTable(Task):
"""
Ensures a BigQuery table exists; creates it otherwise. Note that most initialization keywords
can optionally be provided at runtime.
Args:
- project (str, optional): the project to initialize the BigQuery Client with; if not
provided, will default to the one inferred from your credentials
- dataset (str, optional): the name of a dataset in that the table will be created
- table (str, optional): the name of a table to create
- schema (List[bigquery.SchemaField], optional): the schema to use when creating the table
- clustering_fields (List[str], optional): a list of fields to cluster the table by
- time_partitioning (bigquery.TimePartitioning, optional): a
`bigquery.TimePartitioning` object specifying a partitioninig of the newly created
table
- **kwargs (optional): additional kwargs to pass to the `Task` constructor
"""
def __init__(
self,
project: str = None,
dataset: str = None,
table: str = None,
schema: List[bigquery.SchemaField] = None,
clustering_fields: List[str] = None,
time_partitioning: bigquery.TimePartitioning = None,
**kwargs,
):
self.project = project
self.dataset = dataset
self.table = table
self.schema = schema
self.clustering_fields = clustering_fields
self.time_partitioning = time_partitioning
super().__init__(**kwargs)
@defaults_from_attrs("project", "dataset", "table", "schema")
def run(
self,
project: str = None,
credentials: dict = None,
dataset: str = None,
table: str = None,
schema: List[bigquery.SchemaField] = None,
):
"""
Run method for this Task. Invoked by _calling_ this Task within a Flow context, after
initialization.
Args:
- project (str, optional): the project to initialize the BigQuery Client with; if
not provided, will default to the one inferred from your credentials
- credentials (dict, optional): a JSON document containing Google Cloud
credentials. You should provide these at runtime with an upstream Secret task.
If not provided, Prefect will first check `context` for `GCP_CREDENTIALS` and
lastly will use default Google client logic.
- dataset (str, optional): the name of a dataset in that the table will be created
- table (str, optional): the name of a table to create
- schema (List[bigquery.SchemaField], optional): the schema to use when creating
the table
Returns:
- None
Raises:
- SUCCESS: a `SUCCESS` signal if the table already exists
"""
client = get_bigquery_client(project=project, credentials=credentials)
try:
dataset_ref = client.get_dataset(dataset)
except NotFound:
self.logger.debug("Dataset {} not found, creating...".format(dataset))
dataset_ref = client.create_dataset(dataset)
table_ref = dataset_ref.table(table)
try:
client.get_table(table_ref)
raise SUCCESS(
"{dataset}.{table} already exists.".format(dataset=dataset, table=table)
)
except NotFound:
self.logger.debug("Table {} not found, creating...".format(table))
table = bigquery.Table(table_ref, schema=schema)
# partitioning
if self.time_partitioning:
table.time_partitioning = self.time_partitioning
# cluster for optimal data sorting/access
if self.clustering_fields:
table.clustering_fields = self.clustering_fields
client.create_table(table)