Flume的配置与使用

2 篇文章 0 订阅

前提条件:

安装好hadoop2.7.3(Linux系统下)

安装好Flume,参考:Flume安装配置

原理:

Flume数据流模型

题目:

完成通过Avro Source接收外部数据源,数据缓存在memory channel中,然后通过Logger sink将打印出数据,即:

avro source --> memory channel --> logger sink

 

步骤:

1.进入有权限的目录,例如~目录

$ cd ~

2.创建配置文件avro.conf(关键)

$ nano avro.conf

内容如下:

a1.sources = r1
a1.sinks = k1
a1.channels = c1
#配置source
a1.sources.r1.type = avro
a1.sources.r1.bind = 0.0.0.0
a1.sources.r1.port = 4141
# 配置sink
a1.sinks.k1.type = logger
# 配置channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100
# 绑定 source 和sink 到 channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

 3.启动Flume agent

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

注意: --conf为配置文件所在目录,这里配置为"./"表示当前目录; --conf-file表示配置文件名称; --name表示 flume代理名称,其他的为日志级别 

4.测试

4.1 打开新的终端(重要)

4.2 新建一个文件夹testFlume(如果已存在该文件夹,请跳过mkdir testFlume命令)

$ cd ~
$ mkdir testFlume

4.3 向文件log.00输入一些信息,例如:“hello world”

echo "hello world" > ~/testFlume/log.00

5.使用avro-client发送文件

flume-ng  avro-client  -c ./  -H  0.0.0.0  -p 4141 -F  testFlume/log.00

注意:-c为conf所在目录,-H为主机, -p为端口号 -F为要发送文件所在的路径

 

在监听终端(启动Flume agent命令的终端)看到log.00的内容“hello world”。

 

更多的案例:

1. netcat source --> memory channel --> logger sink

nc.conf

# 设置agent
b1.sources = r1
b1.sinks = k1
b1.channels = c1

# 配置source
b1.sources.r1.type = netcat
b1.sources.r1.bind = localhost
b1.sources.r1.port = 44444

# 配置sink
b1.sinks.k1.type = logger

# 配置channel
b1.channels.c1.type = memory
b1.channels.c1.capacity = 1000
b1.channels.c1.transactionCapacity = 100

#将source和sink绑定到channel
b1.sources.r1.channels = c1
b1.sinks.k1.channel = c1

启动flume

$ flume-ng agent --conf ./ --conf-file nc.conf --name b1 -Dflume.root.logger=INFO,console

启动另一个终端,执行如下命令

$ telnet localhost 44444

进入监听状态后,输入一些数据,按回车发送数据,在flume终端查看接收到的数据。

 

2. exec source --> memory channel --> HDFS sink

exec source表示用执行命令的输出作为数据源,案例中执行的命令为 tail -F /home/hadoop/1.log

HDFS sink表示将数据发送到HDFS中

hdfs.conf

# 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 /home/hadoop/1.log
a2.sources.r2.shell = /bin/bash -c

# Describe the sink
a2.sinks.k2.type = hdfs
a2.sinks.k2.hdfs.path = hdfs://node1:8020/flume/%Y%m%d/%H%M/%S
#上传文件的前缀
a2.sinks.k2.hdfs.filePrefix = logs- 
#是否按照时间滚动文件夹
a2.sinks.k2.hdfs.round = true
#多少时间单位创建一个新的文件夹
a2.sinks.k2.hdfs.roundValue = 1
#重新定义时间单位
a2.sinks.k2.hdfs.roundUnit = minute
#是否使用本地时间戳
a2.sinks.k2.hdfs.useLocalTimeStamp = true
#积攒多少个 Event 才 flush 到 HDFS 一次
a2.sinks.k2.hdfs.batchSize = 3
#设置文件类型,可支持压缩
a2.sinks.k2.hdfs.fileType = DataStream
#多久生成一个新的文件
a2.sinks.k2.hdfs.rollInterval = 600 
#设置每个文件的滚动大小
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

启动hdfs

$ start-dfs.sh

启动flume

$ flume-ng agent --conf ./ --conf-file hdfs.conf --name a2 -Dflume.root.logger=INFO,console

另外启动一个终端,发送些数据到1.log

$ echo "something different 44" >> 1.log
$ echo "something different 55" >> 1.log

查看hdfs的内容,注意cat后面的文件路径需要从flume终端得到。

$ hdfs dfs -cat /flume/20201116/2326/00/logs-.1605540379735.tmp

看到了每一分钟会生成一个数据目录,文件名的前缀为logs- 加上毫秒的时间戳(13位).tmp,文件内容为刚才一分钟内给1.log发送的数据。

 

更多的配置案例,可参考:flume官方文档

 

完成! enjoy it!

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flume是一个分布式、可靠和高效的服务,用于收集、聚合和移动大量日志数据。它主要用于将日志数据从各种源(如Web服务器、数据库、应用程序日志等)中收集,经过处理后将其传输到各种目的地(如Hadoop HDFS、Hive、Kafka等)。 下面是Flume配置使用步骤: 1. 下载并安装Flume 可以从Apache Flume官网下载Flume的最新版本,并按照安装说明进行安装。 2. 编写Flume配置文件 Flume配置文件是一个.properties文件,其中定义了数据源、数据目的地、数据传输顺序等信息。 下面是一个简单的Flume配置文件示例: ``` # 定义agent名称和数据源 agent.sources = source1 agent.sources.source1.type = exec agent.sources.source1.command = tail -F /var/log/messages # 定义数据目的地 agent.sinks = sink1 agent.sinks.sink1.type = avro agent.sinks.sink1.hostname = localhost agent.sinks.sink1.port = 41414 # 定义数据处理管道 agent.channels = channel1 agent.channels.channel1.type = memory agent.channels.channel1.capacity = 1000 agent.channels.channel1.transactionCapacity = 100 #将数据源和数据目的地连接起来 agent.sources.source1.channels = channel1 agent.sinks.sink1.channel = channel1 ``` 其中,agent.sources定义了数据源,agent.sinks定义了数据目的地,agent.channels定义了数据处理管道,通过将数据源和数据目的地连接到数据处理管道上,就可以将数据从源头传输到目的地了。 3. 启动Flume agent 启动Flume agent的命令为: ``` bin/flume-ng agent -n agentName -f /path/to/flume-conf.properties ``` 其中,-n参数指定了agent的名称,-f参数指定了Flume配置文件的路径。 4. 监控Flume agent 可以使用Flume自带的监控工具来监控Flume agent的运行状态。启动监控工具的命令为: ``` bin/flume-ng agent -n agentName -f /path/to/flume-conf.properties -Dflume.monitoring.type=http -Dflume.monitoring.port=34545 ``` 其中,-Dflume.monitoring.type参数指定了监控工具的类型,-Dflume.monitoring.port参数指定了监控工具的端口号。在浏览器中访问http://localhost:34545即可查看监控信息。 这就是Flume配置使用步骤,希望能帮助到您。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值