简体   繁体   中英

How to return boolean value from BigQuery in Python

I'm a new to BigQuery. I want to return boolean value from BigQuery. In this case, I want to output "True". In order to do this, I tried items() method as this article below says but I can't get the point how to use this method on my code. When it comes to getting value from this schema, Could anyone give me advice?? or how should I search it??

https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.table.Row.html

【This app functions】 takes request parameter and passes them to BigQuery and return True or False base on parameter to Client

from flask import Flask, request, jsonify
from google.cloud import bigquery


app = Flask(__name__)

@app.route('/')
def get_request():
    request_luid = request.args.get('luid') or ''
    client = bigquery.Client()
    query = """SELECT EXISTS(
                    SELECT 1
                    FROM `test-266778.conversion_log.conversion_log_2020*` as p
                    WHERE p.luid = '{}'
                    AND p.orderid != '' limit 1000)""".format(request_luid)


    job_config = bigquery.QueryJobConfig(
        query_parameters=[
            bigquery.ScalarQueryParameter("request_luid", "STRING", request_luid)
        ]
    )

    query_job = client.query(query)
    query_res = query_job.result()

    for row in query_res:
        return str(row)


if __name__ == "__main__":
    app.run()
【The result of the code above from Python file】

Row((True,), {'f0_': 0})


【On GCP console】

Row | f0_
1   | True ← This value should be output  

You need to change the return statement:

for row in query_res:
    return row[0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM