flume三个案例

flume 安装 及测试 netcat-logs.conf

Flume :
Flume 安装 上传包 解压
到 conf 目录下
flume.env.sh 配置 jdk 即可
vi netcat-logger.conf

定义这个 agent 中各组件的名字

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

描述和配置 source 组件:r1

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

描述和配置 sink 组件:k1

a1.sinks.k1.type = logger

描述和配置 channel 组件,此处使用是内存缓存的方式

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

描述和配置 source channel sink 之间的连接关系

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
简单测试案例:yum install telnet ;telnet localhost 44444; 输入 hello【前提 下面执行命令 已经执行】
执行命令:bin/flume-ng agent -c conf -f conf/netcat-logger.conf -n a1 -Dflume.root.logger=INFO,console

案例二:需求监控变化的目录内的文件

需求:服务器的某特定目录下,会不断产生新的文件,每当有新文件出现,
就需要把文件采集到 S HDFS 中去
Vi spooldir-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 = spooldir
a1.sources.r1.spoolDir = /root/logs
a1.sources.r1.fileHeader = true

Describe the sink

a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume/events/%y-%m-%d/%H%M/
a1.sinks.k1.hdfs.filePrefix = events-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.rollInterval = 3
a1.sinks.k1.hdfs.rollSize = 20
a1.sinks.k1.hdfs.rollCount = 5
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
Mkdir logs2
Cp a.txt logs2 【前提执行flume命令,cp 后查看结果】
执行命令:
bin/flume-ng agent -c conf -f conf/spooldir-hdfs.conf -n a1 -Dflume.root.logger=INFO,console
案例二结束

案例三 :需求 :监控变化的内容文件

需求:比如业务系统使用 j log4j 生成的日志,日志内容不断增加,需要把追
加到日志文件中的数据实时采集到 hdfs【简而言之:一个文件的内容在变化】
Vi tail-hdfs.conf
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
a1.sources.r1.channels = c1

Describe the sink

a1.sinks.k1.type = hdfs
a1.sinks.k1.hdfs.path = /flume/tailout/%y-%m-%d/%H%M/
a1.sinks.k1.hdfs.filePrefix = events-
a1.sinks.k1.hdfs.round = true
a1.sinks.k1.hdfs.roundValue = 10
a1.sinks.k1.hdfs.roundUnit = minute
a1.sinks.k1.hdfs.rollInterval = 3
a1.sinks.k1.hdfs.rollSize = 20
a1.sinks.k1.hdfs.rollCount = 5
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
执行命令:while true ; do echo date >>test.log ;sleep 1;done 持续向test.log文件中添加date字符串
或者:while true ; do date >>test.log ;sleep 1;done 持续添加系统时间
案例三结束

标题

你可能感兴趣的:(大数据,flume)