kafka与flume整合(kafka充当source,sink,channel)

  1. Kafka充当Flume的source数据源,此时也就意味着Flume需要采集Kafka的数据,Flume相当于是kafka的一个消费者
    .conf文件(KafkaToConsole.conf)
    #sources别名:r1
    a1.sources = r1
    #sink别名:k1
    a1.sinks = k1
    #channel别名:c1
    a1.channels = c1

定义flume的source数据源 —kafka

a1.sources.r1.type = org.apache.flume.source.kafka.KafkaSource
a1.sources.r1.batchSize = 5000
a1.sources.r1.batchDurationMillis = 2000
a1.sources.r1.kafka.bootstrap.servers = node1:9092,node2:9092,node3:9092
a1.sources.r1.kafka.topics = kafka
a1.sources.r1.kafka.consumer.group.id = uek

定义flume的channel----使用基于内存的channel

a1.channels.c1.type = memory
#默认该通道中最大的可以存储的event数量是1000
a1.channels.c1.capacity = 10000
#每次最大可以source中拿到或者送到sink中的event数量也是100
a1.channels.c1.transactionCapacity = 5000

定义sink----console—logger

a1.sinks.k1.type = logger

整合一个flume进程中channel source sink

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
启动

启动flume

[root@node1 data]# flume-ng agent -n a1 -f KafkaToConsole.conf -Dflume.root.logger=INFO,console

生产数据1

[root@node2 ~]# kafka-console-producer.sh --broker-list node1:9092,node2:9092,node3:9092 --topic kafka
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

生产数据2—通过java代码的方式生产

package new_pro;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

// 新版本生产者代码–建议大家使用的版本
public class NewProducer {
public static void main(String[] args) {
Properties props = new Properties();
// Kafka服务端的主机名和端口号
props.put(“bootstrap.servers”, “192.168.200.111:9092, 192.168.200.112:9092, 192.168.200.113:9092”);
// 等待所有副本节点的应答
props.put(“acks”, “all”);
// 消息发送最大尝试次数
props.put(“retries”, 0);
// 一批消息处理大小
props.put(“batch.size”, 16384);
// 请求延时
props.put(“linger.ms”, 1);
// 发送缓存区内存大小
props.put(“buffer.memory”, 33554432);
// key序列化
props.put(“key.serializer”, “org.apache.kafka.common.serialization.StringSerializer”);
// value序列化
props.put(“value.serializer”, “org.apache.kafka.common.serialization.StringSerializer”);

    KafkaProducer<Object, Object> producer = new KafkaProducer<Object, Object>(props);
    for (int i = 0; i < 100; i++) {
        producer.send(new ProducerRecord<Object, Object>("kafka", "hello--"+i));
    }
    // 必须有下面的语句才行,二选一
    // producer.commitTransaction();
    producer.close();
}

}
2. Kafka充当Flume的channel缓存管道,相当于是将Flume采集的数据源的数据先存放到kafka当中,此时Flume相当于是Kafka的生产者
.config文件(netcatToLogger.conf)
#sources别名:r1
a1.sources = r1
#sink别名:k1
a1.sinks = k1
#channel别名:c1
a1.channels = c1

定义flume的source数据源 —netcat

a1.sources.r1.type = netcat
#监听的主机ip:
a1.sources.r1.bind = node1
#监听的端口:
a1.sources.r1.port = 44444

定义flume的channel----使用基于kafka的channel

a1.channels.c1.type = org.apache.flume.channel.kafka.KafkaChannel
a1.channels.c1.kafka.bootstrap.servers = node1:9092,node2:9092,node3:9092
a1.channels.c1.kafka.topic = flume
a1.channels.c1.kafka.consumer.group.id = flume-consumer

定义sink----console—logger

a1.sinks.k1.type = logger

整合一个flume进程中channel source sink

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
启动
[root@node1 data]# flume-ng agent -n a1 -f netcatToLogger.conf -Dflume.root.logger=INFO,console

[root@node2 ~]# telnet node1 44444
3. 最常用的整合方式:Kafka充当Flume的sink下沉地,也就意味着需要将Flume采集的数据源的数据采集到Kafka中,此时Flume相当于是Kafka的生产者
.conf文件(netcatToKafka.conf)
#sources别名:r1
a1.sources = r1
#sink别名:k1
a1.sinks = k1
#channel别名:c1
a1.channels = c1

定义flume的source数据源 —netcat

a1.sources.r1.type = netcat
#监听的主机ip:
a1.sources.r1.bind = node1
#监听的端口:
a1.sources.r1.port = 44444

定义flume的channel----使用基于kafka的channel

a1.channels.c1.type = org.apache.flume.channel.kafka.KafkaChannel
a1.channels.c1.kafka.bootstrap.servers = node1:9092,node2:9092,node3:9092
a1.channels.c1.kafka.topic = flume
a1.channels.c1.kafka.consumer.group.id = flume-consumer

定义sink----console—logger

a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.topic = student
a1.sinks.k1.kafka.bootstrap.servers = node1:9092,node2:9092,node3:9092
a1.sinks.k1.kafka.flumeBatchSize = 200
a1.sinks.k1.kafka.producer.acks = 1

整合一个flume进程中channel source sink

a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1
启动
[root@node1 data]# flume-ng agent -n a1 -f netcatToKafka.conf -Dflume.root.logger=INFO,console

[root@node2 ~]# telnet node1 44444

[root@node3 ~]# kafka-console-consumer.sh --bootstrver node1:9092 --topic student

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flume 可以与 Kafka 进行整合,以实现高效的数据传输和处理。具体步骤如下: 1. 首先需要在 Flume 中配置 Kafka 的相关信息,包括 Kafka 的主机名、端口号、topic 等信息。 2. 在 Flume 的配置文件中,需要添加 Kafka Sink,以将数据发送到 Kafka 中。在 Sink 的配置中,需要指定 Kafka 的相关信息,以及数据的序列化方式等。 3. 启动 Flume Agent,即可将数据传输到 Kafka 中。 下面是一个示例配置文件,将 Flume 中的数据发送到 Kafka 中: ```properties # Name the components on this agent a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = netcat a1.sources.r1.bind = localhost a1.sources.r1.port = 44444 # Describe the sink a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink a1.sinks.k1.topic = test a1.sinks.k1.brokerList = localhost:9092 a1.sinks.k1.batchSize = 20 a1.sinks.k1.requiredAcks = 1 a1.sinks.k1.channel = c1 # 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 ``` 在上述配置文件中,首先定义了一个 netcat Source,用于接收数据。然后定义了一个 Kafka Sink,将数据发送到 Kafka 中。最后定义了一个 memory Channel,用于在内存中缓存事件。 启动 Flume Agent 的命令如下: ``` $ flume-ng agent -c conf -n a1 -f conf/flume_kafka.conf >/dev/null 2>&1 & ``` 其中,-c 参数指定了配置文件所在的目录,-n 参数指定了 Agent 的名称,-f 参数指定了配置文件的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值