1. 开启进程
| [node123]systemctl stop firewalld |
| |
| [node123]zkServer.sh start |
| |
| [node123]kafka-server-start.sh /opt/app/kafka-0.11.0.0/config/server.properties & |
| |
| [node1]kafka-console-consumer.sh --zookeeper node1:2181 --topic demo |
2. java导入依赖
| <?xml version="1.0" encoding="UTF-8"?> |
| <project xmlns="http://maven.apache.org/POM/4.0.0" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| <modelVersion>4.0.0</modelVersion> |
| |
| <groupId>org.example</groupId> |
| <artifactId>kafkacode</artifactId> |
| <version>1.0-SNAPSHOT</version> |
| |
| <dependencies> |
| <dependency> |
| <groupId>org.apache.kafka</groupId> |
| <artifactId>kafka-clients</artifactId> |
| <version>0.11.0.0</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.kafka</groupId> |
| <artifactId>kafka_2.12</artifactId> |
| <version>0.11.0.0</version> |
| </dependency> |
| </dependencies> |
| </project> |
3. java代码
| package new_partition_pro; |
| |
| import org.apache.kafka.clients.producer.Callback; |
| import org.apache.kafka.clients.producer.KafkaProducer; |
| import org.apache.kafka.clients.producer.ProducerRecord; |
| import org.apache.kafka.clients.producer.RecordMetadata; |
| |
| import java.util.Properties; |
| |
| |
| public class NewProducerPartitionAndCallBack { |
| public static void main(String[] args) { |
| |
| Properties prop = new Properties(); |
| prop.put("bootstrap.servers", "192.168.200.111:9092,192.168.200.112:9092,192.168.200.113:9092"); |
| |
| prop.put("key.serializer", "org.apache.kafka.common.serialization.IntegerSerializer"); |
| |
| prop.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); |
| |
| |
| |
| |
| prop.put("partitioner.class","new_partition_pro.CustomPartition"); |
| |
| |
| KafkaProducer<Integer, String> producer = new KafkaProducer<Integer, String>(prop); |
| |
| |
| for (int i = 0; i < 100; i++) { |
| ProducerRecord<Integer, String> record = new ProducerRecord<Integer, String>("demo", i, "hello" + i); |
| |
| final int finalI = i; |
| |
| |
| |
| |
| |
| |
| producer.send(record, new Callback() { |
| public void onCompletion(RecordMetadata recordMetadata, Exception e) { |
| System.out.println("当前这个数据的分区为:"+recordMetadata.partition() + "---offset:" + recordMetadata.offset()); |
| System.out.println("当前的key为:" + finalI); |
| } |
| }); |
| } |
| |
| |
| |
| |
| producer.flush(); |
| } |
| } |
| package new_partition_pro; |
| |
| import org.apache.kafka.clients.producer.Partitioner; |
| import org.apache.kafka.common.Cluster; |
| |
| import java.util.Map; |
| |
| public class CustomPartition implements Partitioner { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public int partition(String s, Object key, byte[] bytes, Object value, byte[] bytes1, Cluster cluster) { |
| |
| |
| |
| |
| |
| |
| int num = Integer.parseInt(key.toString()); |
| if (num % 3 == 0) { |
| return 0; |
| } else if (num % 4 == 1) { |
| return 1; |
| } else { |
| return 2; |
| } |
| } |
| |
| public void close() { |
| |
| } |
| |
| public void configure(Map<String, ?> map) { |
| |
| } |
| } |
4. 效果图

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?