我正在使用Google Pub Sub将Web API的JSON输出发布到特定主题。然后,我正在使用Google数据流模板。我使用的确切模板是PUBSUB到BIGQUERY,它将数据从pubsub移到我定义的特定表中。当任务运行时,所有数据将移至失败的表插入位置,而不是写入成功的记录。我认为消息的编码方式一定存在问题,但我尝试了多种尝试却无济于事。我的python代码在下面列出:
from __future__ import unicode_literals
from sodapy import Socrata
import json
from io import StringIO
from google.oauth2 import service_account
from oauth2client.client import GoogleCredentials
from google.cloud import pubsub_v1
import time
import datetime
import urllib
import urllib.request
import argparse
import base64
credentials = GoogleCredentials.get_application_default()
# change project to your Project ID
project="xxxx"
# change topic to your PubSub topic name
topic="xxxx"
res = urllib.request.urlopen('https:/myurl.com/test.json')
res_body = res.read()
traffic=json.loads(res_body)
publisher = pubsub_v1.PublisherClient()
topicName = 'projects/' + project + '/topics/' + topic
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project,topic)
for key in traffic:
publisher.publish(topicName,str.encode(str(key)))
print(key.items())