Flume_Flume常用配置6 source:taildir channel:memory sink:kafka

 

   最近,博主研究了一下之前一直想研究的 kafka sink。特此记录一下:

 

下面讲解一些注意事项:

 

对于 memory channel 来说 :

 

capacity : 

默认值:100

单位: int

含义: 通道内事件的最大数量。

注意:

     如果事件之间的大小差别很大,可以使用字节数来进行调整。(以下两项)

 

byteCapacity:

默认值: JVM 堆大小的80%

单位: long (字节)

含义: 通道内事件的最大字节数。

注意:

       计算是根据事件体进行评估的。如果有任何头,那么实际的内存使用量就会比配置的值大一些。

 

byteCapacityBufferPercentage :

默认值 : 20%

单位 : int (百分比)

含义: 通道内时间的最大字节数所占的百分比。

 

 

==================================================

 

Step 1:

 

首先,我们先创建 一个 topic

 

${KAFKA_HOME}/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3  --topic flume_kafka_test

${KAFKA_HOME}/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 3 --topic flume_kafka_test

 

=================================================

 

Step2 :

 

并启动一个控制台消费者,用于监听 flume 是否将数据采集到 kafka 中。

 

kafka-console-consumer.sh --topic flume_kafka_test --bootstrap-server 192.168.75.128:9092 --zookeeper localhost:2181

kafka-console-consumer.sh --topic flume_kafka_test --bootstrap-server 192.168.75.128:9092 --zookeeper localhost:2181

 

==================================================

 

Step3: 

 

其次,我们编写一个构造数据的脚本

目录:

/usr/local/flume/my-bin/taildir_withhead-memory-kafka-init

名称:

gen-testcase.sh

#!/bin/bash

logdir1="/tmp/logs/tailDir"
logdir2="/tmp/logs/tailDir2"

if [ ! -d "$logdir1" ]; then
	mkdir -p $logdir1
fi

if [ ! -d "$logdir2" ]; then
        mkdir -p $logdir2
fi


for((i=0; i<=100; i++));do 
	echo "kafka_test-$logdir1 : "+$i >> ${logdir1}/kafka.log;
done

for((i=0; i<=100; i++));do  
        echo "kafka_test-$logdir2 : "+$i >> ${logdir2}/kafka.log;
done

 

 

 

会向我们的 filegroup 的其中一个目录, 输出一些测试用例。

 

==================================================

 

Step4 :

  编写启动脚本。

目录:

/usr/local/flume/my-bin

名称:

start_taildir_withhead-memory-kafka.sh 

#!/bin/bash

ROOT_PATH=$(dirname $(dirname $(readlink -f $0)))
cd $ROOT_PATH

bin/flume-ng agent --conf ./conf/ -f my-conf/flume-taildir_withhead-memory-kafka.properties -Dflume.root.logger=INFO,console -n kafka_agent

 

 

Tips :

     这里特别强调一点 , 启动的 agent name 并不是随便设置的。

这个名字需要 和 properties 中的名字对应上。

这个 agent name 主要方便是一个配置文件配置多个 agent ,指定一个 agent 启动。

 

===========================================================================

 

Step5 :

     编写配置文件

目录:

/usr/local/flume/my-conf

名称:

flume-taildir_withhead-memory-kafka.properties

#simple.conf: A single-node Flume configuration
 
# Name the components on this agent
kafka_agent.sources = r1
kafka_agent.sinks = k1
kafka_agent.channels = c1
 
# Describe/configure the source
kafka_agent.sources.r1.type = TAILDIR
kafka_agent.sources.r1.filegroups = f1 f2
kafka_agent.sources.r1.filegroups.f1 = /tmp/logs/tailDir/.*\.log
kafka_agent.sources.r1.filegroups.f2 = /tmp/logs/tailDir2/.*\.log
#配置source:TAILDIR 下的文件偏移量
kafka_agent.sources.r1.positionFile = /tmp/logs/tailDir2/.flume/taildir_position.json
 
kafka_agent.sources.r1.interceptors = i1 i2 i3

#拦截器配置
kafka_agent.sources.r1.interceptors.i1.type = timestamp
kafka_agent.sources.r1.interceptors.i1.preserveExisting = true
 
kafka_agent.sources.r1.interceptors.i2.type = host
kafka_agent.sources.r1.interceptors.i2.preserveExisting = true
 
kafka_agent.sources.r1.interceptors.i3.type = static
kafka_agent.sources.r1.interceptors.i3.key = country
kafka_agent.sources.r1.interceptors.i3.value = China
 
# Describe the sink
kafka_agent.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
kafka_agent.sinks.k1.topic = flume_kafka_test
kafka_agent.sinks.k1.brokerList = 192.168.75.128:9092
kafka_agent.sinks.k1.producer.type=sync
kafka_agent.sinks.k1.serializer.class=kafka.serializer.StringEncoder
#设置kafka 副本确认机制 
#  与 kafka-producer 设置策略一致
kafka_agent.sinks.k1.requiredAcks=1
#一个批次处理的而消息数量, 一批次中处理消息越多,吞吐量提升,但延时也增加.
#kafka_agent.sinks.k1.batchSize=100

# Use a channel which buffers events in memory
kafka_agent.channels.c1.type = memory
kafka_agent.channels.c1.capacity = 1000
kafka_agent.channels.c1.transactionCapacity = 100
kafka_agent.channels.c1.byteCapacityBufferPercentage = 20
# 注意以下单位为 byte,  10485760 byte = 10MB
kafka_agent.channels.c1.byteCapacity = 10485760

# Bind the source and sink to the channel
kafka_agent.sources.r1.channels = c1
kafka_agent.sinks.k1.channel = c1

 

这里讲解下针对于 kafka sink 的一些配置:

 

 

 

 

==============================

 

Step 6 :

开始测试,

执行 

sh start_taildir_withhead-memory-kafka.sh

 

我们可以看到 kafka 消费者可以消费到数据,证明配置成功。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值