上一节介绍了Flume的一个入门案例,本节博主将分享Flume如何将数据收集到hdfs文件系统上。

    操作步骤:

#1、进入Flume配置目录
cd ~/apps/apache-flume-1.6.0-bin/conf

#2、新建配置文件
vi tail-hdfs.conf

#3、在配置文件中添加如下内容
# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

#exec 指的是命令
# Describe/configure the source
a1.sources.r1.type = exec
#F根据文件名追中, f根据文件的nodeid追中
a1.sources.r1.command = tail -F /home/hadoop/log/test.log
a1.sources.r1.channels = c1

# Describe the sink
#下沉目标
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
#指定目录, flum帮做目的替换
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/
#文件的命名, 前缀
a1.sinks.k1.hdfs.filePrefix = events-

#10 分钟就改目录
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute

#文件滚动之前的等待时间(秒)
a1.sinks.k1.hdfs.rollInterval = 3

#文件滚动的大小限制(bytes)
a1.sinks.k1.hdfs.rollSize = 500

#写入多少个event数据后滚动文件(事件个数)
a1.sinks.k1.hdfs.rollCount = 20

#5个事件就往里面写入
a1.sinks.k1.hdfs.batchSize = 5

#用本地时间格式化目录
a1.sinks.k1.hdfs.useLocalTimeStamp = true

#下沉后, 生成的文件类型,默认是Sequencefile,可用DataStream,则为普通文本
a1.sinks.k1.hdfs.fileType = DataStream

# 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、保存上面配置
shift+Z+Z

5、创建Flume监听的文件所在的文件夹
mkdir /home/hadoop/log

6、创Flume监听的文件,并写循环写入数据
while true
do
echo 111111 >> /home/hadoop/log/test.log
sleep 0.5
done

7、新打开个ssh客户端执行下列命令查看日志文件变化【使用大写的-F是追踪文件名进行输出,而小写-f是inode进行追踪】
tail -F test.log

8、启动hdfs
start-dfs.sh

9、检查下hdfs式否是salf模式:
hdfs dfsadmin -report

10、启动Flume
bin/flume-ng agent -c conf -f conf/tail-hdfs.conf -n a1

11、前端页面查看下, http://centos-aaron-h1:50070, 文件目录: /flume/events/19-02-14

12、使用hdfs命令查看
hdfs dfs -ls /flume/events/19-02-14
hdfs dfs -cat /flume/events/19-02-14/0710/events-.1550099967340

    参考地址:http://flume.apache.org/releases/content/1.6.0/FlumeUserGuide.html

    补充知识:

    linux中的软连接:本质上就是个文件相当于windows中新建了一个快捷方式。rm ... 只删除软连接,不会删除真实文件;rm -rf ...会删除软连接,同时也会删除真实文件。

    linux中的硬连接:本质上是新建一个引用,需要将所有引用删除才会真实文件。     

    最后寄语,以上是博主本次文章的全部内容,如果大家觉得博主的文章还不错,请点赞;如果您对博主其它服务器大数据技术或者博主本人感兴趣,请关注博主博客,并且欢迎随时跟博主沟通交流。

02-14 04:40