java 发送数据到flume_Flume 采集发送消息到kafka

本文介绍了如何配置和使用Flume从Mac或Linux环境中采集日志文件内容,并将其发送到Kafka主题。通过TAILDIR源类型跟踪日志文件,配置KafkaSink将数据传输到指定的Kafka集群。详细配置包括Flume agent脚本、Flume配置文件以及环境变量设置。
摘要由CSDN通过智能技术生成

一些场景需要采集文本内容发送到kafka,flume正好提供了这种解决方案

Flume agent脚本及配置如下

Mac

安装:

brew install flume

启动脚本:

nohup /usr/local/Cellar/flume/1.9.0/bin/flume-ng agent --conf /data/flume/run/flume/conf/conf.d/ --conf-file /data/flume/run/flume/conf/conf.d/mg-res_test.conf --name mg-res_test -Dflume.codeadmin.logger=INFO,console &

linux

安装:

启动脚本:

nohup /data/flume/run/flume/bin/flume-ng agent --conf /data/flume/run/flume/conf/conf.d/ --conf-file /data/flume/run/flume/conf/conf.d/mg-res_test.conf --name mg-res_test -Dflume.codeadmin.logger=INFO,console &

配置说明

/data/flume/run/flume/conf/conf.d/mg-res_test.conf

flume主要配置信息,关注中文描述的部分即可

# Name the components on this agent

mg-res_test.sources = mg-res_test

mg-res_test.sinks = mg-res_test

mg-res_test.channels = mg-res_test

# Describe/configure the source

mg-res_test.sources.mg-res_test.type = TAILDIR

# flume 记录偏移量文件

mg-res_test.sources.mg-res_test.positionFile=/data/logs/test/res-test/mg-res_test.json

mg-res_test.sources.mg-res_test.filegroups=f1

# 待采集的日志文件

mg-res_test.sources.mg-res_test.filegroups.f1=/data/logs/test/res-test/app.log

mg-res_test.sources.mg-res_test.fileHeader=true

# Describe the sink

mg-res_test.sinks.mg-res_test.channel = mg-res_test

mg-res_test.sinks.mg-res_test.type = org.apache.flume.sink.kafka.KafkaSink

# kafka topic配置 以实际为准

mg-res_test.sinks.mg-res_test.kafka.topic = res-test

# kafka 配置信息 以实际为准

mg-res_test.sinks.mg-res_test.kafka.bootstrap.servers = localhost:9092

mg-res_test.sinks.mg-res_test.kafka.flumeBatchSize = 2

mg-res_test.sinks.mg-res_test.kafka.producer.acks = 1

mg-res_test.sinks.mg-res_test.kafka.producer.linger.ms = 1

mg-res_test.sinks.mg-res_test.kafka.producer.compression.type = snappy

# Use a channel which buffers events in memory

mg-res_test.channels.mg-res_test.type = memory

mg-res_test.channels.mg-res_test.capacity = 100000

mg-res_test.channels.mg-res_test.transactionCapacity = 10000

# Bind the source and sink to the channel

mg-res_test.sources.mg-res_test.channels = mg-res_test

mg-res_test.sinks.mg-res_test.channel = mg-res_test

/data/flume/run/flume/conf/conf.d/flume-env.sh

配置java home即可

export JAVA_OPTS="-Xms128m -Xmx128m -Dcom.sun.management.jmxremote"

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home

验证

本地环境可以直接启动kafka消费看有没有消息进入,测试环境请查询kafka消息增量或业务系统实际消费情况

kafka-console-consumer --bootstrap-server localhost:9092 --topic res-test --from-beginning

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答这个问题。您可以按照以下步骤来使用Flume采集Kafka数据到HDFS和Hive中: 1. 首先,确保您已经安装了FlumeKafka、HDFS和Hive等必要的软件。 2. 然后,在Flume的配置文件中添加Kafka作为Source和HDFS/Hive作为Sink。例如,以下是一个简单的配置文件示例: ``` agent.sources = kafka-source agent.channels = hdfs-channel agent.sinks = hdfs-sink hive-sink agent.sources.kafka-source.type = org.apache.flume.source.kafka.KafkaSource agent.sources.kafka-source.batchSize = 1000 agent.sources.kafka-source.kafka.bootstrap.servers = localhost:9092 agent.sources.kafka-source.kafka.topics = my-topic agent.sources.kafka-source.kafka.consumer.group.id = my-group agent.channels.hdfs-channel.type = memory agent.channels.hdfs-channel.capacity = 10000 agent.channels.hdfs-channel.transactionCapacity = 100 agent.sinks.hdfs-sink.type = hdfs agent.sinks.hdfs-sink.hdfs.path = hdfs://localhost:9000/flume/kafka agent.sinks.hdfs-sink.hdfs.fileType = DataStream agent.sinks.hdfs-sink.hdfs.writeFormat = Text agent.sinks.hdfs-sink.channel = hdfs-channel agent.sinks.hive-sink.type = hive agent.sinks.hive-sink.hive.metastore = thrift://localhost:9083 agent.sinks.hive-sink.hive.database = my-db agent.sinks.hive-sink.hive.table = my-table agent.sinks.hive-sink.hive.partition = dt=%Y-%m-%d agent.sinks.hive-sink.channel = hdfs-channel ``` 在上面的配置文件中,我们使用Kafka作为Source,将数据写入到HDFS和Hive中。我们使用Memory Channel将数据缓存在内存中,然后将其写入到HDFS和Hive中。在Hive Sink中,我们还定义了一个分区,以便按日期对数据进行分区。 3. 最后,运行Flume代理以开始从Kafka读取数据并将其写入到HDFS和Hive中。您可以使用以下命令来启动Flume代理: ``` $ bin/flume-ng agent -n agent -c conf -f conf/flume-kafka-hdfs-hive.conf ``` 这样,Flume代理就会开始从Kafka读取数据,并将其写入到HDFS和Hive中。 希望这些信息能够帮助您采集Kafka数据到HDFS和Hive中。如果您有任何其他问题,请随时问我。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值