接收带变量的 HTTP 请求,查询 BQ 并返回响应

Posted

技术标签:

【中文标题】接收带变量的 HTTP 请求,查询 BQ 并返回响应【英文标题】:Receive HTTP request with Variable, Query BQ and Return Response 【发布时间】:2020-06-12 02:44:39 【问题描述】:

我希望在 GCP 中创建一个云函数,它接收带有参数的 HTTP 请求,获取参数并将它们传递给 SQL 语句中的 Bigquery,并返回我可以传递回网站的结果。

我对此很陌生,而且我绝对不是工程师。我的云功能已正确部署,并且在浏览器中收到“OK”响应

当我点击它但无法让BQ 返回的值显示在浏览器上时。

到目前为止,这是我的功能,并提前感谢您的帮助。

import google.cloud.bigquery

def audience(QUERY):
   # BQ Query to get add to cart sessions
   QUERY = """select 
   visitId,
from bigquery-public-data.google_analytics_sample.ga_sessions_20170801
limit 10;
return QUERY"""

print(audience)

【问题讨论】:

【参考方案1】:

这是一个云函数示例,它完全运行您在帖子中提到的查询。尽管如此,它可以根据您的需要很容易地适应任何其他查询。您基本上需要遵循此tutorial 才能部署该功能并基本了解如何使用Client Library for BigQuery 查询数据。

以下是您需要做的总结:

    创建一个文件夹(例如cloudfunctionsexample)并使用cd [FOLDERNAME e.g. cloudfunctionsexample] 进入该文件夹并在该文件夹内创建两个文件:main.pyrequirements.txt

一个。主.py:

from flask import escape
from google.cloud import bigquery

client = bigquery.Client()

def bigquery_example(request):

    request_json = request.get_json(silent=True)
    request_args = request.args

    #Check if request have all the correct parameters to run the query
    if request_json and 'column' in request_json:
        column = request_json['column']
    elif request_args and 'column' in request_args:
        column = request_args['column']
    else:
        return('You are missing the column parameter on the request.')

    if request_json and 'name' in request_json:
        name = request_json['name']
    elif request_args and 'name' in request_args:
        name = request_args['name']
    else:
        return('You are missing the name of the dataset parameter on the request.')

    if request_json and 'limit' in request_json:
        limit = request_json['limit']
    elif request_args and 'limit' in request_args:
        limit = request_args['limit']
    else:
        return('You are missing the limit parameter on the request.')

    #Construct the query based on the parameters
    QUERY = ('SELECT '+column+' FROM `'+name+'` LIMIT '+limit)
    #print(QUERY)

    try:
        query_job = client.query(QUERY)  # API request
        rows = query_job.result()  # Waits for query to finish
        # Create a list and make the results html compatible to be able to be displayed on the browser.
        row_list = []
        for row in rows:
            row_list.append(str(row[column]))
        return("<p>" + "</p><p>".join(row_list) + "</p>")
    except e:
        return(e)

b.要求.txt:

flask
google-cloud-bigquery
    (假设您有Cloud SDK installed)并且您确保App Engine default service account(这是Cloud Functions 使用的默认帐户)分配了Editor 角色,运行以下命令在您的项目中部署该功能:
gcloud functions deploy bigquery_http_example --runtime python37 --trigger-http --allow-unauthenticated --entry-point=bigquery_example --timeout=540
    获取 Cloud Function URL 并使用 curl 命令发出 POST 请求,或简单地将参数添加到 Cloud Function URL 以向 Cloud Function 端点发出 HTTP 请求并直接在浏览器上查看结果。李>

一个。卷曲:

curl -X POST https://[REGION-FUNCTIONS_PROJECT_ID].cloudfunctions.net/bigquery_http_example -H "Content-Type:application/json"  -d '"column":"visitId","name":"bigquery-public-data.google_analytics_sample.ga_sessions_20170801","limit":"10"'

b.云函数网址:

https://[REGION-FUNCTIONS_PROJECT_ID].cloudfunctions.net/bigquery_http_example?column=visitId&name=bigquery-public-data.google_analytics_sample.ga_sessions_20170801&limit=10

【讨论】:

以上是关于接收带变量的 HTTP 请求,查询 BQ 并返回响应的主要内容,如果未能解决你的问题,请参考以下文章

iOS网络数据指标收集

Google BQ:运行参数化查询,其中参数变量是BQ表目标

将 BQ 查询的输出分配给变量

HTTP协议请求响应模型

AppEng Py Flexi :: 长时间运行的请求 :: 502 错误

C#带参数发送http请求,接收返回cookie