flume使用四个案例(监听端口,监听文件并传到另外一台电脑,监听文件夹,监听文件夹并将数据保存到hdfs)

该博客详细介绍了Apache Flume的四个实用案例,包括使用Flume监听端口并将数据输出到控制台,从一个节点监听文件并将其通过Avro协议传输到另一节点,监听文件夹并将内容显示,以及将文件数据持久化到HDFS。每个案例都包含了配置信息、执行步骤和测试方法,为Flume的学习和实践提供了清晰的指导。
摘要由CSDN通过智能技术生成

1.案例一监听端口到界面

1.1监听端口配置信息

## 配置信息 
## 文件名 example-netcat.conf
a1.sources = r1
a1.sinks = k1
a1.channels = c1

a1.sources.r1.type = netcat
a1.sources.r1.bind = localhost
a1.sources.r1.port = 7000

a1.sinks.k1.type = logger

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

1.2 执行

flume-ng agent -n a1 -c /opt/bdsp/apache-flume-1.6.0-bin/conf/ -f /opt/bdsp/conf/flume/example-netcat.conf  -Dflume.root.logger=INFO,console

1.3 监听端口测试

telnet localhost 7000

2监听文件并传到另外一台电脑上界面

2.1配置信息(监听文件->avro->nodez001->logger)

## nodez001
## 文件名 example-avro_node1.conf
a1.sources = r1
a1.sinks = k1
a1.channels = c1

a1.sources.r1.type = cmd 
a1.sources.r1.command = tail -F /root/flume.txt

a1.sinks.k1.type = avro
a1.sinks.k1.hostname = nodez002
a1.sinks.k1.port = 45454

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

## nodez002
## example-avro_node2.conf
a2.sources = r1
a2.sinks = k1
a2.channels = c1


a2.sources.r1.type = avro
a2.sources.r1.hostname = nodez002
a2.sources.r1.port = 45454

a2.sinks.k1.type = logger

a2.channels.c1.type = memory
a2.channels.c1.capacity = 1000
a2.channels.c1.transactionCapacity = 100

a2.sources.r1.channels = c1

2.2 执行

## nodez002上执行
flume-ng agent -n a2 -c /opt/bdsp/apache-flume-1.6.0-bin/conf/ -f /opt/bdsp/conf/flume/example-avro_node2.conf  -Dflume.root.logger=INFO,console
## nodez001上执行
flume-ng agent -n a1 -c /opt/bdsp/apache-flume-1.6.0-bin/conf/ -f /opt/bdsp/conf/flume/example-avro_node1.conf

2.3 测试

## nodez001
echo "xxxx" >> /root/flume.txt
# 查看nodez001 界面

3 监听文件夹到界面

3.1 配置信息

## example-dir.conf 
a1.sources = r1
a1.sinks = k1
a1.channels = c1

a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /opt/bdsp/logs
a1.sources.r1.fileHeader = true

a1.sinks.k1.type = logger

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

3.2 执行

## nodez002上执行
flume-ng agent -n a1 -c /opt/bdsp/apache-flume-1.6.0-bin/conf/ -f /opt/bdsp/conf/flume/example-dir.conf  -Dflume.root.logger=INFO,console

3.3 测试

mv xxx.txt /opt/bdsp/logs/
# 移动文件到指定位置 观察窗口变化

4 监听文件到hdfs

4.1 配置信息

##  example-hdfs.conf 
a1.sources = r1
a1.sinks = k1
a1.channels = c1

a1.sources.r1.type = spooldir
a1.sources.r1.spoolDir = /opt/bdsp/logs
a1.sources.r1.fileHeader = true

a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs = hdfs://bdsp/flume/%y-%m-%d/%H%M
##每隔60秒或者文件大小超过10M的时候产生新文件
##hdfs有多少消息是新建文件,0不基于消息个数
a1.sinks.k1.hdfs.rollCount = 0
##hdfs创建多长时间新建文件,0不基于时间
a1.sinks.k1.hdfs.rollInterval = 60
##hdfs多大时创建新建文件,0不基于文件带下
a1.sinks.k1.hdfs.rollSize = 10240

##当目前被打开的临时文件在改参数指定时间(秒)内,没有目标文件
a1.sinks.k1.hdfs.idleTimeout = 3
a1.sinks.k1.hdfs.fileType = DataStream
a1.sinks.k1.hdfs.useLocalTimeStamp = true
##每五分钟生产一个目录
##是够启用时间上的舍弃,这里的舍弃,类似于四舍五入所有时间表达式
a1.sinks.k1.hdfs.round = true
##时间上进行舍弃的值
a1.sinks.k1.hdfs.roundValue = 5 
##时间上舍弃的单位,包含second,minute,hour
a1.sinks.k1.hdfs.roundUnit = minute

a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

4.2 执行

flume-ng agent -n a1 -c /opt/bdsp/apache-flume-1.6.0-bin/conf/ -f /opt/bdsp/conf/flume/example-hdfs.conf

3.3 测试

mv xxx.txt /opt/bdsp/logs/
# 移动文件到指定位置 观察 hdfs://bdsp/flume 文件夹

未完待续

如果有谬误,请指出来,希望共同学习,共同进步,day day up!

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以通过Flume的Spooling Directory Source来实现这个功能。Spooling Directory Source会监控一个指定的目录,当有新的文件被写入时,就会将文件内容作为事件传递给Flume的Channel。然后再通过Flume的Interceptor将文件中的逗号修改为竖杠,并存储到HDFS中。 具体的配置如下: 1. 在Flume的配置文件中添加一个Spooling Directory Source: ``` # define the Spooling Directory Source agent.sources = spoolDir agent.sources.spoolDir.type = spooldir agent.sources.spoolDir.spoolDir = /path/to/spool/directory agent.sources.spoolDir.fileHeader = true agent.sources.spoolDir.fileSuffix = .COMPLETED ``` 2. 添加一个Interceptor来修改文件内容 ``` # define the Interceptor to modify the file content agent.sources.spoolDir.interceptors = interceptor1 agent.sources.spoolDir.interceptors.interceptor1.type = regex_replace agent.sources.spoolDir.interceptors.interceptor1.regex = , agent.sources.spoolDir.interceptors.interceptor1.replaceString = | ``` 3. 添加一个HDFS Sink ``` # define the HDFS Sink agent.sinks = hdfsSink agent.sinks.hdfsSink.type = hdfs agent.sinks.hdfsSink.hdfs.path = /path/to/hdfs/directory agent.sinks.hdfsSink.hdfs.fileType = DataStream agent.sinks.hdfsSink.hdfs.rollInterval = 0 ``` 最后将Spooling Directory Source和HDFS Sink连接起来: ``` # connect the Source and Sink agent.sources.spoolDir.channels = memoryChannel agent.sinks.hdfsSink.channel = memoryChannel ``` 这样配置后,Flume就会监听指定的目录,当有新文件写入时,将文件内容作为事件传递给内存Channel。Interceptor会修改文件内容,然后将修改后的事件存储到HDFS中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武念

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值