【问题标题】:Cloud Function throwing error Type Error for bigquery client, but working fine on Jupyter NotebookCloud Function 为 bigquery 客户端抛出错误类型错误,但在 Jupyter Notebook 上工作正常
【发布时间】:2021-04-10 07:22:09
【问题描述】:

我正在尝试创建一个从 bigquery 获取数据的云函数。该代码在 Jupyter 上运行良好,但在 Cloud Function 上无法运行。

from google.cloud import bigquery

def main(request):
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] ='xxxx.json'
    bigquery_client = bigquery.Client(project='xxxx')

    query = """select scavenger_hunt_mode_time_spent_minutes, explore_mode_time_spent_minutes,
           browsemode_time_spent_minutes, nationalpark_time_spend_minutes, explore_marker_tracked_ids,
           browsemode_content_clicked_models_ids,browsemode_content_clicked_maps, national_park_lifetime_scenarios_solved,
           scavenger_hunt_mode_session_story_ids_completed, time from `plugo-244108.mix.new_tableau_orboot*` where Email='xxx.com' and orbootername='xxxx'"""
    query_job = bigquery_client.query(query)
    geography = query_job.to_dataframe()

    return len(geography)

TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a int.

【问题讨论】:

    标签: python api google-bigquery google-cloud-functions


    【解决方案1】:

    Cloud 函数遵循 Flask 的语法 - https://cloud.google.com/functions/docs/quickstart-python,因此当 int 在视图中返回时不起作用。将return len(geography) 简单更改为return str(len(geography)) 即可使该功能正常工作。

    只是一个附加,len(geography.index) 应该比len(geography) 快。

    【讨论】:

    • 感谢您的回复。此外,AWS Lambda 和 API Gateway 在这种情况下也发挥了作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2020-03-06
    • 2010-12-24
    • 2022-11-01
    • 1970-01-01
    • 2017-03-24
    相关资源
    最近更新 更多