天天看点

flume案例--实时采集文件的内容变化到hdfs

1 配置tail-hdfs.conf文件

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F /root/logs/test.log

# Describe the sink
a1.sinks.k1.type = hdfs
a1.sinks.k1.channel = c1
a1.sinks.k1.hdfs.path = /flume/tailout/%y-%m-%d/%H-%M/
a1.sinks.k1.hdfs.filePrefix = itcast-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.rollInterval = 5
a1.sinks.k1.hdfs.rollSize = 1014
a1.sinks.k1.hdfs.rollCount = 3
a1.sinks.k1.hdfs.batchSize = 1
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
           

2 启动flume

bin/flume-ng agent -c conf -f conf/tail-hdfs.conf -n a1 -Dflume.root.logger=INFO,console
           

3 创建要监控的文件夹

mkdir -p /root/logs
           

4 创建要监控的文件

touch /root/logs/test.log
           

5 使用shell模拟数据源源不断产生

while true;do date >> /root/logs/test.log;sleep 0.5;done

或者:
#/bin/bash
while true
do
  date >> /root/logs/test.log
  sleep 0.5
done  
           

6 测试

2020-06-23 04:53:02,932 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.hdfs.HDFSDataStream.configure(HDFSDataStream.java:57)] Serializer = TEXT, UseRawLocalFileSystem = false
2020-06-23 04:53:02,943 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.hdfs.BucketWriter.open(BucketWriter.java:251)] Creating /flume/tailout/20-06-23/04-50//itcast-.1592859182933.tmp
2020-06-23 04:53:07,963 (hdfs-k1-roll-timer-0) [INFO - org.apache.flume.sink.hdfs.BucketWriter.close(BucketWriter.java:393)] Closing /flume/tailout/20-06-23/04-50//itcast-.1592859182933.tmp
2020-06-23 04:53:07,975 (hdfs-k1-call-runner-3) [INFO - org.apache.flume.sink.hdfs.BucketWriter$8.call(BucketWriter.java:655)] Renaming /flume/tailout/20-06-23/04-50/itcast-.1592859182933.tmp to /flume/tailout/20-06-23/04-50/itcast-.1592859182933
2020-06-23 04:53:07,977 (hdfs-k1-roll-timer-0) [INFO - org.apache.flume.sink.hdfs.HDFSEventSink$1.run(HDFSEventSink.java:382)] Writer callback called.
           

7 扩展 conf中配置属性解释

#roll 滚动 写入hdfs的数据以何种方式切换滚动
a1.sinks.k1.hdfs.rollInterval = 3  #以时间间隔控制 默认是30s  
a1.sinks.k1.hdfs.rollSize = 20     #以文件大小控制  默认1024 bytes
a1.sinks.k1.hdfs.rollCount = 5     #以event个数控制 默认10个
#谁先满足 触发条件 就滚动
#如果不想一某个属性滚动怎么办?  设置为0  删除没有效果 因为有默认值。


#round 想一想hive中round()函数 取整
#功能:是否开启时间上的舍弃 true表示开启   通俗解释:文件夹以多少时间间隔切换
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute