flume-kafka-hdfs
首先,你需要在Master节点上配置并启动Flume。你需要创建一个Flume配置文件,
指定数据源(数据源类型为netcat,端口为10050)
数据目标(数据目标类型为Kafka,Topic名称为order,分区数为4),
以及备份目标(数据目标类型为HDFS,目录为/user/test/flumebackup)。
Flume配置文件内容可能类似于以下示例:
flume的conf目录下创建
# 名称为 example 的 agent
agent.sources = source1
agent.channels = channel1
agent.sinks = sink1 sink2
# 配置 source1
agent.sources.source1.type = netcat
agent.sources.source1.bind = localhost
agent.sources.source1.port = 10050
# 配置 channel1
agent.channels.channel1.type = memory
agent.channels.channel1.capacity = 10000
agent.channels.channel1.transactionCapacity = 100
# 配置 sink1,将数据写入 Kafka
agent.sinks.sink1.type = org.apache.flume.sink.kafka.KafkaSink
agent.sinks.sink1.kafka.bootstrap.servers = localhost:9092
agent.sinks.sink1.kafka.topic = order
agent.sinks.sink1.kafka.partition.number = 4
agent.sinks.sink1.channel = channel1
# 配置 sink2,将数据备份到 HDFS
agent.sinks.sink2.type = hdfs
agent.sinks.sink2.hdfs.path = /user/test/flumebackup/%Y-%m-%d/%H%M/%S
agent.sinks.sink2.hdfs.filePrefix = flume-backup
agent.sinks.sink2.hdfs.round = true
agent.sinks.sink2.hdfs.roundValue = 10
agent.sinks.sink2.hdfs.roundUnit = minute
agent.sinks.sink2.channel = channel1
二、创建一个名为“itcasttopic”的主题
kafka-topics.sh --create --topic itcasttopic --partitions 3 --replication-factor 2 --zookeeper master:2181,slave1:2181,slave2:2181
--create --topic itcasttopic: 创建主题名称是 itcasttopic
--partitions 3 : 分区数是3
--replication-factor 2:副本数是 2
--zookeeper master:2181,slave1:2181,slave2:2181 : zookeeper:服务的IP地址和端口
##删除主题##
$ bin/kafka-topics.sh --delete -zookeeper master:2181,slave1:2181,slave2:2181 --topic itcasttopic
①、创建生产者
kafka-console-producer.sh --broker-list master:9092,slave1:9092,slave2:9092 --topic itcasttopic
@创建消费者
kafka-console-consumer.sh --from-beginning --topic itcasttopic --bootstrap-server master:90
启动Flume:
shell
$ bin/flume-ng agent -n example -c conf -f conf/example.conf
然后,你需要创建一个Kafka消费者来消费"order" topic中的数据。在Java中,这可以通过Kafka的Consumer API来实现。
你需要消费前两条数据并将结果截图。这部分内容需要编程实现,无法提供直接的命令或配置。
最后,你需要查看HDFS目录/user/test/flumebackup下的第一个文件的前两条数据。这可以通过Hadoop的fs shell命令来实现:
shell
$ hadoop fs -cat /user/test/flumebackup/flume-backup.* | head -n 2
注意:上述配置和命令示例可能需要根据你的实际环境和需求进行修改。
启动Agent
启动Flume Agent命令:
flume-ng agent -c /usr/local/flume/conf -f /usr/local/flume/conf/netcat-logger.conf -n a1
-Dflume.root.logger=INFO,console
参数解析:
-c conf 指定flume自身的配置文件所在目录
-f conf/netcat-logger.conf 指定所描述的采集方案
-n a1 指定这个agent的名字
-Dflume.root.logger=DEBUG,console 设置日志等级
采集需求:监听某目录,每当有新文件出现,就需要把文件采集到HDFS中去
Source:配置从本地目录采集数据
Channel:配置中间缓存采用磁盘存储
Sink:配置存储位置为HDFS
flume连接kafka
最新推荐文章于 2024-04-25 16:59:10 发布