Flume(日志采集工具)

Flume框架基础:
* 理性认知:
1、Flume在集群中扮演的角色
Flume、Kafka用来实时进行数据收集,Spark、Storm用来实时处理数据,impala用来实时查询。
2、Flume框架简介
1.1 Flume提供一个分布式的,可靠的,对大数据量的日志进行高效收集、聚集、移动的服务,Flume只能在Unix环境下运行
1.2 Flume基于流式架构,容错性强,也很灵活简单,主要用于在线实时分析。
1.3 角色
** Source
用于采集数据,Source是产生数据流的地方,同时Source会将产生的数据流传输到Channel,这个有点类似于Java IO部分的Channel
** Channel
用于桥接Sources和Sinks,类似于一个队列。
** Sink
从Channel收集数据,将数据写到目标源(可以是下一个Source,也可以是HDFS或者HBase)
1.4 传输单元
** Event
Flume数据传输的基本单元,以事件的形式将数据从源头送至目的地
1.5 传输过程
source监控某个文件,文件产生新的数据,拿到该数据后,
将数据封装在一个Event中,并put到channel后commit提交,
channel队列先进先出,sink去channel队列中拉取数据,然后写入到hdfs或者HBase中。
3、安装配置FLume
3.1 flume-env.sh
export JAVA_HOME=
4、Flume帮助命令
bin/flume-ng -help
5、案例:

        5.1、案例一:Flume监听端口,输出端口数据。
            5.1.1、创建Flume Agent配置文件flume-telnet.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
            5.1.2、安装telnet工具
                $ sudo rpm -ivh telnet-server-0.17-59.el7.x86_64.rpm 
                $ sudo rpm -ivh telnet-0.17-59.el7.x86_64.rpm
            5.1.3、首先判断44444端口是否被占用
                $ netstat -an | grep 44444
            5.1.4、先开启flume先听端口
                $ bin/flume-ng agent --conf conf/ --name a1 --conf-file conf/flume-telnet.conf -Dflume.root.logger==INFO,console
            5.1.5、使用telnet工具向本机的44444端口发送内容。
                $ telnet localhost 44444

5.2、案例二:监听上传Hive日志文件到HDFS

5.2.1 拷贝Hadoop相关jar到Flume的lib目录下
     share/hadoop/common/lib/hadoop-auth-2.5.0-cdh5.3.6.jar
     share/hadoop/common/lib/commons-configuration-1.6.jar
     share/hadoop/mapreduce1/lib/hadoop-hdfs-2.5.0-cdh5.3.6.jar
     share/hadoop/common/hadoop-common-2.5.0-cdh5.3.6.jar
     5.2.2 创建flume-hdfs.conf文件
        # Name the components on this agent
        a2.sources=r2
        a2.sinks=k2
        a2.channels=c2
        #describe/configure the sources
       a2.sources.r2.type=exec
       a2.sources.r2.command=tail -f /opt/modules/hadoop-2.5.0/logs/hadoop-ch-datanode-hxzy01.com.log
       a2.sources.r2.shell=/bin/bash -c

       a2.sinks.k2.type=hdfs
       a2.sinks.k2.hdfs.path=hdfs://hxzy01.com/flume/%Y%m%d/%H
       a2.sinks.k2.hdfs.filePrefix=events-hadoop-
        a2.sinks.k2.hdfs.round=true
        a2.sinks.k2.hdfs.roundValue=1
        a2.sinks.k2.hdfs.roundUnit=hour
        a2.sinks.k2.hdfs.useLocalTimeStamp = true
        a2.sinks.k2.hdfs.batchSize =1000
        a2.sinks.k2.hdfs.fileType=DataStream
        a2.sinks.k2.hdfs.rollInterval=600
        a2.sinks.k2.hdfs.rollSize=134217700
        a2.sinks.k2.hdfs.rollCount=0
        a2.sinks.k2.hdfs.minBlockReplicas=1



        a2.channels.c2.type=memory
        a2.channels.c2.capacity=1000
        a2.channels.c2.transactionCapacity=100

        a2.sources.r2.channels=c2
        a2.sinks.k2.channel=c2

5.2.3、执行监控配置
$ bin/flume-ng agent –conf conf/ –name a2 –conf-file conf/flume-hdfs.conf

5.3、案例三:Flume监听整个目录

5.3.1 创建配置文件flume-dir.conf
$ cp -a flume-hdfs.conf flume-dir.conf
                    a3.sources = r3
                    a3.sinks = k3
                    a3.channels = c3

                    # Describe/configure the source
                    a3.sources.r3.type = spooldir
                    a3.sources.r3.spoolDir = /opt/modules/cdh/apache-flume-1.5.0-cdh5.3.6-bin/upload
                    a3.sources.r3.fileHeader = true
                    #忽略所有以.tmp结尾的文件,不上传
                    a3.sources.r3.ignorePattern = ([^ ]*\.tmp)

                    # Describe the sink
                    a3.sinks.k3.type = hdfs
                    a3.sinks.k3.hdfs.path = hdfs://192.168.122.20:8020/flume/upload/%Y%m%d/%H
                    #上传文件的前缀
                    a3.sinks.k3.hdfs.filePrefix = upload-
                    #是否按照时间滚动文件夹
                    a3.sinks.k3.hdfs.round = true
                    #多少时间单位创建一个新的文件夹
                    a3.sinks.k3.hdfs.roundValue = 1
                    #重新定义时间单位
                    a3.sinks.k3.hdfs.roundUnit = hour
                    #是否使用本地时间戳
                    a3.sinks.k3.hdfs.useLocalTimeStamp = true
                    #积攒多少个Event才flush到HDFS一次
                    a3.sinks.k3.hdfs.batchSize = 1000
                    #设置文件类型,可支持压缩
                    a3.sinks.k3.hdfs.fileType = DataStream
                    #多久生成一个新的文件
                    a3.sinks.k3.hdfs.rollInterval = 600
                    #设置每个文件的滚动大小
                    a3.sinks.k3.hdfs.rollSize = 134217700
                    #文件的滚动与Event数量无关
                    a3.sinks.k3.hdfs.rollCount = 0
                    #最小冗余数
                    a3.sinks.k3.hdfs.minBlockReplicas = 1


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

                    # Bind the source and sink to the channel
                    a3.sources.r3.channels = c3
                    a3.sinks.k3.channel = c3
5.3.2、执行测试
        $ bin/flume-ng agent --conf conf/ --name a3 --conf-file conf/flume-dir.conf &
            总结:
                在使用Spooling Directory Source
                注意事项:
                    1、不要在监控目录中创建并持续修改文件
                    2、上传完成的文件会以.COMPLETED结尾
                    3、被监控文件夹每600毫秒扫描一次变动。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值