目录
Kafka实战记录-目录:https://blog.youkuaiyun.com/weixin_39565597/article/details/104402046
1、启动kafka
# 启动kafka kafka-server-start.sh [-daemon] server.properties [--override property=value]*
# -daemon 是否以守护进程开启
# --override property=value 覆盖配置值,属性property改为value
[root@node1 kafka-2.2.2] ./bin/kafka-server-start.sh -daemon ./config/server.properties
# 查看进程
[root@node1 kafka-2.2.2] jps
49145 Kafka
# 关闭kafka
[root@node1 kafka-2.2.2] ./bin/kafka-server-stop.sh
2、创建topic
# 创建一个topic
[root@node1 kafka-2.2.2] ./bin/kafka-topics.sh --zookeeper node1:2181 --create --topic apihello --replication-factor 2 --partitions 2
Created topic "apihello".
[root@node1 kafka-2.2.2] ./kafka-topics.sh --zookeeper node1:2181 --list
apihello
3、生产者
public static void main(String[] args) {
Properties props = new Properties();
// Kafka服务端的主机名和端口号
props.put("bootstrap.servers", "node1: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");
Producer<String, String> producer = new KafkaProducer<>(props);
for (int i = 0; i < 50; i++) {
producer.send(new ProducerRecord<String, String>("apihello", Integer.toString(i), "hello world-" + i),
new Callback() {
@Override
public void onCompletion(RecordMetadata recordMetadata, Exception e) {
if (recordMetadata != null) {
System.err.println(recordMetadata.partition() + "---" + recordMetadata.offset());
}
}
});
}
producer.close();
}
结果:
0---0
0---1
0---2
0---3
0---4
0---5
0---6
0---7
0---8
0---9
0---10
0---11
0---12
0---13
0---14
0---15
0---16
0---17
0---18
0---19
0---20
0---21
0---22
0---23
0---24
0---25
1---0
1---1
1---2
1---3
1---4
1---5
1---6
1---7
1---8
1---9
1---10
1---11
1---12
1---13
1---14
1---15
1---16
1---17
1---18
1---19
1---20
1---21
1---22
1---23
4、消费者
public static void main(String[] args) {
Properties props = new Properties();
// 定义kakfa 服务的地址,不需要将所有broker指定上
props.put("bootstrap.servers", "node1:9092");
// 制定consumer group
props.put("group.id", "test");
// 是否自动确认offset
props.put("enable.auto.commit", "true");
// 自动确认offset的时间间隔
props.put("auto.commit.interval.ms", "1000");
// key的序列化类
props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
// value的序列化类
props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer");
// 定义consumer
KafkaConsumer<String, String> consumer = new KafkaConsumer<>(props);
// 消费者订阅的topic, 可同时订阅多个
consumer.subscribe(Arrays.asList("apihello"));
while (true) {
// 读取数据,读取超时时间为100ms
ConsumerRecords<String, String> records = consumer.poll(100);
for (ConsumerRecord<String, String> record : records) {
System.out.printf("offset = %d, key = %s, value = %s%n", record.offset(), record.key(), record.value());
}
}
}
结果:
offset = 0, key = 0, value = hello world-0
offset = 1, key = 2, value = hello world-2
offset = 2, key = 5, value = hello world-5
offset = 3, key = 6, value = hello world-6
offset = 4, key = 10, value = hello world-10
offset = 5, key = 11, value = hello world-11
offset = 6, key = 12, value = hello world-12
offset = 7, key = 15, value = hello world-15
offset = 8, key = 17, value = hello world-17
offset = 9, key = 18, value = hello world-18
offset = 10, key = 20, value = hello world-20
offset = 11, key = 21, value = hello world-21
offset = 12, key = 23, value = hello world-23
offset = 13, key = 26, value = hello world-26
offset = 14, key = 27, value = hello world-27
offset = 15, key = 34, value = hello world-34
offset = 16, key = 36, value = hello world-36
offset = 17, key = 37, value = hello world-37
offset = 18, key = 38, value = hello world-38
offset = 19, key = 41, value = hello world-41
offset = 20, key = 42, value = hello world-42
offset = 21, key = 43, value = hello world-43
offset = 22, key = 45, value = hello world-45
offset = 23, key = 47, value = hello world-47
offset = 24, key = 48, value = hello world-48
offset = 25, key = 49, value = hello world-49
offset = 0, key = 1, value = hello world-1
offset = 1, key = 3, value = hello world-3
offset = 2, key = 4, value = hello world-4
offset = 3, key = 7, value = hello world-7
offset = 4, key = 8, value = hello world-8
offset = 5, key = 9, value = hello world-9
offset = 6, key = 13, value = hello world-13
offset = 7, key = 14, value = hello world-14
offset = 8, key = 16, value = hello world-16
offset = 9, key = 19, value = hello world-19
offset = 10, key = 22, value = hello world-22
offset = 11, key = 24, value = hello world-24
offset = 12, key = 25, value = hello world-25
offset = 13, key = 28, value = hello world-28
offset = 14, key = 29, value = hello world-29
offset = 15, key = 30, value = hello world-30
offset = 16, key = 31, value = hello world-31
offset = 17, key = 32, value = hello world-32
offset = 18, key = 33, value = hello world-33
offset = 19, key = 35, value = hello world-35
offset = 20, key = 39, value = hello world-39
offset = 21, key = 40, value = hello world-40
offset = 22, key = 44, value = hello world-44
offset = 23, key = 46, value = hello world-46