This example illustrates how to create a Google cloud credentials object from your Data Observatory enabled CARTO account.
from cartoframes.auth import Credentials
creds = Credentials.from_file('creds.json')
from google.oauth2.credentials import Credentials as GoogleCredentials
gcloud_project, gcloud_token = creds.get_gcloud_credentials()
gcloud_credentials = GoogleCredentials(gcloud_token)
from google.cloud import bigquery, storage
bq_client = bigquery.Client(project=gcloud_project, credentials=gcloud_credentials)
gcs_client = storage.Client(project=gcloud_project, credentials=gcloud_credentials)
As an example, the following code shows how to use the Google BigQuery client to access to a public dataset from Data Observatory:
from cartoframes.data.observatory import Dataset
dataset = Dataset.get('acs_sociodemogr_8c2655e0')
dataset.id
'carto-do-public-data.usa_acs.demographics_sociodemographics_usa_county_2015_5yrs_20132017'
query_job = bq_client.query('SELECT * FROM `{}` LIMIT 1'.format(dataset.id))
results = query_job.result()
results.schema[0:3]
[SchemaField('geoid', 'STRING', 'NULLABLE', None, (), None), SchemaField('do_date', 'DATE', 'NULLABLE', None, (), None), SchemaField('total_pop', 'FLOAT', 'NULLABLE', None, (), None)]
for row in results:
print(row['geoid'], row['total_pop'])
18025 10598.0