一.定义
数据采集是大数据分析全流程的重要环节,典型的数据采集工具包括ETL工具、日志采集工具、数据迁移工具等。
Flume是一个高可用的、高可靠的、分布式的海量日志采集、聚合和传输的系统。
二.Flume 的安装配置
- 1.下载flume
wget http://mirror.bit.edu.cn/apache/flume/1.8.0/apache-flume-1.8.0-bin.tar.gz
- 2.创建flume目录
cd /opt/module
mkdir flume
- 3 把压缩文件解压到flume中
tar -zxvf apache-flume-1.8.0-bin.tar.gz -C /opt/module/flume
- 4 配置环境变量
vi /etc/profile
写入
export FLUME_HOME=/opt/module/flume/apache-flume-1.8.0-bin
export FLUME_CONF_DIR=$FLUME_HOME/conf
export PATH=$FLUME_HOME/bin:$PATH
使环境变量生效
source /etc/profile
- 5 配置flume的JAVA_HOME
cd /opt/module/flume/apache-flume-1.8.0-bin
cd conf
cp flume-env.sh.template flume-env.sh
vi flume-env.sh
写入
export JAVA_HOME=/opt/module/jdk1.8.0_121
- 6 查看flume的版本
cd /opt/module/flume/apache-flume-1.8.0-bin
./bin/flume-ng version
三.常见问题解决
- 1.Error: Could not find or load main class org.apache.flume.tools.GetJavaProperty
错误: 找不到或无法加载主类 org.apache.flume.tools.GetJavaProperty
这个问题是因为之前安装hbase导致的
解决方法
cd /opt/module/hbase-1.4.9/conf
vi hbase-env.sh
注释掉以下语句
# export HBASE_CLASSPATH=/opt/module/hbase/conf
重新执行查看版本那几条命令
问题解决
四. Flume的使用实例
示列1
- 1 创建avro.conf文件
cd /opt/module/flume/apache-flume-1.8.0-bin/conf
vi avro.conf
- 2 写入配置信息
a1.sources= r1
a1.sinks= k1
a1.channels= c1
#Describe/configure the source
a1.sources.r1.type=avro
a1.sources.r1.channels= c1
a1.sources.r1.bind= 0.0.0.0
a1.sources.r1.port= 4141
#Describe the sink
a1.sinks.k1.type=logger
# Use achannel which buffers events in memory
a1.channels.c1.type=memory
a1.channels.c1.capacity= 1000
a1.channels.c1.transactionCapacity= 100
# Bindthe source and sink to the channel
a1.sources.r1.channels= c1
a1.sinks.k1.channel= c1
- 3 执行命令
./bin/flume-ng agent -c . -f /opt/module/flume/apache-flume-1.8.0-bin/conf/avro.conf -n a1 -Dflume.root.logger=INFO,console
屏幕信息显示如下
- 4 打开另一个Linux终端
cd /opt/module/flume/apache-flume-1.8.0-bin/
touch log.00
sh -c 'echo “hello world” > /opt/module/flume/apache-flume-1.8.0-bin/log.00'
- 5 执行命令
./bin/flume-ng avro-client --conf conf -H localhost -p 4141 -F /opt/module/flume/apache-flume-1.8.0-bin/log.00
示列2
- 1 创建文件
cd /opt/module/flume/apache-flume-1.8.0-bin/
touch example.conf
- 2 写入
# 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
*3 执行下面命令
./bin/flume-ng agent --conf ./conf --conf-file ./example.conf --name a1 -Dflume.root.logger=INFO,console
- 4 打开另一个终端
输入
telnet localhost 44444
输入helloworld
查看原来终端
执行成功。