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

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

相关推荐
雨辰AI2 分钟前
生产级实测:SpringBoot3 + 达梦数据库接口从 200ms 优化至 20ms 完整调优指南
java·数据库·spring boot·后端·政务
(Charon)31 分钟前
【C++ 面试高频:内存管理、RAII 和智能指针详解】
java·开发语言·word
凡人叶枫41 分钟前
Effective C++ 条款39:明智而审慎地使用 private 继承
java·数据库·c++·嵌入式开发
轻刀快马1 小时前
跨越软硬件的共鸣(二):从 Cache 写策略看 Redis 与 DB 的一致性博弈
java·开发语言·redis·计算机组成原理
折哥的程序人生 · 物流技术专研1 小时前
Java 23 种设计模式:从踩坑到精通 | 装饰器模式 —— 比继承更灵活的扩展方式,你用过吗?
java·装饰器模式·java面试·结构型模式·java设计模式·javaio·从踩坑到精通
lili00121 小时前
2026 企业 AI 选型新范式:OpenRouter Fusion 证明多模型融合性价比远超单模型,企业该如何重构技术栈? - 微元算力(weytoken)
java·人工智能·python·重构·ai编程
shushangyun_1 小时前
汽车服务行业B2B平台+AI解决方案哪家专业:2026年最新测评
java·运维·网络·数据库·人工智能·汽车
A.说学逗唱的Coke1 小时前
【大模型专题】Spring AI Alibaba × Skill 整合实战:让 AI 真正“会干活
java·人工智能·spring
大黄说说2 小时前
深入理解 Go 协程 Goroutine:并发编程的核心精髓
java·数据库·python
许彰午2 小时前
38_Java设计模式之装饰器模式
java·设计模式·装饰器模式