Windows上结合使用Flume和Kafka

时间:2023-03-08 16:54:00

Win7+Flume1.8.0 + Kafka1.0.0

1.目标

①使用Flume作为Kafka的Producer;

②使用Kafka作为Flume的Sink;

其实以上两点是同一个事情在Flume和Kafka两个立场上的不同描述而已,其实就是同一个事情。

2.运行Kafka

①运行Zookeeper

zkserver 

②运行Kafka

这里注意一下是否正常运行了,如果日志报错则将日志文件夹删除后再让其自动重新生成。

.\bin\windows\kafka-server-start.bat .\config\server.properties

③创建一个名字为flume的Topic

kafka-topics.bat --create --zookeeper localhost: --replication-factor  --partitions  --topic flume

④打开这个Topic的一个Consumer

kafka-console-consumer.bat --zookeeper localhost: --topic flume

具体可参看《Windows上搭建Kafka运行环境

3.运行Flume

①创建一个kafka_sink.conf配置文件

# example.conf: A single-node Flume configuration

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1 # Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = # Describe the sink
a1.sinks.k1.channel = c1
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.topic = flume
a1.sinks.k1.kafka.bootstrap.servers = localhost:
a1.sinks.k1.kafka.flumeBatchSize =
a1.sinks.k1.kafka.producer.acks =
a1.sinks.k1.kafka.producer.linger.ms =
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 =
a1.channels.c1.transactionCapacity = # Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

②启动Flume

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

具体可参看《Windows上搭建Flume运行环境

4.使用telnet连接并发送数据

①启动另外一个cmd,使用telnet连接到44444端口并发送信息Test Flume and Kafka!

(这里注意一下,在测试过程中,首条数据显示较慢!)

telnet localhost 

②在Kafka的Consumer终端显示如下:

Windows上结合使用Flume和Kafka

以上。