python 客户端的 PubSub publish() 函数中的动态属性数
Dynamic number of attributes in PubSub publish() function for python client
我正在尝试使用 Python 发布者客户端向 Pub/Sub service on the Google Cloud Platform 发布消息。
问题描述:
我有一个包含多个 sheet 的 excel 文件。每个 sheet 都有不同的列数。对于每个 sheet,我需要将每一行作为单独的消息发布到云上的 PubSub 服务。
对于具有 4 列的 sheet,我参考 documentation 中的示例代码使用了以下步骤:
- 从
google.cloud.pubsub_v1.PublisherClient()
class 创建一个名为 publisher
的对象
- 使用
publisher.publish()
方法将数据发送到我在云端的主题
- 在
publish()
方法的参数中,我将每一列视为消息中的一个属性,如以下代码行所示(此行用于从一行发布):
future = publisher.publish(topic_path, data, column1 = columnvalue1 , column2 = columnvalue2)
问题:
在我正在编写的程序中,我希望从用户指定的 sheet 发布消息。由于不同的 sheet 具有不同的列数,我想知道是否有任何方法可以根据特定 [=54] 中的列数动态更改 publish()
函数中的参数数量=]?
我的尝试;
我尝试使用 if-else 语句来使用暴力方法。例如,三个 sheet 分别有 2,3 和 4 列;我从 Excel 文件中将指定的 sheet 读取到 pandas 数据帧中。然后我遍历每一行并按如下方式发布内容:
for i,row in df.iterrows():
# <All other relevant code and operations>
if (args.sheet == 'Sheet1'):
future = publisher.publish(topic_path, data, column1 = columnvalue1 , column2 = columnvalue2)
elif (args.sheet == 'Sheet2'):
future = publisher.publish(topic_path, data, column1 = columnvalue1 , column2 = columnvalue2, column3 = columnvalue3)
elif (args.sheet == 'Sheet3'):
future = publisher.publish(topic_path, data, column1 = columnvalue1 , column2 = columnvalue2, column3 = columnvalue3, column4 = columnvalue4)
# <rest of the code>
这种方法有效;但随着 sheet 数量的增加,不断添加更多 elif
条件成为一项繁琐的任务。
有没有更好的选择来解决这个问题?
有两件事:
- 您需要知道 sheet 您需要哪些属性。你可以用你想要的格式定义它,YAML 或 JSON 非常适合这个
{
"sheet1": ["column1", "column2"],
"sheet2": ["column1", "column2", "column3"],
"sheet3": ["column1", "column2", "column3", "column4"],
}
- 然后您需要动态构建属性列表以将它们与消息一起推送。您需要查看发布方法定义
def publish(
self, topic, data, ordering_key="", retry=gapic_v1.method.DEFAULT, **attrs
):
和文档字符串
attrs (Mapping[str, str]): A dictionary of attributes to be sent as metadata. (These may be text strings or byte strings.)
所以,现在,根据sheet,你需要构建一个字典,并将其传递给发布方法
dictionary={'column1':'columnvalue1','column2':'columnvalue2'}
publisher.publish(topic_path, data,**dictionary)
你必须要关键部分。现在一些算法来浏览你的 sheet,获取值,根据 sheet 数字和定义构建字典。这就是全部
我正在尝试使用 Python 发布者客户端向 Pub/Sub service on the Google Cloud Platform 发布消息。
问题描述:
我有一个包含多个 sheet 的 excel 文件。每个 sheet 都有不同的列数。对于每个 sheet,我需要将每一行作为单独的消息发布到云上的 PubSub 服务。
对于具有 4 列的 sheet,我参考 documentation 中的示例代码使用了以下步骤:
- 从
google.cloud.pubsub_v1.PublisherClient()
class 创建一个名为 - 使用
publisher.publish()
方法将数据发送到我在云端的主题 - 在
publish()
方法的参数中,我将每一列视为消息中的一个属性,如以下代码行所示(此行用于从一行发布):future = publisher.publish(topic_path, data, column1 = columnvalue1 , column2 = columnvalue2)
publisher
的对象
问题:
在我正在编写的程序中,我希望从用户指定的 sheet 发布消息。由于不同的 sheet 具有不同的列数,我想知道是否有任何方法可以根据特定 [=54] 中的列数动态更改 publish()
函数中的参数数量=]?
我的尝试;
我尝试使用 if-else 语句来使用暴力方法。例如,三个 sheet 分别有 2,3 和 4 列;我从 Excel 文件中将指定的 sheet 读取到 pandas 数据帧中。然后我遍历每一行并按如下方式发布内容:
for i,row in df.iterrows():
# <All other relevant code and operations>
if (args.sheet == 'Sheet1'):
future = publisher.publish(topic_path, data, column1 = columnvalue1 , column2 = columnvalue2)
elif (args.sheet == 'Sheet2'):
future = publisher.publish(topic_path, data, column1 = columnvalue1 , column2 = columnvalue2, column3 = columnvalue3)
elif (args.sheet == 'Sheet3'):
future = publisher.publish(topic_path, data, column1 = columnvalue1 , column2 = columnvalue2, column3 = columnvalue3, column4 = columnvalue4)
# <rest of the code>
这种方法有效;但随着 sheet 数量的增加,不断添加更多 elif
条件成为一项繁琐的任务。
有没有更好的选择来解决这个问题?
有两件事:
- 您需要知道 sheet 您需要哪些属性。你可以用你想要的格式定义它,YAML 或 JSON 非常适合这个
{
"sheet1": ["column1", "column2"],
"sheet2": ["column1", "column2", "column3"],
"sheet3": ["column1", "column2", "column3", "column4"],
}
- 然后您需要动态构建属性列表以将它们与消息一起推送。您需要查看发布方法定义
def publish( self, topic, data, ordering_key="", retry=gapic_v1.method.DEFAULT, **attrs ):
和文档字符串
attrs (Mapping[str, str]): A dictionary of attributes to be sent as metadata. (These may be text strings or byte strings.)
所以,现在,根据sheet,你需要构建一个字典,并将其传递给发布方法
dictionary={'column1':'columnvalue1','column2':'columnvalue2'}
publisher.publish(topic_path, data,**dictionary)
你必须要关键部分。现在一些算法来浏览你的 sheet,获取值,根据 sheet 数字和定义构建字典。这就是全部