前期准备

了解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. ZJNU 1153 - 找单词——中级

    状态转移b[i]记录价值为i的单词种类数d[j+k*i]+=b[j] , k<=a[i]&&j+k*i<=50表示价值为j+k*i的单词可以由价值为j的单词加上k个i字母转 ...

  2. python常见内置函数总结

    简单的内置函数 len    求长度 min   求最小值 max  求最大值 sorted  排序 reversed   反向 sum   求和 进制转换 bin   转为二进制 oct   转为八 ...

  3. vim 复制 单个 单词: 移动光标到单词词首,快速摁 yw

    vim 复制 单个 单词:   移动光标到单词词首,快速摁 yw

  4. script和scriptreplay_超骚气的实时监控你的服务器

    今天看到一个超级叼的linux命令,可以完整记录屏幕上的命令与输出结果. 有人问这有什么叼的,不就是保存历史操作记录吗?我看看日志也能看出来. 不不不,我要说的“完整记录”包括第几秒执行什么命令,就像 ...

  5. [USACO09OCT]谷仓里的回声Barn Echoes(hush、STL)

    https://www.luogu.org/problem/P2957 题目描述 The cows enjoy mooing at the barn because their moos echo b ...

  6. RPM包和YUM仓库管理

    1.RPM包管理 RPMRPM Package Manger,前身Redhat Package Manger,由红帽开发用于软件包的安装升级卸载与查询有一个完整的数据库体系,每个RPM包的所有信息都固 ...

  7. hybrid|Conform the norm of|Mollusk|uncanny|canny|Canvas|documentary

    hybrid混合物 Conform the norm of 符合规范 Mollusk贝类 uncanny诡异的 canny精明的 Canvas帆布 documentary纪录片  

  8. VS工程的相对路径写法

    最近搭建一个VS工程的框架,为了让所有人都能直接使用,要使用相对路径,下面的几种常见路径的写法: 1.两个点“..\”表示在工程文件(*.vcxproj)的上一级目录. 2.一个点“.\”则表示和工程 ...

  9. 基于 maven 的ssm 框架搭建

    1.新建一个 maven 工程, war 包 2.引入 pom 文件(springmvc+spring+mybatis) 3.引入配置文件 4.引入页面,编写 contorller 层测试 5.编写查 ...

  10. 关于dubbo接口性能测试

      最初的压测这个dubbo接口有三种思路: .第一种就是基于业务,比如注册业务,注册成功后,会发送短信消息到用户手机,通过业务调用消息服务,最容易实现,但是业务瓶颈最大导致测试结果不准 .第二种是通 ...