如何从 "Google Cloud functions" 连接到 "Google Cloud BigQuery" public 数据集

How to connect to "Google Cloud BigQuery" public dataset from "Google Cloud functions"

我修改了 hello_pubsub (Python3.7) 服务的默认函数,用于连接数据集 table 这是我在 "Google Cloud BigQuery" 服务中创建的。但是,在许多不同的方法之后,我对如何使这个函数连接到我在 BigQuery 中创建的数据集一无所知。我确信只有函数的 SQLite 代码有错误。有人,请帮助我

我的代码是:

import sqlite3
import base64

def hello_pubsub(event, context):
    """Triggered from a message on a Cloud Pub/Sub topic.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    connection = sqlite3.connect("<BigQueryDataset>")
    crsr = connection.cursor()
    crsr.execute("SELECT * FROM `iotcoretutorial-xxxxxx.DHT11.DHT11Data` WHERE temperature > 24") 
    ans= crsr.fetchall()    

    pubsub_message = base64.b64decode(event['data']).decode('utf-8')
    print("Hello "+pubsub_message)
    print(ans)

PS:这里的iotcoretutorial-xxxxxx指的是项目ID。我使用 xxxxxx 来隐藏我的项目身份(请多多包涵!)

简而言之iotcoretutorial-xxxxxx.DHT11.DHT11Data是我在"Google Cloud BigQuery"上创建的一个table,带有温度和湿度值,我想使用 hello_pubsub 函数打印,如果温度值 > 24

则执行一些操作

尝试使用 BigQuery Client Libraries. More docs here too.

import base64
from google.cloud import bigquery

def hello_pubsub(event, context):
    """Triggered from a message on a Cloud Pub/Sub topic.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    client = bigquery.Client(project="<your-project>")

    query_str = "SELECT * FROM `iotcoretutorial-xxxxxx.DHT11.DHT11Data` WHERE temperature > 24"

    job = client.query(
        query_str,
        # Location must match that of the dataset(s) referenced in the query.
        location="<your-location>"
    )
    #Wait for job to finish
    job.result()
    #Get results as a dataframe
    #This requires pandas
    #You can do something different with your results here if you want
    ans_df = job.to_dataframe()

    pubsub_message = base64.b64decode(event['data']).decode('utf-8')
    print("Hello " + pubsub_message)
    print(ans_df)