WebServer/ApplicationServer分散在各个机器上,然而咱们依旧想在Hadoop平台上进行统计分析,如何将日志收集到Hadoop平台呢?html
shell cp hadoop集群的机器上;
hadoop fs -put ... /复制代码
显然该法面临着容错、负载均衡、高延迟、数据压缩等一系列问题这显然已经没法知足需求了!java
不如问问神奇的Flume呢???node
只须要配置文件,轻松解决以上问题!web
其余的还有好比:shell
顺便来看看官方文档apache
指定数据源(Avro, Thrift, Spooling, Kafka, Exec)服务器
把数据暂存(Memory, File, Kafka等用的比较多)网络
把数据写至某处(HDFS, Hive, Logger, Avro, Thrift, File, ES, HBase, Kafka等)架构
为了跨多个代理或跳数据流,先前代理的接收器和当前跳的源须要是avro类型,接收器指向源的主机名(或IP地址)和端口。负载均衡
日志收集中很是常见的状况是大量日志生成客户端将数据发送到链接到存储子系统的少数消费者代理。 例如,从数百个Web服务器收集的日志发送给写入HDFS集群的十几个代理。这能够经过使用avro接收器配置多个第一层代理在Flume中实现,全部这些代理都指向单个代理的avro源(一样,您能够在这种状况下使用thrift源/接收器/客户端)。 第二层代理上的此源将接收的事件合并到单个信道中,该信道由信宿器消耗到其最终目的地。
Flume支持将事件流多路复用到一个或多个目的地。 这是经过定义能够复制或选择性地将事件路由到一个或多个信道的流复用器来实现的。上面的例子显示了来自代理“foo”的源代码将流程扩展到三个不一样的通道。 扇出能够复制或多路复用。 在复制流的状况下,每一个事件被发送到全部三个通道。 对于多路复用状况,当事件的属性与预配置的值匹配时,事件将被传递到可用通道的子集。 例如,若是一个名为“txnType”的事件属性设置为“customer”,那么它应该转到channel1和channel3,若是它是“vendor”,那么它应该转到channel2,不然转到channel3。 能够在代理的配置文件中设置映射。
export FLUME_VERSION=1.9.0
export FLUME_HOME=/usr/local/Cellar/flume/1.9.0/libexec
export FLUME_CONF_DIR=$FLUME_HOME/conf
export PATH=$FLUME_HOME/bin:$PATH复制代码
安装成功
看看官网的第一个案例
# example.conf: A 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 = 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复制代码
a1:agent名称r1:Source名称k1:Sink名称c1:Channel名称
看看其中的
相似于netcat的源,它侦听给定端口并将每行文本转换为事件。 像nc -k -l [host] [port]这样的行为。 换句话说,它打开一个指定的端口并侦听数据。 指望是提供的数据是换行符分隔的文本。 每行文本都转换为Flume事件,并经过链接的通道发送。
必需属性以粗体显示。
在INFO级别记录事件。 一般用于测试/调试目的。 必需属性以粗体显示。 此接收器是惟一的例外,它不须要在“记录原始数据”部分中说明的额外配置。
事件存储在具备可配置最大大小的内存中队列中。 它很是适用于须要更高吞吐量的流量,而且在代理发生故障时准备丢失分阶段数据。 必需属性以粗体显示。
在conf目录下
使用名为flume-ng
的shell脚本启动代理程序,该脚本位于Flume发行版的bin目录中。 您须要在命令行上指定代理名称,config目录和配置文件:
bin/flume-ng agent -n $agent_name -c conf -f conf/flume-conf.properties.template复制代码
bin/flume-ng agent \
--name a1 \
--conf $FLUME_HOME/conf \
--conf-file $FLUME_HOME/conf/example.conf \
-Dflume.root.logger=INFO,console复制代码
如今,代理将开始运行在给定属性文件中配置的源和接收器。
telnet 127.0.0.1 44444复制代码
2019-06-12 17:52:39,711 (SinkRunner-PollingRunner-DefaultSinkProcessor)
[INFO - org.apache.flume.sink.LoggerSink.process(LoggerSink.java:95)]
Event: { headers:{} body: 4A 61 76 61 45 64 67 65 0D JavaEdge. }复制代码
其中的Event是Fluem数据传输的基本单元Event = 可选的header + byte array
Exec源在启动时运行给定的Unix命令,并指望该进程在标准输出上连续生成数据(stderr被简单地丢弃,除非属性logStdErr设置为true)。 若是进程因任何缘由退出,则源也会退出而且不会生成其余数据。 这意味着诸如cat [named pipe]或tail -F [file]之类的配置将产生所需的结果,而日期可能不会 - 前两个命令产生数据流,然后者产生单个事件并退出
exec source + memory channel + logger sink
# example.conf: A 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 /Volumes/doc/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
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复制代码
在conf下新建配置文件以下:
exec s + memory c + avro savro s + memory c + loger s
exec-memory-avro.conf
# 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 /Volumes/doc/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 = localhost
exec-memory-avro.sinks.avro-sink.port = 44444
# Use a channel which buffers events in memory
exec-memory-avro.channels.memory-channel.type = memory
exec-memory-avro.channels.memory-channel.capacity = 1000
exec-memory-avro.channels.memory-channel.transactionCapacity = 100
# 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复制代码
# 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 /Volumes/doc/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 = localhost
exec-memory-avro.sinks.avro-sink.port = 44444
# Use a channel which buffers events in memory
exec-memory-avro.channels.memory-channel.type = memory
exec-memory-avro.channels.memory-channel.capacity = 1000
exec-memory-avro.channels.memory-channel.transactionCapacity = 100
# 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复制代码
https://tech.meituan.com/2013/12/09/meituan-flume-log-system-architecture-and-design.html