Edits history of script submission #292 for ' Execute arbitrary query (bigquery)'

  • python3
    One script reply has been approved by the moderators
    Ap­pro­ved
    #requirements:
    #google-cloud-bigquery
    #pandas
    #db-dtypes
    
    import os
    import json
    from google.cloud import bigquery
    
    # try with a query such as
    """
        SELECT name, SUM(number) as total_people
        FROM `bigquery-public-data.usa_names.usa_1910_2013`
        WHERE state = 'TX'
        GROUP BY name, state
        ORDER BY total_people DESC
        LIMIT 20
    """
    
    gcp_service_account = dict
    
    
    def main(sa: gcp_service_account, query: str):
    
        sa_path = "/tmp/sa.json"
        f = open(sa_path, "a")
        f.write(json.dumps(sa))
        f.close()
    
        os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = sa_path
        client = bigquery.Client()
        query_job = client.query(query)
    
        return query_job.to_dataframe().values.tolist()
    

    Submitted by rubenfiszel 1218 days ago