Flume:监控端口数据官方案例

监控端口数据官方案例

1.案例需求:

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

2. 需求分析:

在这里插入图片描述

3. 实现步骤:

① 软件环境配置
(1)安装netcat工具
[atguigu@hadoop102 ~]$ sudo yum install -y nc

(2)判断44444端口是否被占用
[atguigu@hadoop102 ~]$ sudo netstat -nlp | grep 44444

(3)在flume目录下创建job文件夹并进入job文件夹。
[atguigu@hadoop102 flume]$ mkdir -p job/simpleCase
[atguigu@hadoop102 flume]$ cd job/simpleCase

② 编写配置文件(自己手敲一遍)
在job/simpleCase文件夹下创建Flume Agent配置文件flume-1-netcat-logger.conf, 添加如下内容
[atguigu@hadoop102 simpleCase]$ vim flume-1-netcat-logger.conf

#Name the components on this agent
a1.sources = r1                                      # 为a1的Source组件命名为r1,多个组件用空格间隔
a1.sinks = k1                                        # 为a1的Sink组件命名为k1,多个组件用空格间隔
a1.channels = c1                                    # 为a1的Channel组件命名为c1,多个组件用空格间隔

# Describe/configure the source
a1.sources.r1.type = netcat                      # 配置r1的类型
a1.sources.r1.bind = localhost                  # 配置r1的绑定地址(注意localhost和hadoop102的区别)
a1.sources.r1.port = 44444                       # 配置r1的监听端口

# Describe the sink
a1.sinks.k1.type = logger                        # 配置k1的类型为logger,输出到控制台

# Use a channel which buffers events in memory
a1.channels.c1.type = memory                    # 配置c1的类型为memory
a1.channels.c1.capacity = 1000                 # 配置c1的容量为1000个事件
a1.channels.c1.transactionCapacity = 100     # 配置c1的事务容量为100个事件

# Bind the source and sink to the channel
a1.sources.r1.channels = c1                    # 配置r1的channel属性,指定r1连接到那个channel
a1.sinks.k1.channel = c1                        # 配置k1的channel属性,指定k1连接到那个channel

注:配置文件来源于官方手册http://flume.apache.org/FlumeUserGuide.html

③ 部署运行flume监听端口
·第一种写法:
[atguigu@hadoop102 flume]$ bin/flume-ng agent --conf conf/ --name a1 --conf-file job/simpleCase/flume-1-netcat-logger.conf -Dflume.root.logger=INFO,console

·第二种写法:
[atguigu@hadoop102 flume]$ bin/flume-ng agent -c conf/ -n a1 -f job/simpleCase/flume-1-netcat-logger.conf -Dflume.root.logger=INFO,console

·参数说明:
–conf/-c:表示配置文件存储在conf/目录
–name/-n:表示给agent起名为a1
–conf-file/-f:指定读取的配置文件是在job/simpleCase文件夹下的flume-1-1netcat-logger.conf文件。
-Dflume.root.logger=INFO,console :-D表示flume运行时动态修改flume.root.logger参数属性值,并将控制台日志打印级别设置为INFO级别。日志级别包括:log、info、warn、error。

④ 测试
(1)使用netcat工具向本机的44444端口发送内容
[atguigu@hadoop102 flume]$ nc localhost 44444
hello
atguigu

(2)在Flume监听页面观察接收数据情况
……
2018-09-07 21:17:48,494 (SinkRunner-ProllingRunner-DefaultSinkProcessor) [INFO – org.apache.flume.sink.Sink.process(LoggerSink.java:95)] Event:{headers:{} body: 68 65 6c 6F 0D hello.}

思考:nc hadoop102 44444,flume能否接收到?

猜你喜欢

转载自blog.csdn.net/weixin_45427648/article/details/131841551