python - 将 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())
解决方案
推荐阅读
- twilio - 如何在 Studio 中存储 JSON 变量?
- laravel - 复选框在 Laravel Nova 自定义工具中不起作用
- google-apps-script - 有没有办法使用/结合 python 代码和添加谷歌套件?
- php - mySQL 结果为 JSON 数组 PHP
- sql - 找出比较同一行中其他列的最小列值
- ios - 如何在 SwiftUI 中检测 TextField 的实时变化?
- c# - 尝试添加新产品时,一个或多个实体的验证失败
- c++ - 在 Windows 上使用 MFC 或 ATL 标头和 MinGW 编译代码?
- python-3.x - Python -- While 循环失败
- java - selenium jre1.7 和 1.8 兼容性冲突