flume消费kafka数据太慢_修改flume源码实现按日志类型注入kafka topic的不同partition...

需求 : 根据flume采集到的header.type的类型分别注入topic的不同partition

思路 :

1.需要修改flume的sink的源码包,将原来基于哈希分配的策略覆盖掉。

2.将修改后的jar包放到flume的lib依赖中,覆盖原来的sink包。

3.配置启动文件。

4.分别消费不同topic查看是否成功。

一,下载源码

  1. 可以到flume官网自行下载:apache-flume-1.9.0-src

下载完成之后看一下目录结构,flume-ng-sink 就是我们要修改的部分

090f1bff42b17ce90153e7cf73de9b97.png

2. 将该模块导入编辑工具我使用的是idea,需要漫长的时间导入maven依赖,注意刚导入的项目使用的maven可能找不到配置。注意手动修改一下maven home。

f510a33992b3b0762f7963877b79cc27.png

下一步在pom.xml中添加

<build>
    


完成以上两步可以先尝试打一下包,如果打包成功,就可以安心的修改源码了。

二,修改源码

打开flume-ng-kafka-sink模块。

6da59d61d4486dd346710da5ebd03d52.png

找到KafkaSink

7c6947f3c5fdd31a38e6afc15c15214e.png

在KafkaSink中找到这样一个代码块。

1f71dffc2c6d0a2b79534b28f09ced41.png

可以看到,这里就是根据partitionId进行写入操作了。

所以我们只需要在这个代码块的上方写出自己想要partition分配策略就好了

8547b88616f6ecb244f5fd4f0e9e7a29.png
String 

修改完成后进行打包操作。

516c6777d246f45e1ef0c23d4c54735b.png

3,覆盖flume 依赖lib中原来的flume-ng-kafka-sink-1.9.0.jar

9d326b5f1f65ed16ec154d8c699404cb.png

4.设置flume启动文件 flume-kafka.conf

a1.sources= r1
a1.sinks= k1
a1.channels= c1
#Describe/configure the source
a1.sources.r1.type= http
a1.sources.r1.port= 50000
a1.sources.r1.channels= c1
#Describe the sink
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.topic = flume_kafka_test
a1.sinks.k1.kafka.bootstrap.servers = localhost:9092
a1.sinks.k1.kafka.flumeBatchSize = 20
a1.sinks.k1.kafka.producer.acks = 1
a1.sinks.k1.kafka.producer.linger.ms = 1
a1.sinks.k1.kafka.producer.compression.type = snappy
#Use a channel which buffers events in memory
a1.channels.c1.type= memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# 描述和配置source channel sink之间的连接关系
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

FLUME_HOME/bin下执行

flume-ng.cmd agent --conf ../conf -conf-file ../conf/flume_kafka.conf --name a1 -property flume.root.logger=INFO,console

cmd 插入数据

curl -X POST -d '[{"headers" :{"type":"INFO"},"body" : "hello INFO1"}]' http://localhost:50000
curl -X POST -d '[{"headers" :{"type":"DEBUG"},"body" : "hello DEBUG1"}]' http://localhost:50000
curl -X POST -d '[{"headers" :{"type":"WARN"},"body" : "hello WARN1"}]' http://localhost:50000
curl -X POST -d '[{"headers" :{"type":"ERROR"},"body" : "hello ERROR1"}]' http://localhost:50000
curl -X POST -d '[{"headers" :{"type":"ERROR"},"body" : "hello ERROR2"}]' http://localhost:50000
curl -X POST -d '[{"headers" :{"type":"DEBUG"},"body" : "hello DEBUG2"}]' http://localhost:50000
curl -X POST -d '[{"headers" :{"type":"WARN"},"body" : "hello WARN2"}]' http://localhost:50000

分别查看各个partition中的数据是否满足预期效果。

kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --partition 0 --from-beginning

bc503936f0a4b85397f3b1aa268c337d.png
kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --partition 1 --from-beginning

7b5ec1ea6559421bd3d7e94822454989.png
kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --partition 2 --from-beginning

9413e1dfc238fd8bf674bbd58cd01045.png
kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --partition 3 --from-beginning

34f57cac0b679bba75b9d2969e19a4fe.png

到此测试结束,成功了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值