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

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

完

相关推荐
若亦_36 分钟前
Java中关于this的三种用法
java·开发语言·intellij-idea
山甫aa38 分钟前
锁一篇搞定:乐观锁、悲观锁、CAS、数据库锁与分布式锁【从零开始的 Web 后端学习】
java·数据库·分布式·后端·学习·web
软件技术NINI41 分钟前
基于SpringBoot和Vue的博物馆预约平台管理系统的设计与实现
java·毕业设计·毕设
独泪了无痕10 小时前
探索Pinyin4j-Java中的拼音转换利器
java·后端
道尔柯南11 小时前
C++ string 类详解:从常用接口到深拷贝,彻底掌握字符串处理
android·java·c++
kingx312 小时前
Bugku 3122 谷歌小恐龙题解:纯离线逆向 Vault 的 AES-GCM 加密资源(附 Python 解密器)
java·逆向·ctf·bugku·aes-gcm
DevOpenClub15 小时前
职教高考控制线 API:招生类别枚举、筛选与空分数含义
java·数据库·高考
露天赏雪16 小时前
第3篇:Sentinel熔断降级实战:大模型接口抖动时的服务兜底方案
java·开发语言·人工智能·spring boot·spring·servlet·sentinel
天天被压力16 小时前
【A股分钟级高频数据完全手册 #06】北交所分钟级数据接口:京市股票Pro版最新分时
java·人工智能·python
天天被压力16 小时前
【A股分钟级高频数据完全手册 #05】指数历史分钟数据接口:Pro版沪深指数分时回放
java·人工智能·python