05 kafka 如何存储较大数据记录

前言

此问题是最近碰到的一个问题

最近的一个项目, 单条记录的大小 超过了 1M, 然后 使用 kafka 存储数据出现了各种问题

一些问题很容易发现问题, 但是一些问题 很隐蔽

这里 特此记录一下, 篇幅不会很长

测试用例

测试用例如下, 问题主要分为两个地方, 其一是客户端这边的限制, 其二是服务器那边的限制

其中 服务器那边的限制 出现的问题较为隐蔽

复制代码
package com.hx.test14;

/**
 * Test10KafkaProducer
 *
 * @author Jerry.X.He
 * @version 1.0
 * @date 2023-03-12 10:15
 */
public class Test10KafkaProducer {

    // Test06KafkaProducer
    public static void main(String[] args) throws Exception {

        Properties properties = new Properties();
        properties.put("bootstrap.servers", "192.168.0.116:9092");
        properties.put("acks", "all");
        properties.put("retries", 0);
        properties.put("batch.size", 16384);
        properties.put("linger.ms", 1);
        properties.put("buffer.memory", 33554432);
        properties.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        properties.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
//        properties.put("max.request.size", 10485760);

        String topic = "test20230312";
        String path = "/Users/jerry/Jobs/14_lzxm/xx/realdata.json";
        String content = Tools.getContent(path);
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);
        long start = System.currentTimeMillis();
        for (int i = 1; i <= 10; i++) {
            Future<RecordMetadata> respFuture = kafkaProducer.send(new ProducerRecord<>(topic, content));
            System.out.println("message" + i);
        }
        long spent = System.currentTimeMillis() - start;
        System.out.println(" spent " + spent + " ms ");
        kafkaProducer.close();

//
    }

}

客户端这边的限制

客户端这边报错如下

复制代码
java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.RecordTooLargeException: The message is 1880363 bytes when serialized which is larger than the maximum request size you have configured with the max.request.size configuration.

主要是客户端这边 本地对于消息大小的校验 使用的配置是 max.request.size

然后 默认值是 1M, 我们这里没有手动配置, 因此是 默认值 1M

但是实际消息大小在 1.7M 左右, 因此 抛出了异常

解决方式, ProducerConfig 增加 max.request.size 的配置

服务器那边的限制

客户端这边生产线消息之后, 同步获取服务器的反馈, 得到信息如下

但是 服务器那边 没有报错消息, 因此 较为隐蔽

复制代码
Exception in thread "main" java.util.concurrent.ExecutionException: org.apache.kafka.common.errors.RecordTooLargeException: The request included a message larger than the max message size the server will accept.
	at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.valueOrError(FutureRecordMetadata.java:98)
	at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.get(FutureRecordMetadata.java:67)
	at org.apache.kafka.clients.producer.internals.FutureRecordMetadata.get(FutureRecordMetadata.java:30)
	at com.hx.test14.Test10KafkaProducer.main(Test10KafkaProducer.java:41)
Caused by: org.apache.kafka.common.errors.RecordTooLargeException: The request included a message larger than the max message size the server will accept.

服务器这边处理如下, 有一个 消息大小的限制

读取的配置是服务器的 message.max.bytes 的配置

调整了了这个配置之后, 服务器这边的校验就过了

客户端这边拿到存储的记录如下

问了一下 chatgpt

和本文的梳理出来的东西一致, 但是 开销却比 人类 大脑快多了

相关推荐
Re_zero2 小时前
线上日志被清空?这段仅10行的 IO 代码里竟然藏着3个毒瘤
java·后端
洋洋技术笔记2 小时前
Spring Boot条件注解详解
java·spring boot
程序员清风20 小时前
程序员兼职必看:靠谱软件外包平台挑选指南与避坑清单!
java·后端·面试
皮皮林5511 天前
利用闲置 Mac 从零部署 OpenClaw 教程 !
java
华仔啊1 天前
挖到了 1 个 Java 小特性:var,用完就回不去了
java·后端
SimonKing1 天前
SpringBoot整合秘笈:让Mybatis用上Calcite,实现统一SQL查询
java·后端·程序员
DemonAvenger2 天前
Kafka性能调优:从参数配置到硬件选择的全方位指南
性能优化·kafka·消息队列
日月云棠2 天前
各版本JDK对比:JDK 25 特性详解
java
用户8307196840822 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
JavaGuide2 天前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code