首页 > 解决方案 > 将 JSON 输出从 Web api 发布到 Google Big Query 会导致数据写入失败的记录表

问题描述

我正在使用 Google Pub Sub 将 Web API 的 JSON 输出发布到特定主题。然后我使用谷歌数据流模板。我使用的确切模板是 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())

标签: pythonjsongoogle-bigquerygoogle-cloud-dataflowgoogle-cloud-pubsub

解决方案


推荐阅读