Python connection to google big query using ADC

Viewed 120

I am trying to get data from Google big query table using python. I dont have a service account access,but i have individual access to bigquery using gcloud. i have application default credentials Json file. I need to how to make a connection to bigquery usinG ADC.

code snippet:

from google.cloud import bigquery
conn=bigquery.Client()
query="select * from my_data.test1"
conn.query(query)

When i run above code snippet i am getting error saying:


NewConnectionError: <urllib3.connection.HttpsConnection object at 0x83dh46bdu640>: Failed to establish a new connection:[Error -2] Name or Service not known


Note: ENVIRONMENT Variable GOOGLE APPLICATION CREDENTIALS is not set and empty

1 Answers

Your script works for me because I authenticated using end user credentials from Google Cloud SDK, once you have the SDK installed you can simply run:

gcloud auth application-default login

The credentials from your json file are not being passed to the bigquery client, e.g.:

client = bigquery.Client(project=project, credentials=credentials)

to set that up you can follow these steps: https://cloud.google.com/bigquery/docs/authentication/end-user-installed

or this thread has some good details on setting the credentials environment variable: Setting GOOGLE_APPLICATION_CREDENTIALS for BigQuery Python CLI

Related