Flume 案例:监控端口数据并打印

127 阅读1分钟

数据流:NetCat TCP Source -> Memory Channel -> Logger Sink

配置文件:netcat-tcp-memory-logger.conf

# Name the components on this agent
a1.sources = r1
a1.channels = c1
a1.sinks = k1

# Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
# 最多存储 Event 的个数
a1.channels.c1.capacity = 1000
# 在一次事务中,从 Source 中获取 Event 或者写 Event 到 Sink 的最大数量
a1.channels.c1.transactionCapacity = 100

# Describe the sink
a1.sinks.k1.type = logger

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

准备工作:

# 安装 netcat 工具
sudo yum install -y nc

# 查看 44444 端口是否被占用
sudo netstat -nlp | grep 44444

运行:

flume-ng agent \
-n a1 \
-c conf \
-f conf/netcat-tcp-memory-logger.conf \
-Dflume.root.logger=INFO,console

发送数据:

~ nc localhost 44444
hello flume
OK

flume打印数据:

2023-02-16 14:09:05,202 INFO sink.LoggerSink: Event: { headers:{} body: 68 65 6C 6C 6F 20 66 6C 75 6D 65                hello flume }