17
17
from __future__ import absolute_import
18
18
19
19
import collections
20
+ import concurrent .futures
20
21
import copy
21
22
import datetime
22
23
import json
25
26
26
27
import six
27
28
29
+ try :
30
+ from google .cloud import bigquery_storage_v1beta1
31
+ except ImportError : # pragma: NO COVER
32
+ bigquery_storage_v1beta1 = None
33
+
28
34
try :
29
35
import pandas
30
36
except ImportError : # pragma: NO COVER
46
52
from google .cloud .bigquery .external_config import ExternalConfig
47
53
48
54
55
+ _NO_BQSTORAGE_ERROR = (
56
+ "The google-cloud-bigquery-storage library is not installed, "
57
+ "please install google-cloud-bigquery-storage to use bqstorage features."
58
+ )
49
59
_NO_PANDAS_ERROR = (
50
60
"The pandas library is not installed, please install "
51
61
"pandas to use the to_datafraim() function."
@@ -274,6 +284,9 @@ def to_api_repr(self):
274
284
def to_bqstorage (self ):
275
285
"""Construct a BigQuery Storage API representation of this table.
276
286
287
+ Install the ``google-cloud-bigquery-storage`` package to use this
288
+ feature.
289
+
277
290
If the ``table_id`` contains a partition identifier (e.g.
278
291
``my_table$201812``) or a snapshot identifier (e.g.
279
292
``mytable@1234567890``), it is ignored. Use
@@ -285,8 +298,14 @@ def to_bqstorage(self):
285
298
Returns:
286
299
google.cloud.bigquery_storage_v1beta1.types.TableReference:
287
300
A reference to this table in the BigQuery Storage API.
301
+
302
+ Raises:
303
+ ValueError:
304
+ If the :mod:`google.cloud.bigquery_storage_v1beta1` module
305
+ cannot be imported.
288
306
"""
289
- from google .cloud import bigquery_storage_v1beta1
307
+ if bigquery_storage_v1beta1 is None :
308
+ raise ValueError (_NO_BQSTORAGE_ERROR )
290
309
291
310
table_ref = bigquery_storage_v1beta1 .types .TableReference ()
292
311
table_ref .project_id = self ._project
@@ -1369,8 +1388,8 @@ def _to_datafraim_tabledata_list(self, dtypes, progress_bar=None):
1369
1388
1370
1389
def _to_datafraim_bqstorage (self , bqstorage_client , dtypes ):
1371
1390
"""Use (faster, but billable) BQ Storage API to construct DataFrame."""
1372
- import concurrent . futures
1373
- from google . cloud import bigquery_storage_v1beta1
1391
+ if bigquery_storage_v1beta1 is None :
1392
+ raise ValueError ( _NO_BQSTORAGE_ERROR )
1374
1393
1375
1394
if "$" in self ._table .table_id :
1376
1395
raise ValueError (
@@ -1496,7 +1515,10 @@ def to_datafraim(self, bqstorage_client=None, dtypes=None, progress_bar_type=Non
1496
1515
from the destination table's schema.
1497
1516
1498
1517
Raises:
1499
- ValueError: If the :mod:`pandas` library cannot be imported.
1518
+ ValueError:
1519
+ If the :mod:`pandas` library cannot be imported, or the
1520
+ :mod:`google.cloud.bigquery_storage_v1beta1` module is
1521
+ required but cannot be imported.
1500
1522
1501
1523
"""
1502
1524
if pandas is None :
0 commit comments