googledatalab / datalab

Interactive tools and developer experiences for Big Data on Google Cloud Platform.
Apache License 2.0
975 stars 249 forks source link

Cannot connect to private BigQuery datasets from datalab #2087

Open trickster opened 5 years ago

trickster commented 5 years ago

This gives me correct json output as the result. Executed inside datalab instance under a service account.

!echo "{\"kind\":\"bigquery#queryRequest\",\"useLegacySql\":false,\"query\":\"select count(*) from private.data.table;\"}" | \
  curl -X POST -d @- -H "Content-Type: application/json" \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    https://www.googleapis.com/bigquery/v2/projects/private/queries

But this python code does not gives me an error in the same environment.

from google.cloud import bigquery

client = bigquery.Client()

query_job=client.query("""
select count(*) from private.data.table""")
results = query_job.result()

This is the stacktrace

---------------------------------------------------------------------------
NotFound                                  Traceback (most recent call last)
<ipython-input-25-c641c54e5935> in <module>()
      1 job=client.query("""
      2 select count(*) from private.data.table""")
----> 3 job.result()

/usr/local/envs/py3env/lib/python3.5/site-packages/google/cloud/bigquery/job.py in result(self, timeout, retry)
   2641         # Do not refresh is the state is already done, as the job will not
   2642         # change once complete.
-> 2643         if self.state != _DONE_STATE:
   2644             self._query_results = self._client._get_query_results(
   2645                 self.job_id, retry,

/usr/local/envs/py3env/lib/python3.5/site-packages/google/cloud/bigquery/job.py in result(self, timeout)
    686         :rtype: _AsyncJob
    687         :returns: This instance.
--> 688 
    689         :raises:
    690             :class:`~google.cloud.exceptions.GoogleCloudError` if the job

/usr/local/envs/py3env/lib/python3.5/site-packages/google/api_core/future/polling.py in result(self, timeout)
    104             timeout (int):
    105                 How long (in seconds) to wait for the operation to complete.
--> 106                 If None, wait indefinitely.
    107 
    108         Returns:

/usr/local/envs/py3env/lib/python3.5/site-packages/google/cloud/bigquery/job.py in _blocking_poll(self, timeout)
   2615                   yet complete.
   2616         """
-> 2617         result = self._job_statistics().get('estimatedBytesProcessed')
   2618         if result is not None:
   2619             result = int(result)

/usr/local/envs/py3env/lib/python3.5/site-packages/google/api_core/future/polling.py in _blocking_poll(self, timeout)
     83         Args:
     84             timeout (int):
---> 85                 How long (in seconds) to wait for the operation to complete.
     86                 If None, wait indefinitely.
     87         """

/usr/local/envs/py3env/lib/python3.5/site-packages/google/api_core/retry.py in retry_wrapped_func(*args, **kwargs)
    258                 sleep_generator,
    259                 self._deadline,
--> 260                 on_error=on_error,
    261             )
    262 

/usr/local/envs/py3env/lib/python3.5/site-packages/google/api_core/retry.py in retry_target(target, predicate, sleep_generator, deadline, on_error)
    175     for sleep in sleep_generator:
    176         try:
--> 177             return target()
    178 
    179         # pylint: disable=broad-except

/usr/local/envs/py3env/lib/python3.5/site-packages/google/api_core/future/polling.py in _done_or_raise(self)
     60 
     61     @abc.abstractmethod
---> 62     def done(self):
     63         """Checks to see if the operation is complete.
     64 

/usr/local/envs/py3env/lib/python3.5/site-packages/google/cloud/bigquery/job.py in done(self, retry)
   2603 
   2604         return parameters
-> 2605 
   2606     @property
   2607     def estimated_bytes_processed(self):

/usr/local/envs/py3env/lib/python3.5/site-packages/google/cloud/bigquery/client.py in _get_query_results(self, job_id, retry, project, timeout_ms, location)
    577 
    578         path = '/projects/{}/queries/{}'.format(project, job_id)
--> 579 
    580         # This call is typically made in a polling loop that checks whether the
    581         # job is complete (from QueryJob.done(), called ultimately from

/usr/local/envs/py3env/lib/python3.5/site-packages/google/cloud/bigquery/client.py in _call_api(self, retry, **kwargs)
    329             method='POST', path=path, data=table.to_api_repr())
    330         return Table.from_api_repr(api_response)
--> 331 
    332     def _call_api(self, retry, **kwargs):
    333         call = functools.partial(self._connection.api_request, **kwargs)

/usr/local/envs/py3env/lib/python3.5/site-packages/google/api_core/retry.py in retry_wrapped_func(*args, **kwargs)
    258                 sleep_generator,
    259                 self._deadline,
--> 260                 on_error=on_error,
    261             )
    262 

/usr/local/envs/py3env/lib/python3.5/site-packages/google/api_core/retry.py in retry_target(target, predicate, sleep_generator, deadline, on_error)
    175     for sleep in sleep_generator:
    176         try:
--> 177             return target()
    178 
    179         # pylint: disable=broad-except

/usr/local/envs/py3env/lib/python3.5/site-packages/google/cloud/_http.py in api_request(self, method, path, query_params, data, content_type, headers, api_base_url, api_version, expect_json, _target_object)
    291 
    292         if not 200 <= response.status_code < 300:
--> 293             raise exceptions.from_http_response(response)
    294 
    295         if expect_json and response.content:

NotFound: 404 GET https://www.googleapis.com/bigquery/v2/projects/private/queries/*****************?maxResults=0: Not found: Job private:*****************