Flume + Kafka + HDFS:拆分消息

时间:2016-10-18 06:53:27

标签: hadoop hdfs apache-kafka avro flume

我有以下flume代理配置来读取来自kafka源的消息并将它们写回HDFS接收器

tier1.sources  = source1
tier 1.channels = channel1
tier1.sinks = sink1

tier1.sources.source1.type = org.apache.flume.source.kafka.KafkaSource
tier1.sources.source1.zookeeperConnect = 192.168.0.100:2181
tier1.sources.source1.topic = test
tier1.sources.source1.groupId = flume
tier1.sources.source1.channels = channel1
tier1.sources.source1.interceptors = i1
tier1.sources.source1.interceptors.i1.type = timestamp
tier1.sources.source1.kafka.consumer.timeout.ms = 100

tier1.channels.channel1.type = org.apache.flume.channel.kafka.KafkaChannel
tier1.channels.channel1.brokerList = 192.168.0.100:9092

tier1.channels.channel1.topic = test
tier1.channels.channel1.zookeeperConnect = 192.168.0.100:2181/kafka
tier1.channels.channel1.parseAsFlumeEvent = false

tier1.sinks.sink1.channel = channel1
tier1.sinks.sink1.type = hdfs
tier1.sinks.sink1.hdfs.writeFormat = Text
tier1.sinks.sink1.hdfs.fileType = DataStream
tier1.sinks.sink1.hdfs.filePrefix = test-kafka
tier1.sinks.sink1.hdfs.fileSufix = .avro
tier1.sinks.sink1.hdfs.useLocalTimeStamp = true
tier1.sinks.sink1.hdfs.path = /tmp/kafka/%y-%m-%d
tier1.sinks.sink1.hdfs.rollCount=0
tier1.sinks.sink1.hdfs.rollSize=0

如果每个投票期间只有一条卡夫卡消息到达,则kafka消息内容是正确序列化为文件的avro数据。

当两个kafka消息到达同一批次时,它们被分组在同一个HDFS文件中,因为avro消息包含schema + data,结果文件包含schema + data + schema + data,导致它无效.avro文件。

如何拆分avro事件以分割不同的kafka消息,将其中的每一个写入不同的文件

谢谢

1 个答案:

答案 0 :(得分:0)

一种方法:假设您将源kafka传入数据称为“ SourceTopic”。您可以将自定义接收器注册到此“ SourceTopic”。

<FlumeNodeRole>.sinks.<your-sink>.type =net.my.package.CustomSink

在CustomSink中,您可以编写一种方法来区分传入的消息,对其进行拆分,然后将其重新发送到其他“ DestinationTopic”。现在,此“ DestinationTopic”可以用作文件序列化的新渠道。

请参阅下面的链接了解管道排屑槽: https://flume.apache.org/FlumeUserGuide.html