![](/img/trans.png)
[英]How to count messages from a subscription utilizing streaming pull with GCP Pub/Sub?
[英]GCP Pub/Sub - How to retrieve state from BQ scheduled query
我有一个 Big Query 计划查询,它通过发布/订阅触发云功能。
我希望函数从发布/订阅消息中读取“状态”值,以便我可以查看它是否成功完成。
下面将始终触发 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"
}
}
我已经想通了。
data = base64.b64decode(event['data']).decode('utf-8')
这将返回一个 json 格式的字符串,而不是一个字典对象。 您需要通过以下方式转换为 dict:
data_dict = json.loads(data)
为了能够像字典一样访问它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.