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

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

相关推荐
专注VB编程开发20年3 小时前
vb.net datatable新增数据时改用数组缓存
java·linux·windows
(>_<)3 小时前
java minio 分片上传工具类与测试demo
java·minio·分片上传
不想打工的码农3 小时前
MyBatis-Plus多数据源实战:被DBA追着改配置后,我肝出这份避坑指南(附动态切换源码)
java·后端
Coder_Boy_4 小时前
Deeplearning4j+ Spring Boot 电商用户复购预测案例
java·人工智能·spring boot·后端·spring
踢足球09294 小时前
寒假打卡:2026-2-7
java·开发语言·javascript
闻哥4 小时前
Kafka高吞吐量核心揭秘:四大技术架构深度解析
java·jvm·面试·kafka·rabbitmq·springboot
金牌归来发现妻女流落街头4 小时前
【Springboot基础开发】
java·spring boot·后端
考琪4 小时前
Nginx打印变量到log方法
java·运维·nginx
wangjialelele4 小时前
Linux中的进程管理
java·linux·服务器·c语言·c++·个人开发
历程里程碑4 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse