Search code examples
pythonflaskgoogle-bigqueryflask-restful

print to view result of BigQuery query


I have a BigQuery query inside a function as follows:

def get_data_from_bigquery():
    """query bigquery to get data to import to PSQL"""
    app_id = get_app_id()
    bq = bigquery.Client(project=app_id)
    query = """SELECT * FROM `Backup.Prod_UserUserMetadata`"""
    query_job = bq.query(query)
    data = query_job.result()
    rows = list(data)
    print(rows)
    return rows

I have my route as follows:

@app.route('/azure-import-data')
def print_data():
    return 'This is the data:\n \n'

The function get_data_from_bigquery() is being called inside my get() function inside the following class:

class AzureImportProcess(Resource):
    def get(self):
        get_data_from_bigquery()
...
api.add_resource(AzureImportProcess, '/azure-import-data')

The print(rows) function doesn't seem to work since it's not being printed either on Cloud Console or in the browser. Is there any other way I could print the data to see how it's being formatted?


Solution

  • First, you do not need the the print statement inside the get_data_from_bigquery function. Second, the get function needs to have a return statement in order for something to show.

    Edit the function definition as follows:

    def get_data_from_bigquery():
        """query bigquery to get data to import to PSQL"""
        app_id = get_app_id()
        bq = bigquery.Client(project=app_id)
        query = """SELECT * FROM `Backup.Prod_UserUserMetadata`"""
        query_job = bq.query(query)
        data = query_job.result()
        rows = list(data)
        return rows
    

    Edit the Resource class definition as follows:

    class AzureImportProcess(Resource):
        def get(self):
            bq_data = get_data_from_bigquery()
            return 'This is the data:\n \n{}'.format(bq_data)
    

    Now you can add your resource to the API object:

    api.add_resource(AzureImportProcess, '/azure-import-data')
    

    You don't need to define the print_data function here, unless you are dealing with more complicated API's.

    I hope this helps.