Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x GCP发布/订阅-如何从BQ计划查询检索状态_Python 3.x_Google Cloud Platform_Google Cloud Pubsub - Fatal编程技术网

Python 3.x GCP发布/订阅-如何从BQ计划查询检索状态

Python 3.x GCP发布/订阅-如何从BQ计划查询检索状态,python-3.x,google-cloud-platform,google-cloud-pubsub,Python 3.x,Google Cloud Platform,Google Cloud Pubsub,我有一个大查询计划查询,它通过pub/sub触发一个云函数 我希望函数从pub/sub消息中读取“state”值,以便查看它是否成功完成 下面的语句将始终触发else语句。如果删除If语句,它将返回一个KeyError import base64 def hello_pubsub(event, context): data = base64.b64decode(event['data']).decode('utf-8') if 'state' in data:

我有一个大查询计划查询,它通过pub/sub触发一个云函数

我希望函数从pub/sub消息中读取“state”值,以便查看它是否成功完成

下面的语句将始终触发else语句。如果删除If语句,它将返回一个KeyError

import base64

def hello_pubsub(event, context):
    data = base64.b64decode(event['data']).decode('utf-8')

    if 'state' in data:
        state = data['state']
        print("returned state: " + state)
    else:
        print ("No state attribute found")
以下是函数应接收的pubsub消息:

{
"data":
{"dataSourceId": "scheduled_query", 
"destinationDatasetId": "xxxxxxxxxx", 
"emailPreferences": { }, 
"endTime": "2020-03-12T20:40:13.627285Z", 
"errorStatus": { },
"name": "xxxxxxxxxx", "notificationPubsubTopic": "projects/xxxxxxxxxx/topics/xxxxxxxxxx", 
"params": { "destination_table_name_template": "xxxxxxxxxx", "query": "xxxxxxxxxx", "write_disposition": "WRITE_TRUNCATE" }, 
"runTime": "2020-03-05T10:00:00Z", 
"scheduleTime": "2020-03-12T20:37:13.17166Z", 
"startTime": "2020-03-12T20:37:13.328479Z", 
"state": "SUCCEEDED", 
"updateTime": "2020-03-12T20:40:13.627307Z", 
"userId": "xxxxxxxxxx"
}
}

您可以查看python库

你也有

最后,你可以检查一下,我已经弄明白了

data = base64.b64decode(event['data']).decode('utf-8')
这将返回json格式的字符串,而不是字典对象。您需要通过以下方式转换为dict:

data_dict = json.loads(data)

以便能够像字典一样访问它。

在问题中显示函数收到的
事件
数据。“不工作”是什么意思?一个错误还是其中一条消息?嗨,约翰,很抱歉没有说得更清楚。我已经编辑了我的帖子以包含这些信息。你问题的更新是实际收到的数据还是你认为应该收到的?实际收到的,通过打印到日志中获得的。