Skip to content

Big Query

This example demonstrates how to use the Big Query connector in ydata-sdk.

Don't forget to set up your license key

    import os

    os.environ['YDATA_LICENSE_KEY'] = '{add-your-key}'

Example Code

"""Google Cloud Big Query example."""

from ydata.connectors import BigQueryConnector
from ydata.utils.formats import read_json

def get_token(token_path: str):
    "Utility to load a token from .secrets"
    # Use relative path from file to token to be able to run regardless of the cwd()
    return read_json(token_path)


if __name__ == "__main__":

    # Load the secret token
    token = get_token("gcs_credentials.json")

    # Instantiate the Connector
    connector = BigQueryConnector(
        project_id="bucketname", keyfile_dict=token)

    # Load a dataset
    data = connector.query(
        query="""
        SELECT * FROM `ydatasynthetic.dataset_test.cardio_data`
    """
    )
    nrows, ncols = data.shape(lazy_eval=False)
    print(f"Our data has {nrows} rows and {ncols} columns.")

    data_sample = connector.query_sample(query="""
        SELECT * FROM `ydatasynthetic.dataset_test.cardio_data`
    """)

    print(data_sample)

    connector.write_table_from_data(data=data_sample, database='dataset_test', table='Teste_sample')