flume基本操作——source、sink

1、准备工作

1、安装flume

2、在flume目录下创建data目录进入 /opt/apps/flume

mkdir data

3、安装netcat(安装过可不执行)

yum install -y nc

2、source

1.basic.conf配置

在data目录下创建basic.conf

#给Agent起个名称
a1.sources=r1
a1.channels=c1
a1.sinks=s1

#配置sources
a1.sources.r1.type=netcat
a1.sources.r1.bind=0.0.0.0
# 着重      端口号与nectcat发送端口一致(可更改)        #######################
a1.sources.r1.port=12345
 

#配置channel
a1.channels.c1.type=memory
#通道中允许1000各事件
a1.channels.c1.capacity=1000
#每次给sink发送100各事件
a1.channels.c1.transactionCapacity=100

#配置sink
#将日志打印在控制台上
a1.sinks.s1.type=logger


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

启动命令,进入到data目录下执行

flume-ng agent \
--conf conf \
--conf-file ./basic.conf \
--name a1 \
-Dflume.root.logger==INFO,console

另起一个node01使用nc命令窗口向12345端口发送hello

nc node02 12345

或者
nc -l 12345

发送

flume采集到hello 

 

2、spoolSource 配置:监测的文件夹

使用flume spool的source,当被监控的目录下有文件更新,就会将文件数据发给chanel。

在data目录下创建spoolsource.conf

#给Agent起个名称
a1.sources=r1
a1.channels=c1
a1.sinks=s1

#配置sources,监测的文件夹
a1.sources.r1.type=spooldir
a1.sources.r1.spoolDir=/opt/flumedata
a1.sources.r1.port=12345

#配置channel
a1.channels.c1.type=memory
#通道中允许1000各事件
a1.channels.c1.capacity=1000
#每次给sink发送100各事件
a1.channels.c1.transactionCapacity=100

#配置sink
#将日志打印在控制台上
a1.sinks.s1.type=logger


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

在/opt/下创建flumedata文件夹

mkdir flumedata

 在/opt/apps/flume/data目录下启动spoolsource

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

向flumedata文件夹中写入数据

vim test.txt

输入hello flume \ hello spark

 flume采集到数据

 原来文件后缀改变

3、httpsource配置:监测http请求

Http Source可以通过Http Post接收事件。

 在data目录下创建httpsource.conf

#给Agent起个名称
a1.sources=r1
a1.channels=c1
a1.sinks=s1

#配置sources http请求
a1.sources.r1.type=http
a1.sources.r1.port=12345

#配置channel
a1.channels.c1.type=memory
#通道中允许1000各事件
a1.channels.c1.capacity=1000
#每次给sink发送100各事件
a1.channels.c1.transactionCapacity=100

#配置sink
#将日志打印在控制台上
a1.sinks.s1.type=logger


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

启动采集器

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

 发送http请求

curl -X POST -d '[{"header":{},"body":"hello flume"}]' http://node01:12345

采集到数据

 4、配置execsource.conf

在data目录下创建execsource.conf

Exec Source在启动时运行给定的linux命令,并期望进程在标准输出上产生连续的数据(除非属性logStdErr设置为true,否则stderr将被丢弃)。 如果进程由于任何原因退出,source也会退出,并且不会生成更多数据。

#给Agent起个名称
#
a1.sources=r1
a1.channels=c1
a1.sinks=s1

#配置sources
a1.sources.r1.type=exec
a1.sources.r1.command=ping www.baidu.com
a1.sources.r1.bind=0.0.0.0
a1.sources.r1.port=12345

#配置channel
a1.channels.c1.type=memory
#通道中允许1000各事件
a1.channels.c1.capacity=1000
#每次给sink发送100各事件
a1.channels.c1.transactionCapacity=100

#配置sink
#将日志打印在控制台上
a1.sinks.s1.type=logger


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

启动采集 

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

执行结果已经采集到数据包 

 5、avrosource.conf配置

监听Avro端口来接受外部avro客户端的事件流,和netcat不同的是,avro-source接收到的是
经过avro序列化之后的数据,然后反序列化数据继续传输,所以,如果avro-source的话,源
数据必须是经过avro序列化之后的数据。而netcat接收的是字符串格式的数据。

在node01上配置

#给Agent起个名称
a1.sources=r1
a1.channels=c1
a1.sinks=s1

#配置sources
a1.sources.r1.type=avro
a1.sources.r1.bind=0.0.0.0
a1.sources.r1.port=12345

#配置channel
a1.channels.c1.type=memory
#通道中允许1000各事件
a1.channels.c1.capacity=1000
#每次给sink发送100各事件
a1.channels.c1.transactionCapacity=100

#配置sink
#将日志打印在控制台上
a1.sinks.s1.type=logger


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

启动采集器

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

 无测试

3、sink

1、filesink

#给Agent起个名称
a1.sources=r1
a1.channels=c1

a1.sinks=s1

#配置sources
a1.sources.r1.type=netcat
a1.sources.r1.bind=0.0.0.0
a1.sources.r1.port=12345

#配置channel
a1.channels.c1.type=memory
#通道中允许1000各事件
a1.channels.c1.capacity=1000
#每次给sink发送100各事件
a1.channels.c1.transactionCapacity=100

#配置sink
#将日志打印在控制台上
a1.sinks.s1.type=file_roll
a1.sinks.s1.sink.directory=/opt/flumelog

#每隔一个小时会产生一个新的日志文件
#把日志落在了本地文件
a1.sinks.s1.sink.rollInterval=3600


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

2、hdfssink

#给Agent起个名称
#
a1.sources=r1
a1.channels=c1
a1.sinks=s1

#配置sources
a1.sources.r1.type=netcat
a1.sources.r1.bind=0.0.0.0
a1.sources.r1.port=12345

#配置channel
a1.channels.c1.type=memory
#通道中允许1000各事件
a1.channels.c1.capacity=1000
#每次给sink发送100各事件
a1.channels.c1.transactionCapacity=100

#配置sink
#将日志打印在控制台上
a1.sinks.s1.type=hdfs
a1.sinks.s1.hdfs.path=hdfs://node01:9000/flume
#利用流的形式将数据写到Hdfs
a1.sinks.s1.hdfs.fileType=DataStream


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

 

Flume的核心概念包括:

1. Agent(代理):Flume中的基本工作单元,负责数据的收集、传输和存储。

2. Source(数据源):负责从数据源收集数据,并将其传输给Channel。

3. Channel(通道):用于在Source和Sink之间缓冲和存储事件数据,确保可靠传输和可扩展性。

4. Sink(数据汇):将数据从Channel中取出,并将其传输到目标存储系统,如HDFS、HBase、Kafka等。

Flume还提供了多种Source和Sink的类型,以适应不同的数据源和目标系统。此外,Flume还支持事件拦截器(interceptor)用于数据转换和处理,以及拓扑(Topology)的定义和配置,用于指定Agent之间的数据流动关系。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值