前言
截至今天,在 Python SDK 2.9.0 的当前版本中,Google 云 PubSub 功能仍在积极开发中,目前仅设计用于流处理,因此您不能将它们用作发布到 PubSub 的管道的一部分.
您仍然可以安装 Google PubSub 客户端,但由于 Apache Beam 运行时的具体情况,您会偶然发现一些限制和细微差别,例如:
- 您必须安装
google-cloud-pubsub 并指定您对 Apache Beam 的依赖项
- 克服为 Apache Beam 管道安装的 PubSub 客户端的序列化不兼容问题
- 以某种方式显式为 PubSub 客户端提供身份验证凭据
这是一个基本的演练,让管道能够将元素从已处理的集合发布到 Google 云 PubSub 服务。
让我们假设一个基本结构:
├── my_stuff
│ ├── __init__.py
│ └── my_package.py
├── .gitignore
├── main.py
├── README.md
└── setup.py
获取 PubSub 客户端
安装 google-cloud-pubsub(假设 pip:pip install google-cloud-pubsub)现在您将遇到providing dependencies 的问题,我建议您遵循文档的最后一部分并提供@ 987654327@ 带有一些元数据和你的包依赖:
from setuptools import find_packages, setup
setup(
name="my_stuff",
version="0.1.0",
description="My Pipeline for DirectRunner that publishes to Google Cloud PubSub",
long_description=open("README.md").read(),
classifiers=[
"Programming Language :: Python",
],
author="John Doe",
author_email="john.doe@example.com",
url="https://example.com/",
license="proprietary",
packages=find_packages(),
include_package_data=True,
zip_safe=True,
install_requires=['google-cloud-pubsub==0.35.4'],
)
您可以使用pip freeze | grep google-cloud-pubsub 获取准确安装的版本。
使 PubSub 客户端可序列化
如果您只是尝试 Map 具有 PubSub 客户端实例的发布函数,那么您会从 Apache Beam 收到一个奇怪的错误,说它无法反序列化它。
为了克服这个问题,您可以创建一个可调用的类并在 pickle documentation 之后实现一些方法来克服序列化问题。
这是一个 PubSub 发布者实例化客户端的基本示例:
class Publisher(object):
"""
PubSub publisher for the beam pipeline.
"""
@staticmethod
def init_client():
return pubsub_v1.PublisherClient(credentials='TODO: Get credentials')
def __init__(self, topic):
self.topic = topic
self.client = self.init_client()
def __getstate__(self):
return self.topic
def __setstate__(self, topic):
self.topic = topic
self.client = self.init_client()
def __call__(self, item, *args, **kwargs):
self.client.publish(self.topic, b'{}'.format(item))
指定凭据
从问题中不清楚是否需要重用管道运行时的凭据或需要单独指定。
指定凭据的方法很少。您可以使用service_account.Credentials 构建它们,或者使用GoogleCredentials 重用运行时的凭据。
硬编码凭据
from google.cloud import pubsub_v1
from google.oauth2 import service_account
client1 = pubsub_v1.PublisherClient(
credentials=service_account.Credentials.from_service_account_info({
"type": "service_account",
"project_id": "****",
"private_key_id": "****",
"private_key": "-----BEGIN PRIVATE KEY-----\n****\n-----END PRIVATE KEY-----\n",
"client_email": "****",
"client_id": "****",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/***"
})
)
来自 JSON 文件的凭据(通过操作系统环境变量)
from google.cloud import pubsub_v1
from google.oauth2 import service_account
import os
client2 = pubsub_v1.PublisherClient(
credentials=service_account.Credentials.from_service_account_file(
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]
)
)
重用运行时凭据(用于管道执行)
from google.cloud import pubsub_v1
from oauth2client.client import GoogleCredentials
client3 = pubsub_v1.PublisherClient(
credentials=GoogleCredentials.get_application_default()
)
用法
现在您可以像任何其他转换一样在管道中使用Publisher:
published = (pipeline | "Publish" >> beam.Map(Publisher("pub/sub/topic")))
请不要忘记,您需要为管道运行器添加 --setup_file /absolute/path/to/setup.py 参数。