在使用 Apache Beam 的 KafkaIO ReadFromKafka 方法读取消息时,如果后续 Pipeline 未被触发,可能是因为没有正确使用 pipeline.run() 方法来启动 Pipeline。以下是一个示例代码:
import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
options = PipelineOptions()
p = beam.Pipeline(options=options)
messages = (p
| 'ReadFromKafka' >> beam.io.kafka.KafkaIO
.read_from_topics(['topic_name'], bootstrap_servers='host:port',
value_decoder=lambda x: x.decode('utf-8'))
)
# Define processing logic
processed_messages = (messages
| beam.Map(lambda message: message.upper()))
# Write results to a file
(processed_messages
| beam.io.WriteToText('output.txt'))
# Run the pipeline
p.run()
此示例代码从 Kafka 主题“topic_name”中读取消息,并将消息转换为大写字母,然后将其写入一个文件中。最后,使用pipeline.run()方法启动Pipeline。如果pipeline.run()方法未被调用,则Pipeline将无法执行。
上一篇:ApacheBeam结合GCPDataflow抛出INVALID_ARGUMENT错误
下一篇:ApacheBeam库的升级导致使用setRowSchema和setCoder时出现IllegalStateExceptions异常