Flume1 初识Flume和虚拟机搭建Flume环境

前言:html

      工做中须要同步日志到hdfs,之前是找运维用rsync作同步,如今通常是用flume同步数据到hdfs。之前为了工做简单看个flume的一些东西,今天下午有时间本身利用虚拟机搭建了flume环境,并简单作了几个练习。学习过程当中仍是比较顺利的,如今将学习的过程记录与此,供之后本身查阅,若是能帮助到其余人,天然是更好的。node

===============================================================长长的分割线====================================================================apache

正文:服务器

  关于flume的理论介绍,网上能够搜到到不少的资料,你们能够自行搜索,我这里就不在重复赘述。运维

      本文中主要涉及三块内容: 第一,fume概念简介;第二,搭建flume环境并运行hello word;第三,在第二点的基础上,再介绍一种“源”的使用方式。工具

 

  第一步,flume简介(这部分资料参考了网上文章的资料):学习

    (1). flume基本组件            测试

   a. Event:消息的基本单位,由headers和body组成
   b. Agent:JVM进程,负责将外部来源产生的消息转发到外部的目的地 
    • Source:从外部来源读入event,并写入channel 
    • Channel:event暂存组件,source写入后,event将会一直保存,直到被sink成功消费。 
    • Sink:从channel读入event,并写入目的地this

      (2). flume数据流,对照下面的两幅数据流图片,须要咱们记住以下概念:spa

       a. 源将事件写到一个或多个通道中

       b. 通道做为事件从源到接收器传递的保留区

       c. 接收器只从一个通道接收事件

       d. 代理可能会有多个源、通道与接收器。

      

       

    

      第二步,搭建flume环境并运行hello world:

      (1). 从flume官网上下载 http://flume.apache.org/download.html  flume安装包,能够下载源码包本身编译,可是我当初直接下载了编译好的apache-flume-1.5.2-bin.tar.gz。

      (2). 将apache-flume-1.5.2-bin.tar.gz这个压缩包经过tar -xzvf apache-flume-1.5.2-bin.tar.gz 命令解压缩。为了便于后边的讲解说一下我解压缩后的目录: /myself_settings/flume1.5.2/apache-flume-1.5.2-bin

      (3). 直接进入conf目录,配置example0001.conf 配置文件,配置内容以下: 咱们定义了一个名叫agent1的Agent。其中包含了名为src1的源、名为channel1的通道以及名为sink1的接收器。

 1 # example0001.conf: A single-node Flume Configuration
 2 
 3 # Name the components on this agent
 4 agent1.sources = src1
 5 agent1.sinks = sink1
 6 agent1.channels = channel1
 7 
 8 # Describe/configure the source
 9 agent1.sources.src1.type = netcat
10 agent1.sources.src1.bind = 127.0.0.1
11 agent1.sources.src1.port = 44444
12 
13 #Describe the sink
14 agent1.sinks.sink1.type = logger
15 
16 # Use a channel which buffers events in memory
17 agent1.channels.channel1.type = memory
18 agent1.channels.channel1.capacity = 1000
19 agent1.channels.channel1.transactionCapacity = 100
20 
21 # Bind the source and sink to the channel
22 agent1.sources.src1.channels = channel1
23 agent1.sinks.sink1.channel = channel1 

      (4). 在安装目录的根目录,好比个人目录: /myself_settings/flume1.5.2/apache-flume-1.5.2-bin  执行命令: ./bin/flume-ng agent -n agent1 -c conf -f conf/example0001.conf -Dflume.root.logger=INFO,console ,启动成功以下图:

     

   (5). 在启动成功后,咱们执行telnet命令: telnet localhost 44444,正确链接telnet后,依次输入hello、hello world和hello flume,这时咱们能够看到刚才启动agent的控制台中就成功接收并输出了你刚才输入的内容,详细看上图矩形框选中的部分。

 

      第三步,exec源的使用。这个主要是若是我想实时的接收业务系统的日志,那么能够设置这种源。

   (1). 直接进入conf目录,配置example0002.conf 配置文件,配置内容以下: 咱们定义了一个名叫agent2的Agent。其中包含了名为src2的源、名为channel2的通道以及名为sink2的接收器。

 1 # example0001.conf: A single-node Flume Configuration
 2 
 3 # Name the components on this agent
 4 agent2.sources = src2
 5 agent2.sinks = sink2
 6 agent2.channels = channel2
 7 
 8 # Describe/configure the source
 9 agent2.sources.src2.type = exec
10 agent2.sources.src2.command = tail -F /test/test.log
11 
12 #Describe the sink
13 agent2.sinks.sink2.type = logger
14 
15 # Use a channel which buffers events in memory
16 agent2.channels.channel2.type = memory
17 agent2.channels.channel2.capacity = 1000
18 agent2.channels.channel2.transactionCapacity = 100
19 
20 # Bind the source and sink to the channel
21 agent2.sources.src2.channels = channel2
22 agent2.sinks.sink2.channel = channel2 

      (2). 结合上面的配置文件,咱们会发现和咱们以前的定义的example1.conf的例子不一样主要在与九、10行标红的定义源的类型和方式。结合命令来讲,就是若是/test/test.log文件中内容发生变化,那么就会把新增的数据传入到当前agent2中。

      (3). 为了测试上边的这个例子,在启动agent2以前,咱们定义一个crontab任务:  */1 * * * * date >> /test/test.log ,每隔一分钟往/test/test.log中插入一条当前服务器的时间。 

      (4). 在上边crontab任务执行后,咱们开始启动agent2:  ./bin/flume-ng agent -n agent2 -c conf -f conf/example0002.conf -Dflume.root.logger=INFO,console。

      (5). 随着每隔一分钟test.log中新增的一条时间记录,agent2的控制台也会相应的接收并输出一条记录,以下图:

        

       

 

      综上所述,咱们一块儿完成了对flume的初步认识,我我的任务若是你是开发,这些基本的了解对你来讲是必要的,固然若是从这篇文章中看,貌似flume比较简单,可是我我的以为单看flume确实没有太多可说的,可是咱们若是把kafka、flume、storm等实时计算工具融合起来的话,仍是要好好研究研究的。

相关文章
相关标签/搜索