BigQuery

From XennisWiki
(Redirected from Google-cloud-bigquery)
Jump to: navigation, search

BigQuery is a analytics data warehouse of the Google Cloud Platform.

Python SDK

Installation

pip install google-cloud-bigquery

Example usage

from google.api_core.exceptions import NotFound
from google.cloud import firestore, bigquery
from datetime import datetime

def get_or_create_table(client: bigquery.Client) -> bigquery.Table:
    try:
        dataset = client.get_dataset('pm_sensor')
    except NotFound as _:
        dataset = client.create_dataset('pm_sensor')

    table_ref = bigquery.TableReference(dataset, table_id='data')
    try:
        return client.get_table(table_ref)
    except NotFound as _:
        return client.create_table(bigquery.Table(table_ref, schema=[
            bigquery.SchemaField('signal', 'NUMERIC', description='WiFi signal strength in dBm'),
            bigquery.SchemaField('temperature', 'NUMERIC', description='Sensor DHT22 temperature in °C'),
            bigquery.SchemaField('datetime', 'DATETIME', description='Datetime of measurement', mode='REQUIRED'),
        ]))


client = bigquery.Client()
errors = client.insert_rows(get_or_create_table(client), [{
    'signal': 56,
    'temperature': 12.6
    'datetime': datetime.now()
}])
if errors:
    logging.warning('failed to store data in bigquery: %s', errors)

See also

External links