Flume实现读取文件

一、需求分析

Flume实现读取文件_第1张图片

二、上传jar包

因为本案例要读到hdfs里,所以,就要hadoop相关jar包支持。

将commons-configuration-1.6.jar、hadoop-auth-2.7.2.jar、hadoop-common-2.7.2.jar、hadoop-hdfs-2.7.2.jar、commons-io-2.4.jar、htrace-core-3.1.0-incubating.jar拷贝到/opt/module/flume/lib文件夹下。注:标红的为测试版本必须引入的jar

具体jar怎么找呢?其实可以百度即可。

三、配置文件

1、创建文件:$ touch flume-file-hdfs.conf

2、编辑内容如下

其中里面比较重要的几点有: ①在追加文件tail -F是大写的F不是小写,大写的意思是在关闭以后,下次接着重启,而-f是关闭就关闭了。②记得指定shell = /bin/bash -c,指定是linux的类型。③sink的配置可以参考flume的文档

# 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 /opt/module/hive/logs/hive.log
a2.sources.r2.shell = /bin/bash -c

# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://hadoop102:9000/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
#设置每个文件的滚动大小  128M
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、执行

$ bin/flume-ng agent  -n  a1  -c  conf/  -f    job/flume-local-dfs.conf  

-n就是相当于name,-c---conf,f---file

4、在hdfs中查看即可

 

 

 

你可能感兴趣的:(大数据学习,大数据生态技术)