大数据(9)

Flume简介 --(实时抽取数据的工具)

1) Flume提供一个分布式的,可靠的,对大数据量的日志进行高效收集、聚集、移动的服务,Flume只能在Unix环境下运行。

2) Flume基于流式架构,容错性强,也很灵活简单。

3) Flume、Kafka用来实时进行数据收集,Spark、Storm用来实时处理数据,impala用来实时查询。

Flume角色

1、Source

用于采集数据,Source是产生数据流的地方,同时Source会将产生的数据流传输到Channel,这个有点类似于Java IO部分的Channel。

2、Channel

用于桥接Sources和Sinks,类似于一个队列。

3、Sink

从Channel收集数据,将数据写到目标源(可以是下一个Source,也可以是HDFS或者HBase)。

4、Event

传输单元,Flume数据传输的基本单元,以事件的形式将数据从源头送至目的地。

Flume传输过程

source监控某个文件或数据流,数据源产生新的数据,拿到该数据后,将数据封装在一个Event中,并put到channel后commit提交,channel队列先进先出,sink去channel队列中拉取数据,然后写入到HDFS中。

Flume部署及使用

1.下载并解压到指定目录

$ tar -zxf ~/softwares/installtions/apache-flume-1.7.0-bin.tar.gz -C ~/modules/

  

2.重命名文件,且修改配置文件。

cd /home/admin/modules/apache-flume-1.7.0-bin/conf

将所有带有template的文件去掉template

修改配置文件 flume-env.sh
(只需修改你对应的java安装路径即可)

export JAVA_HOME=/home/admin/modules/jdk1.8.0_131

  

使用案例

案例一 :端口数据监控

1.先安装telnet,telnet rpm包 下载

$ sudo rpm -ivh xinetd-2.3.14-40.el6.x86_64.rpm
$ sudo rpm -ivh telnet-0.17-48.el6.x86_64.rpm
$ sudo rpm -ivh telnet-server-0.17-48.el6.x86_64.rpm

  

2.创建案例一的文件夹,与配置文件

在Flume的根目录创建对应的文件夹

mkdir -p jobs/job1

  

3.创建项目对应的配置文件

cd /home/admin/modules/apache-flume-1.7.0-bin/jobs/job1

vim 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

  

4.判断44444端口是否被占用

$ sudo netstat -tunlp | grep 44444

  

5.先开启flume先听端口

在Flume的根目录下

$ bin/flume-ng agent --conf conf/ --name a1 --conf-file jobs/job1/flume-telnet.conf -Dflume.root.logger==INFO,console

  

6.使用telnet工具向本机的44444端口发送内容

克隆会话

$ telnet localhost 44444

然后随便输入点东西看Flume是否能够监看到发送的内容

  

案例二 :hive日志监控

 1.下载并将hadoop jar包拉到flume的lib目录下

http://flume.apache.org/ 官方文档

cp ~/softwares/installtions/flume-hadoop-jar/* ~/modules/apache-flume-1.7.0-bin/lib/

  

2.创建项目二的文件夹与配置文件

mkdir jobs/job2

vim jobs/job2/flume-hdfs.conf

# Name the components on this agent
a2.sources = r2
a2.sinks = k2
a2.channels = c2
# Describe/configure the source
a2.sources.r2.type = exec
a2.sources.r2.command = tail -F /home/admin/modules/apache-hive-1.2.2-bin/hive.log
a2.sources.r2.shell = /bin/bash -c

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

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

# Bind the source and sink to the channel
a2.sources.r2.channels = c2
a2.sinks.k2.channel = c2

  

3.修改hive log 日志上传目录

vim /home/admin/modules/apache-hive-1.2.2-bin/conf/hive-log4j.properties 

hive.log.dir=/home/admin/modules/apache-hive-1.2.2-bin/logs

在hive的根目录创建logs文件夹

mkdir logs

  

4.启动案例2的flume任务

bin/flume-ng agent --conf conf/ --name a2 --conf-file jobs/job2/flume-hdfs.conf

  

5.新开会话,在新会话中启动hive。成功的话,hive的日志已经存到了hdfs上面的flume文件夹里面了

原文地址:https://www.cnblogs.com/shifu204/p/9644872.html