发布到kafka主题的典型示例

时间:2019-06-27 00:19:35

标签: python apache-kafka faust

我很好奇您应该如何表达自己想要在浮士德时代传递给Kafka主题的消息。他们的自述文件中的示例似乎未写入主题:

import faust

class Greeting(faust.Record):
    from_name: str
    to_name: str

app = faust.App('hello-app', broker='kafka://localhost')
topic = app.topic('hello-topic', value_type=Greeting)

@app.agent(topic)
async def hello(greetings):
    async for greeting in greetings:
        print(f'Hello from {greeting.from_name} to {greeting.to_name}')

@app.timer(interval=1.0)
async def example_sender(app):
    await hello.send(
        value=Greeting(from_name='Faust', to_name='you'),
    )

if __name__ == '__main__':
    app.main()

我希望以上代码中的hello.send会向该主题发布消息,但似乎没有。

有许多阅读主题的示例,还有许多使用cli推送即席消息的示例。梳理完文档后,我看不到任何清晰的示例将示例发布到代码中。我只是发疯了,上面的代码应该可以工作吗?

5 个答案:

答案 0 :(得分:4)

send()函数是调用以写入主题的正确函数。您甚至可以指定一个特定的分区,就像等效的Java API调用一样。

这里是send()方法的参考:

https://faust.readthedocs.io/en/latest/reference/faust.topics.html#faust.topics.Topic.send

答案 1 :(得分:0)

您可以使用sink告诉Faust将代理功能的结果传递到哪里。如果需要,还可以一次将多个主题用作接收器。

@app.agent(topic_to_read_from, sink=[destination_topic])
async def fetch(records):
    async for record in records:
        result = do_something(record)
        yield result

答案 2 :(得分:0)

因此,我们遇到了向除sink主题之外的其他主题发送消息的需求。

我们发现的最简单的方法是:foo = await my_topic.send_soon(value="wtfm8")

您也可以像下面通过asyncio事件循环一样直接使用send

loop = asyncio.get_event_loop()
foo = await ttopic.send(value="wtfm8??")
loop.run_until_complete(foo)

答案 3 :(得分:0)

如果您只想让Faust生产者(而不是与使用者/接收者结合使用),则原始问题实际上具有正确的代码位,这是一个功能齐全的脚本,可将消息发布到“ faust_test” Kafka主题,该主题可通过以下方式使用任何卡夫卡/浮士德消费者。

像下面这样运行以下代码:python faust_producer.py worker

"""Simple Faust Producer"""
import faust

if __name__ == '__main__':
    """Simple Faust Producer"""

    # Create the Faust App
    app = faust.App('faust_test_app', broker='localhost:9092')
    topic = app.topic('faust_test')

    # Send messages
    @app.timer(interval=1.0)
    async def send_message(message):
        await topic.send(value='my message')

    # Start the Faust App
    app.main()

答案 4 :(得分:0)

不知道这有多么重要,但我在尝试学习 Faust 时遇到了这个问题。从我读到的,这是正在发生的事情:

topic = app.topic('hello-topic', value_type=Greeting)

这里的误解是您创建的主题就是您尝试使用/读取的主题。您创建的主题当前没有任何作用。

await hello.send(
        value=Greeting(from_name='Faust', to_name='you'),
    )

这实质上创建了一个中间 kstream,它将值发送到您的 hello(greetings) 函数。 def hello(...) 将在有新消息发送到流时被调用,并处理正在发送的消息。

@app.agent(topic)
async def hello(greetings):
    async for greeting in greetings:
        print(f'Hello from {greeting.from_name} to {greeting.to_name}')

这是从 hello.send(...) 接收 kafka 流并简单地将其打印到控制台(没有输出到创建的“主题”)。您可以在此处向新主题发送消息。因此,您可以执行以下操作,而不是打印:

topic.send(value = "my message!")

或者:

这是你在做什么:

  1. example_sender() 向 hello(...) 发送消息(通过中间 kstream)
  2. hello(...) 拿起消息并打印出来 注意:不会向正确的主题发送消息

您可以这样做:

  1. example_sender() 向 hello(...) 发送消息(通过中间 kstream)

  2. hello(...) 拿起消息并打印

  3. hello(...) 还向创建的主题发送一条新消息(假设您正在尝试转换原始数据)

     app = faust.App('hello-app', broker='kafka://localhost')
     topic = app.topic('hello-topic', value_type=Greeting)
     output_topic = app.topic('test_output_faust', value_type=str)
    
     @app.agent(topic)
     async def hello(greetings):
         async for greeting in greetings:
             new_message = f'Hello from {greeting.from_name} to {greeting.to_name}'
             print(new_message)
             await output_topic.send(value=new_message)