88try :
99 # The BigQuery Storage API client is an optional dependency. It is only
1010 # required when use_bqstorage_api=True.
11- from google .cloud import bigquery_storage_v1beta1
11+ from google .cloud import bigquery_storage
1212except ImportError : # pragma: NO COVER
13- bigquery_storage_v1beta1 = None
13+ bigquery_storage = None
1414
1515from pandas_gbq .exceptions import AccessDenied
1616import pandas_gbq .schema
1717
1818logger = logging .getLogger (__name__ )
1919
2020BIGQUERY_INSTALLED_VERSION = None
21+ BIGQUERY_CLIENT_INFO_VERSION = "1.12.0"
22+ HAS_CLIENT_INFO = False
2123SHOW_VERBOSE_DEPRECATION = False
2224SHOW_PRIVATE_KEY_DEPRECATION = False
2325PRIVATE_KEY_DEPRECATION_MESSAGE = (
3436
3537
3638def _check_google_client_version ():
37- global BIGQUERY_INSTALLED_VERSION , SHOW_VERBOSE_DEPRECATION , SHOW_PRIVATE_KEY_DEPRECATION
39+ global BIGQUERY_INSTALLED_VERSION , HAS_CLIENT_INFO , SHOW_VERBOSE_DEPRECATION , SHOW_PRIVATE_KEY_DEPRECATION
3840
3941 try :
4042 import pkg_resources
@@ -44,10 +46,17 @@ def _check_google_client_version():
4446
4547 # https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/bigquery/CHANGELOG.md
4648 bigquery_minimum_version = pkg_resources .parse_version ("1.9.0" )
49+ bigquery_client_info_version = pkg_resources .parse_version (
50+ BIGQUERY_CLIENT_INFO_VERSION
51+ )
4752 BIGQUERY_INSTALLED_VERSION = pkg_resources .get_distribution (
4853 "google-cloud-bigquery"
4954 ).parsed_version
5055
56+ HAS_CLIENT_INFO = (
57+ BIGQUERY_INSTALLED_VERSION >= bigquery_client_info_version
58+ )
59+
5160 if BIGQUERY_INSTALLED_VERSION < bigquery_minimum_version :
5261 raise ImportError (
5362 "pandas-gbq requires google-cloud-bigquery >= {0}, "
@@ -392,6 +401,29 @@ def sizeof_fmt(num, suffix="B"):
392401
393402 def get_client (self ):
394403 from google .cloud import bigquery
404+ import pandas
405+
406+ try :
407+ # This module was added in google-api-core 1.11.0.
408+ # We don't have a hard requirement on that version, so only
409+ # populate the client_info if available.
410+ import google .api_core .client_info
411+
412+ client_info = google .api_core .client_info .ClientInfo (
413+ user_agent = "pandas-{}" .format (pandas .__version__ )
414+ )
415+ except ImportError :
416+ client_info = None
417+
418+ # In addition to new enough version of google-api-core, a new enough
419+ # version of google-cloud-bigquery is required to populate the
420+ # client_info.
421+ if HAS_CLIENT_INFO :
422+ return bigquery .Client (
423+ project = self .project_id ,
424+ credentials = self .credentials ,
425+ client_info = client_info ,
426+ )
395427
396428 return bigquery .Client (
397429 project = self .project_id , credentials = self .credentials
@@ -751,14 +783,20 @@ def _make_bqstorage_client(use_bqstorage_api, credentials):
751783 if not use_bqstorage_api :
752784 return None
753785
754- if bigquery_storage_v1beta1 is None :
786+ if bigquery_storage is None :
755787 raise ImportError (
756- "Install the google-cloud-bigquery-storage and fastavro packages "
757- "to use the BigQuery Storage API."
788+ "Install the google-cloud-bigquery-storage and fastavro/pyarrow "
789+ "packages to use the BigQuery Storage API."
758790 )
759791
760- return bigquery_storage_v1beta1 .BigQueryStorageClient (
761- credentials = credentials
792+ import google .api_core .gapic_v1 .client_info
793+ import pandas
794+
795+ client_info = google .api_core .gapic_v1 .client_info .ClientInfo (
796+ user_agent = "pandas-{}" .format (pandas .__version__ )
797+ )
798+ return bigquery_storage .BigQueryStorageClient (
799+ credentials = credentials , client_info = client_info
762800 )
763801
764802
0 commit comments