flume和kafka整合——采集实时日志落地到hdfs

一、采用架构

在这里插入图片描述

flume 采用架构
exec-source + memory-channel + kafka-sink
kafka-source + memory-channel + hdfs-sink
模拟需求:
使用flume实时监听日志文件,并将采集数据传输到kafka,再从kafka采集数据到flume,最后落地到HDFS。

二、 前期准备

2.1 虚拟机配置

操作系统主机名IPflumekafka
Ubuntuslave1192.168.0.104flume-kafka集群部署
Ubuntuslave2192.168.0.105集群部署
Ubuntuslave3192.168.0.106kafka-flume集群部署

slave1部署flume实时监听日志文件
slave3部署flume实时消费kafka数据
kafka采用3台虚拟机集群部署方式

2.2 启动hadoop集群

因为需要将数据存储到HDFS,需要hadoop集群。这里使用Hadoop-2.7.6版本。不会集群的可以参考这篇:Hadoop-2.7.6 集群部署搭建

2.3 启动zookeeper集群,kafka集群

kafka单节点启动也可以。可以参考这篇:Kafka入门集群部署

三、编写配置文件

3.1 slave1创建flume-kafka.conf

cd /opt/apps/apache-flume-1.9.0-bin/job
vim flume-kafka.conf 

在flume-kafka.conf输入

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

# Describe/configure the sources
# exec即execute执行命令
a1.sources.r1.type = exec
# 要执行的命令
a1.sources.r1.command = tail -F /data/sdk.log
# 执行shell脚本的绝对路径
a1.sources.r1.shell = /bin/bash -c

# Describe the sink
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.topic = sdk
a1.sinks.k1.kafka.bootstrap.servers = slave1:9092,slave2:9092,slave3:9092

# 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

这里监控的是/data/sdk.log,并将传输kafka的sdk这个topic

3.2 slave3 创建kafka-flume.conf

cd /opt/apps/apache-flume-1.9.0-bin/job
vim kafka-flume.conf 

在kafka-flume.conf输入

# Name the components on this agent
a1.sources = r1
a1.sinks = k1
a1.channels = c1
 
a1.sources.r1.type = org.apache.flume.source.kafka.KafkaSource
a1.sources.r1.kafka.bootstrap.servers = slave1:9092,slave2:9092,slave3:9092
a1.sources.r1.kafka.topics = sdk
a1.sources.kafkaSource.kafka.consumer.timeout.ms = 100
a1.sources.r1.batchSize = 20

 
# Describe the sink
a1.sinks.k1.type = hdfs
# 上传到hdfs的路径
a1.sinks.k1.hdfs.path = hdfs://slave1:9000/sdk/%Y%m%d%H
# 文件前缀
a1.sinks.k1.hdfs.filePrefix = logs-
# 是否按时间新建文件夹
a1.sinks.k1.hdfs.round = true
# 定义多久新建文件夹
a1.sinks.k1.hdfs.roundValue = 1
# 重新定义时间单位
a1.sinks.k1.hdfs.roundUnit = hour
# 是否使用本地时间戳
a1.sinks.k1.hdfs.useLocalTimeStamp = true
# flush到hdfs需要积攒event的数量
a1.sinks.k1.hdfs.batchSize = 50
# 设置文件类型
a1.sinks.k1.hdfs.fileType = DataStream
# 多久生成一个新的文件(单位:秒)
a1.sinks.k1.hdfs.rollInterval = 60
# 接收多少数据生成一个新文件(单位:字节)
a1.sinks.k1.hdfs.rollSize = 134217700
# 文件的滚动与event数量无关
a1.sinks.k1.hdfs.rollCount = 0
# 最小冗余数
a1.sinks.k1.hdfs.minBlockReplicas = 1
 
# 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

3.3 创建kafka的topic

cd /opt/apps/kafka_2.12-2.3.0
bin/kafka-topics.sh --zookeeper slave1:2181 --create --replication-factor 2 --partitions 1 --topic sdk

3.4 启动flume配置测试

进入到slave1的flume根目录执行
cd /opt/apps/apache-flume-1.9.0-bin

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

进入到slave3的flume根目录执行
cd /opt/apps/apache-flume-1.9.0-bin

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

效果:
在这里插入图片描述
往实时监控sdk.log写入数据,可以看到数据传输到HDFS上

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值