使用Flume监听端口数据

案例需求

使用Flume监听一个端口(44444),收集该端口数据并打印到控制台

实现步骤

  1. 安装netcat工具
  1. sudo yum install -y nc
  1. 判断44444端口是否被占用
  1. sudo netstat -tunlp | grep 44444
  1. 创建Flume Agent配置文件flume-netcat-logger.conf
  1. # 进入flume目录下的conf文件夹
  2. cd conf
  3. # 在job文件夹下创建flume-netcat-logger.conf文件
  4. vim flume-netcat-logger.conf
  1. 在flume-netcat-logger.conf文件中写入如下内容
# 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 = 44444

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

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

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

a1表示agent的名称

  1. 开启flume监听端口
# 方法1
bin/flume-ng agent --conf conf/ --name a1 --conf-file conf/flume-netcat-logger.conf -Dflume.root.logger=INFO,console

# 方法2
bin/flume-ng agent -c conf/ -n a1 -f conf/flume-netcat-logger.conf -Dflume.root.logger=INFO,console

参数说明:

—conf/-c:表示配置文件存储在 conf/目录

—name/-n:表示给 agent 起名为 a1

—conf-file/-f:flume 本次启动读取的配置文件是在 job 文件夹下的 flume-telnet.conf文件。

-Dflume.root.logger=INFO,console :-D 表示 flume 运行时动态修改 flume.root.logger参数属性值,并将控制台日志打印级别设置为 INFO 级别。日志级别包括:log、info、warn、error。

  1. 使用netcat工具向本机的44444端口发送内容
nc localhost 44444
# 发送以下内容
hello
world
  1. 在flume监听页面观察接收数据的情况