是一个日志采集框架:把数据从各种数据源采集汇总传输的各种的目的地
我们不生产数据,我们只是数据的搬运工
Source+Channel+Sink=Agent
1.配置source
2.配置channel
3.配置 sink
4.连接各个组件
采集存储 推送
flume的配置文件:
1.这些的意义就是定义source sinks channels 的变量
a1.sources=r1
a1.sinks=k1
a.channels=c1
2。这个source 在今天网络上localhost:44444的数据
a1.sources.r1.type=netcat. 表示source的类型: nectar:网猫 监听指定IP和端口的udp和tcp数据
a1.sources.r1.bind=localhost
a1.sources.r1.port=44444
3.
表示以日志的形式输出(这里的日志输出是根据config目录下的log4j.properties中配置的目录下)
a1.sinkls.k1.type=logger
4.
通道。表示在内存中流过。
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000#运行的最多容量 1000条
a1.channels.c1.transactionCapacity=100 #一批允许提交的最大数据量。(事物相关的)
5. 连线
a1.sources.r1.channels=c1
a1.sinks.k1.channel=c1
这里的a1表示Agent的名字
启动:
bin/flume-ng agent —name agent1 —conf-file conf/example.conf —conf conf/ -Dflume.root.logger=INFO,console -Dflume.monitoring.type=http -Dflume.monitoring.port=34343 &
—1.9.0的版本这个好使
bin/flume-ng agent --conf ./conf/ -f conf/flume.conf -Dflume.root.logger=DEBUG,console -n agent1
这句启动命令的意思就是:根据配置参数。example.con 启动一个名字 为 agent1 的agent. 并且将日志输出到控制台。同时还坚挺一个http的协议 这个地址为34343
Apache Flume是一个分布式,可靠且可用的系统,用于有效地从许多不同的源收集,聚合和移动大量日志数据到集中式数据存储。
Apache Flume的使用不仅限于日志数据聚合。 由于数据源是可定制的,因此Flume可用于传输大量事件数据,包括但不限于网络流量数据,社交媒体生成的数据,电子邮件消息以及几乎任何可能的数据源。
环境的要求:
Java运行时环境 - Java 1.8或更高版本
内存 - 源,通道或接收器使用的配置的足够内存
磁盘空间 - 通道或接收器使用的配置的足够磁盘空间
目录权限 - 代理使用的目录的读/写权限
Event:数据传输的最小单位:
Source:
作用:
AvroSource:监听一个avro服务端口,采集Avro数据序列化后的数据
ThriftSource:监听一个Thrift服务端口,采集Thrift数据序列化后的数据
ExecSource:基于Unix的commond在标准输出上采集数据
JMSSource: java 消息服务数据源,java 消息服务是一个与具体平台无关的API,这是支持JMS规范的数据源采集
Spooling DirectorySource:通过文件夹里的新增的文件作为数据源的采集
Kafka Source:从Kafka服务中采集数据
NetCat Source:绑定的端口(tcp、udp),将流经端口的每一个文本行数据作为Event输入
HttpSource:监听Http Post和GET产生的数据的采集
Channel:
作用:
Memory Channel:使用内存作为数据的存储
JDBC Channel:使用JDBC数据源来作为数据的存储
Kafka Channel:使用Kafka服务来作为数据的存储
FileChannel:使用文件来作为数据的存储
Spillable Memory Channel:使用内存和文件作为数据的存储,即先存在内存中,如果内存中数据达到法治则flush到文件中
Sink:
作用:数据的最终目的地
接受channel写入的数据以指定的形式展现出来(或存储或展示)。
sink的表现形式很多:比如:打印到控制台、hdfs、Avro服务中,文件中等
常见采集的数据类型:
HDFS sink、HiveSink、LoggerSink、AvroSink、ThriftSink、FileRollSink、HBaseSink、KafkaSink、ElasticSearchSink、MorphlineSoler等
Event :
作用:
是FlumeNG传输的数据的基本单位,也是事务的基本单位:
在文本文件中,通常是一行记录就是一个Event
在网络消息的传输系统中,一条消息就是一个Event
event里面包含header和body
Event里面的heard类型。 Map<String,String>
我们也可以在Source中自定义heard中的Key:Value,在某些channel和sink中使用heard
怎样实时监听一个文件中的数据的增加呢?
如果这个文件增加的量特别大呢?
Define a memory channel called ch1 on agent1
agent1.channels.ch1.type = memory
Define an Avro source called avro-source1 on agent1 and tell it
to bind to 0.0.0.0:41414. Connect it to channel ch1.
agent1.sources.avro-source1.channels = ch1
agent1.sources.avro-source1.type = exec
agent1.sources.avro-source1.command = tail -f /Users/huyong/App/logs/test.log
agent1.sources.avro-source1.port = 41414
Define a logger sink that simply logs all events it receives
and connect it to the other end of the same channel.
agent1.sinks.log-sink1.channel = ch1
agent1.sinks.log-sink1.type = logger
Finally, now that we’ve defined all of our components, tell
agent1 which ones we want to activate.
agent1.channels = ch1
agent1.sources = avro-source
agent1.sinks = log-sink1
Jps -v 查看java 进程使用的内存参数
Agent 的启动方式:
RPC:远程过程调用
Avro:是序列化的一种方式,他表示将数据序列化的时候使用Avro这中方式序列化
使用Avro启动的必须使用Avro客户端才能通信
使用Avro客户端将信息发送过去:
bin/flume-ng avro-client --conf conf/ --host 127.0.0.1 --port 41414 --filename /Users/huyong/App/logs/test.log
这句话的意思就是将 /Users/huyong/App/logs/test.log 文件的数据 通过avro序列化的方式发送到127.0.0.1:41414的主机上
flume的高级组件 (共三个)
1.拦截器:(针对source)
主要作用:对于一个source可以指定一个或多个拦截器,对数据进行处理
2.选择器:(针对于Channel)
如果配置多个channel 每次数据都为发送
a1.sources = r1
a1.channels = c1 c2 c3 c4
a1.sources.r1.selector.type = multiplexing
a1.sources.r1.selector.header = state
a1.sources.r1.selector.mapping.CZ = c1
a1.sources.r1.selector.mapping.US = c2 c3
a1.sources.r1.selector.default = c4
有4选择器
swithc(state){
case CZ:
channels=ca
case US:
channels=c2 c3
default :
channels=c4
}
3.sink 负载均衡、故障转移
一个channel发给2个sink
a1.sinkgroups = g1
a1.sinkgroups.g1.sinks = k1 k2
a1.sinkgroups.g1.processor.type = load_balance
a1.sinkgroups.g1.processor.backoff = true
a1.sinkgroups.g1.processor.selector = random
负载均衡
使用Java代码将日志直接打到Avrosource中:
添加依赖:
org.apache.flume.flume-ng-clients flume-ng-log4jappender 1.9.0这种写法只支持Java版本,如果是C的需要需要使用tail -f 监听对文件的增加
自定义Sink: