前期准备

了解Flume 架构及核心组件

Flume 架构及核心组件

Source : 收集(指定数据源从哪里获取)

Channel : 聚集

Sink : 输出(把数据写到哪里去)

学习使用 Flume

通过一个简单的小例子学习使用 Flume

使用 Flume 的关键就是写配置文件

配置文件的构成:

A) 配置 Source

B) 配置 Channel

C) 配置 Sink

D) 把以上三个组件串起来

A simple example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
# a1: agent 的名称
# r1: source 的名称
# k1: sink 的名称
# c1: channel 的名称 # Describe/configure the source
a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 44444
# type: source组件的类型
# bind: source绑定的主机或IP
# port: source绑定的端口号 # Describe the sink
a1.sinks.k1.type = logger
# 把日志输出到控制台 # Use a channel which buffers events in memory
a1.channels.c1.type = memory
# 存放在内存队列 # Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
# r1的channels指定到c1
# k1的channel从c1得到
# 一个source可以输出到多个channel
# 一个channel只能输出一个sink

实战一

需求

需求:从指定网络端口采集数据输出到控制台

写配置文件

/abs/app/apache-flume-1.6.0-cdh5.7.0-bin/conf 目录中新建 example.conf 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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 = hadoop
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 # Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动 agent

Flume 官网启动 agent 的命令:

1
$ bin/flume-ng agent -n $agent_name -c conf -f conf/flume-conf.properties.template

agent options:

1
2
3
--name,-n <name>          the name of this agent (required)
--conf,-c <conf> use configs in <conf> directory
--conf-file,-f <file> specify a config file (required if -z missing)

实际用的启动 agent 的命令:

1
flume-ng agent -n a1 -c $FLUME_HOME $FLUME_HOME/conf/example.conf -Dflume.root.logger=INFO,console

// Dflume.root.logger=INFO,console 为将输出结果显示到控制台

启动失败

1
2
3
4
5
Info: Including Hive libraries found via () for Hive access
+ exec /abs/app/jdk1.8.0_161/bin/java -Xmx20m -Dflume.root.logger=INFO,console -cp '/abs/app/apache-flume-1.6.0-cdh5.7.0-bin:/abs/app/apache-flume-1.6.0-cdh5.7.0-bin/lib/*:/lib/*' -Djava.library.path= org.apache.flume.node.Application -n a1 -f /abs/app/apache-flume-1.6.0-cdh5.7.0-bin/conf/example.conf
log4j:WARN No appenders could be found for logger (org.apache.flume.lifecycle.LifecycleSupervisor).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

上网查了一下,别人是 -c 的路径指定错误,我的也错了。

-c 后面跟的是 Flumeconf 目录

所以正确的启动命令为:

1
flume-ng agent -n a1 -c $FLUME_HOME/conf -f $FLUME_HOME/conf/example.conf -Dflume.root.logger=INFO,console

正常启动后可以看到如下:

可以看到 SinkSource 都启动了

绑定的主机名为 hadoop 的 IP 和绑定的端口号都有显示

验证

1
2
[root@hadoop ~]# telnet hadoop 44444
-bash: telnet: command not found

显示找不到 telnet ,用 yum install telnet 安装telnet

telnet 进入 hadoop 的 44444 端口进行输入单词按 Enter

agent 的那一端显示如下:

从图中可以看到如下:

1
Event: { headers:{} body: 73 70 61 72 6B 0D   spark. }

Event 是 Flume 数据传输的基本单元

Event = 可选的 header + byte array

以上实现了从指定网络端口采集数据输出到控制台的需求。


实战二

需求

需求:监控一个文件实时采集新增的数据输出到控制台

根据需求可以采用以下方案实现:

Agent 选型: exec source + memory channel + logger sink

写配置文件

大专栏  Flume 实战练习/abs/data 目录新建 data.log

1
touch data.log

/abs/app/apache-flume-1.6.0-cdh5.7.0-bin/conf 目录中新建 exec-memory-logger.conf 如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# exec-memory-logger.conf: A realtime 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 = exec
a1.sources.r1.command = tail -F /abs/data/data.log
a1.sources.r1.shell = /bin/sh -c # Describe the sink
a1.sinks.k1.type = logger # Use a channel which buffers events in memory
a1.channels.c1.type = memory # Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

启动 agent

Flume 启动 agent 的命令:

1
flume-ng agent -n a1 -c $FLUME_HOME/conf -f $FLUME_HOME/conf/exec-memory-logger.conf -Dflume.root.logger=INFO,console

// Dflume.root.logger=INFO,console 为将输出结果显示到控制台

正常启动后可以看到如下:

可以看到 SourceChannelSink 的类型和启动类型以及 Source 要执行的命令

验证

/abs/data 目录输入 echo hello >> data.log

agent 的那一端显示如下:

以上实现了监控一个文件实时采集新增的数据输出到控制台的需求。

拓展

参照 Flume 用户指南

如果用 Flume 采集数据做离线处理,可以使用 HDFS Sink

如果用 Flume 采集数据做实时处理,可以使用 Kafka Sink

这里只提供一个拓展,根据具体的需求使用。


实战三

需求

需求:将 A 服务器上的日志实时采集到 B 服务器

根据需求可以采用以下方案实现:

Agent A 选型: exec source + memory channel + avro sink

Agent B 选型: avro source + memory channel + logger sink

写配置文件

/abs/app/apache-flume-1.6.0-cdh5.7.0-bin/conf 目录中新建如下配置文件:

exec-memory-avro.conf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# exec-memory-avro.conf: A realtime Flume configuration
# Name the components on this agent
exec-memory-avro.sources = exec-source
exec-memory-avro.sinks = avro-sink
exec-memory-avro.channels = memory-channel # Describe/configure the source
exec-memory-avro.sources.exec-source.type = exec
exec-memory-avro.sources.exec-source.command = tail -F /abs/data/data.log
exec-memory-avro.sources.exec-source.shell = /bin/sh -c # Describe the sink
exec-memory-avro.sinks.avro-sink.type = avro
exec-memory-avro.sinks.avro-sink.hostname = hadoop
exec-memory-avro.sinks.avro-sink.port = 44444 # Use a channel which buffers events in memory
exec-memory-avro.channels.memory-channel.type = memory # Bind the source and sink to the channel
exec-memory-avro.sources.exec-source.channels = memory-channel
exec-memory-avro.sinks.avro-sink.channel = memory-channel

avro-memory-logger.conf:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# avro-memory-logger.conf: A realtime Flume configuration
# Name the components on this agent
avro-memory-logger.sources = avro-source
avro-memory-logger.sinks = logger-sink
avro-memory-logger.channels = memory-channel # Describe/configure the source
avro-memory-logger.sources.avro-source.type = avro
avro-memory-logger.sources.avro-source.bind = hadoop
avro-memory-logger.sources.avro-source.port = 44444 # Describe the sink
avro-memory-logger.sinks.logger-sink.type = logger # Use a channel which buffers events in memory
avro-memory-logger.channels.memory-channel.type = memory # Bind the source and sink to the channel
avro-memory-logger.sources.avro-source.channels = memory-channel
avro-memory-logger.sinks.logger-sink.channel = memory-channel

启动 agent

两个 Agent ,先启动 Agent A ,再启动 Agent B

先启动 avro-memory-logger:

1
flume-ng agent -n avro-memory-logger -c $FLUME_HOME/conf -f $FLUME_HOME/conf/avro-memory-logger.conf -Dflume.root.logger=INFO,console

再启动 exec-memory-avro:

1
flume-ng agent -n exec-memory-avro -c $FLUME_HOME/conf -f $FLUME_HOME/conf/exec-memory-avro.conf -Dflume.root.logger=INFO,console

验证

/abs/data/ 目录中输入以下命令:

1
2
echo hello spark >> data.log
echo Valentine >> data.log

Agent avro-memory-logger 显示如下:

以上实现了将 A 服务器上的日志实时采集到 B 服务器的需求。

这里采用的是一个服务器开三个窗口,有条件的可以尝试用两台服务器进行这个实战练习

Flume 实战练习的更多相关文章

  1. Flume实战案例运维篇

    Flume实战案例运维篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Flume概述 1>.什么是Flume Flume是一个分布式.可靠.高可用的海量日志聚合系统,支 ...

  2. Flume 实战(1) -- 初体验

    前言: Flume-ng是数据收集/聚合/传输的组件, Flume-ng抛弃了Flume OG原本繁重的zookeeper和Master, Collector, 其整体的架构更加的简洁和明了. 其基础 ...

  3. Flume 实战(2)--Flume-ng-sdk源码分析

    具体参考: 官方用户手册和开发指南 http://flume.apache.org/FlumeDeveloperGuide.html *) 定位和简单例子 1). Flume-ng-sdk是用于编写往 ...

  4. 5.flume实战(二)

    需求:监控一个文件实时采集新增的数据并输出到控制台 简单理解就是:监控一个文件,只要这个文件有新的内容追加,就将它输出到控制台. agent技术选型:exec source + memory chan ...

  5. 4.flume实战(一)

    需求:从指定网络端口采集数据输出到控制台 使用flume的关键就是写配置文件 a)配置source b)配置channel c)配置sink d)把以上三个组件串起来 我们看一下官网给的配置文件 # ...

  6. Flume 实战,将多台机器日志直接收集到 Kafka

    目前我们使用的一个 b 端软件的报错日志分散在集群各处,现在想把它收集到一个地方然后统一丢进 Kafka 提供给下游业务进行消费. 我想到了 flume,之前让同事搭建的这次自己想多了解一些细节于是就 ...

  7. 6.flume实战(三)※

    需求:将A服务器上的日志实时采集到B服务器上面去 大致原理: 技术选型: exec source + memory channel + avro sink avro source + memory c ...

  8. Spark Streaming从Flume Poll数据案例实战和内幕源码解密

    本节课分成二部分讲解: 一.Spark Streaming on Polling from Flume实战 二.Spark Streaming on Polling from Flume源码 第一部分 ...

  9. Flume+Sqoop+Azkaban笔记

    大纲(辅助系统) 离线辅助系统 数据接入 Flume介绍 Flume组件 Flume实战案例 任务调度 调度器基础 市面上调度工具 Oozie的使用 Oozie的流程定义详解 数据导出 sqoop基础 ...

随机推荐

  1. IOC&AOP

  2. 吴裕雄--天生自然TensorFlow高层封装:Keras-多输入输出

    # 1. 数据预处理. import keras from keras.models import Model from keras.datasets import mnist from keras. ...

  3. Error: Invalid or corrupt jarfile SpringBootTemplate.jar

    当在尝试将SpringBoot打包成为Jar文件, 丢到linux服务器去运行的时候, 尝试在windows自带的CMD窗口命令行中运行jar文件的时候, 遇到了这样的问题. 错误的意思是: 无效 或 ...

  4. xcode垃圾目录以及Mac隐藏显示文件快捷键

    ~/Library/Developer/Xcode/DerivedData 显示:defaults write com.apple.finder AppleShowAllFiles -bool tru ...

  5. FPGA 状态机-序列检测器verilog

    实现功能:检测出串行输入数据4位Data二进制序列0101,当检测到该序列的时候,out=1,否则out=0 (1)给出状态编码,画出状态图 (2)门电路实现 (3)verilog实现 首先规定Q3Q ...

  6. 1)PHP,数据库操作类网址

    (1)脚本之家 http://www.jb51.net/article/94347.htm (2)一个博客 http://www.cnblogs.com/lvchenfeng/p/5003629.ht ...

  7. auto uninstaller (autodesk 修复大师) 简体中文版 更新下载地址

    小伙伴是不是遇到 CAD/3dmax/maya/Revit/Inventor 安装失败或者安装不了的问题了呢?AUTODESK系列软件着实令人头疼,CAD/3dmax/maya/Revit/Inven ...

  8. Jumpserver 一键部署(支持离线安装)

    1.教程介绍1.1::通过本教程起到抛砖引玉效果,希望各位喜爱Jumpserver堡垒机的朋友受益良多. 1.2::以下提供的任何软件仅供学习交流使用. 2.下载链接2.1::centos_1810最 ...

  9. spring事务管理(xml配置)与spring自带连接数据库JdbcTemplate

    什么是事务,很通俗的话来说就是,我们日常生活中总会出现在银行转账的业务,加入A向B转账100元,此时A的账户中应该减少100元,B的账户中增加100元,但是如果在A转完账B还没有接受的时候,服务器出现 ...

  10. spring顾问包装通知

    前边说到了顾问就是通知,没有实践,这里就实践一下,证明一下. 虽然可以说顾问就是通知,但是他们还是有的一定的区别的,通知是将目标类中所有的方法都进行的增强,而顾问却可以指定到特定的方法上,也就是说顾问 ...