大数据实战之分布式日志收集框架Flume

如何解决我们的数据从其他的server上移动到Hadoop之上????\
1)shell脚本 cp hadoop集群的机器上,然后上传到hdfs  hadoop fs -put ....
    缺点:如何做监控,如果有机器宕机怎么办    时效性不好    网络IO和磁盘IO开销很大    容错和负载均衡
    
2)flume 通过配置文件可以解决大部分业务场景的应用,还可以结合shell脚本

flume概述:
webserver(源端)    ===>    flume    ===>    hdfs(目的地)

设计目标:
    可靠性
    扩展性
    管理性
    
业界同类产品的对比:
    (***)flume:    Cloudera/Apache     Java开发
    Scribe:    Facebook    C/C++开发    使用简单,但容错性不太好,已经停止维护
    Chukwa:    Yahoo/Apache Java开发,负载均衡不行    已经停止维护
    kafka:    消息队列,分布式的消息系统,起到数据缓冲的作用,和flume的作用不一样,两者不是同一领域的
    fluentd:    和flume类似,用Ruby开发
    (***)LogStash:ELK中的日志采集工具    E:ElasticSearch K:Kibana
    
    
flume架构和核心组件:
1)Source    收集

2)Channel    聚集

3)Sink        输出

flume安装的前置条件:
1):jdk1.7+
2):内存空间足够
3):磁盘空间足够
4):文件的权限    agent必须要有文件的读写权限

flume的安装配置
    下载    解压    配置Java的环境变量到flume-env.sh 
    检测安装是否成功:    flume-ng version
    
flume的配置文件:
使用flume的关键就是写配置文件
A):    配置Source
B):    配置Channel
C):    配置Sink
D):    把以上三个组件串起来需求一:从指定网络端口(44444)采集数据输出到控制台
配置flume配置文件:    example.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 = hadoop000
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

启动flume

$ bin/flume-ng agent    \    ------调用flume-ng脚本的agent命令
 --name a1 \    ------指定此次job的agent的名称
 --conf $FLUME_HOME/conf    \    ------指定当前flume的配置文件的目录
 --conf-file $FLUME_HOME/conf/job_flume_netcat.conf \    ------指定此次job的配置文件
 -Dflume.root.logger==INFO,console    ------flume日志的输出级别和输出位置

bin/flume-ng agent --name a1 --conf conf --conf-file conf/example.conf -Dflume.root.logger==INFO,console 

重开一个窗口,telnet指定端口,输入数据提供给flume采集:

telnet hadoop000 44444 然后输入内容

分析:
Event: { headers:{} body: 68 65 6C 6C 6F 0D                               hello. }

每次输入一个数据,都会生成一个    Event,在flume中数据传输的基本单元就是Event
Event=可选的header + byte array(68 65 6C 6C 6F)需求二:监控一个文件实时采集新增的数据输出到控制台
agent选型:    exec source    +    memory channel +    logger sink
编辑配置文件,需要变动的地方很少:
exec-memory-logger.conf:

# 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 /home/hadoop/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

启动命令:

bin/flume-ng agent --name a1 --conf conf --conf-file conf/exec-memory-logger.conf -Dflume.root.logger==INFO,console

需求三:将A服务器上的日志实时采集到B服务器
工作中一般跨界点采集数据,都是用的avro sink 
技术选型,需要用到两个agent
exec source +    memory channel +    avro sink 
avro source +    memory channel +    logger sink

 exec-memory-avro.conf:

exec-memory-avro.sources = exec-source
exec-memory-avro.sinks = avro-sink
exec-memory-avro.channels = memory-channel

exec-memory-avro.sources.exec-source.type = exec
exec-memory-avro.sources.exec-source.command = tail -F /home/hadoop/data/data.log
exec-memory-avro.sources.exec-source.shell = /bin/sh -c

exec-memory-avro.sinks.avro-sink.type = avro
exec-memory-avro.sinks.avro-sink.hostname = hadoop000
exec-memory-avro.sinks.avro-sink.port = 44444

exec-memory-avro.channels.memory-channel.type = memory

exec-memory-avro.sources.exec-source.channels = memory-channel
exec-memory-avro.sinks.avro-sink.channel = memory-channel


编辑配置文件avro-memory-logger.conf:

avro-memory-logger.sources = avro-source
avro-memory-logger.sinks = logger-sink
avro-memory-logger.channels = memory-channel

avro-memory-logger.sources.avro-source.type = avro
avro-memory-logger.sources.avro-source.bind = hadoop000
avro-memory-logger.sources.avro-source.port = 44444

avro-memory-logger.sinks.logger-sink.type = logger

avro-memory-logger.channels.memory-channel.type = memory

avro-memory-logger.sources.avro-source.channels = memory-channel
avro-memory-logger.sinks.logger-sink.channel = memory-channel

注意:
要先启动avro-memory-logger,进行监听:

bin/flume-ng agent --name avro-memory-logger --conf conf --conf-file conf/avro-memory-logger.conf -Dflume.root.logger==INFO,console

再启动被监听的机器:

bin/flume-ng agent --name exec-memory-avro --conf conf --conf-file conf/exec-memory-avro.conf -Dflume.root.logger==INFO,console

日志收集过程:
1):机器A上监控一个文件,当我们访问主站时,会有用户行为日志,记录到access.log中
2):avro-sink把新产生的日志输出到对应的avro source 指定的hostname和port上
3):通过avro source 对应的agent将我们的日志输出到对应的控制台(其实是kafka)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值