คีย์ข้อความและกลยุทธ์การแบ่งพาร์ทิชัน
เรียนรู้ว่า Kafka ใช้คีย์ข้อความเพื่อกำหนดเส้นทางเรกคอร์ดไปยังพาร์ทิชันอย่างไร วิธีนี้รับประกันลำดับได้อย่างไร และวิธีออกแบบคีย์กับตัวแบ่งพาร์ทิชันแบบกำหนดเอง
คีย์ข้อความและกลยุทธ์การแบ่งพาร์ทิชัน เป็นบทเรียน Apache Kafka & Stream Processing Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Apache Kafka & Stream Processing Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Role of the Key
Every Kafka record can carry an optional key. The key is not just data — it determines which partition the record is written to.
Default Partitioner
When a key is present, the default partitioner hashes it and maps the hash to a partition. The same key always lands in the same partition (for a fixed partition count).
partition = hash(key) % numPartitionsNo Key Behavior
If you send records without a key, the producer distributes them across partitions (sticky batching in modern clients) for balanced load — but ordering across partitions is not guaranteed.
Ordering Guarantee
Kafka guarantees ordering only within a partition. By keying related records (e.g. all events for one order id), you guarantee they are processed in order.
send("orders", orderId, event); // all events for an order stay orderedChoosing a Good Key
A good key reflects your ordering and grouping needs.
- User events keyed by userId.
- Order events keyed by orderId.
- Avoid keys with too few distinct values (causes hot partitions).
Hot Partitions
If one key value dominates traffic, its partition becomes a hotspot while others sit idle. Watch key cardinality and distribution to keep load balanced.
Keying in Spring Boot
Pass the key as the second argument to send; Spring forwards it to the partitioner.
kafkaTemplate.send("orders", order.getId(), payload);Custom Partitioner
For special routing logic, implement the Partitioner interface and override partition().
public int partition(String topic, Object key, byte[] keyBytes,
Object value, byte[] valueBytes, Cluster cluster) {
return isVip(key) ? 0 : 1 + (Math.abs(key.hashCode()) % (n - 1));
}Registering the Partitioner
Tell the producer to use your class via configuration.
spring:
kafka:
producer:
properties:
partitioner.class: com.example.VipPartitionerPartition Count and Keys
Changing the partition count changes hash % numPartitions, so existing keys may move to new partitions, breaking ordering. Choose partition count carefully up front.
Putting It Together
Keys are the link between data and partitions. They give you per-key ordering, drive load distribution, and can be customized with a partitioner for advanced routing.
Quick Check
Test your understanding of keys and partitioning.
Recap
You learned message keys and partitioning.
- The key determines the target partition via hashing.
- Same key, same partition, ordered processing.
- Low-cardinality keys cause hot partitions.
- A custom Partitioner enables advanced routing.
เรียนรู้ Apache Kafka & Stream Processing Fundamentals ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “คีย์ข้อความและกลยุทธ์การแบ่งพาร์ทิชัน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “คีย์ข้อความและกลยุทธ์การแบ่งพาร์ทิชัน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Apache Kafka & Stream Processing Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “คีย์ข้อความและกลยุทธ์การแบ่งพาร์ทิชัน”
เรียนรู้ว่า Kafka ใช้คีย์ข้อความเพื่อกำหนดเส้นทางเรกคอร์ดไปยังพาร์ทิชันอย่างไร วิธีนี้รับประกันลำดับได้อย่างไร และวิธีออกแบบคีย์กับตัวแบ่งพาร์ทิชันแบบกำหนดเอง คุณปฏิบัติ Apache Kafka & Stream Processing Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Apache Kafka & Stream Processing Fundamentals หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Apache Kafka & Stream Processing Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “คีย์ข้อความและกลยุทธ์การแบ่งพาร์ทิชัน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Apache Kafka & Stream Processing Fundamentals นี้ได้ไหม
ได้ บทเรียน Apache Kafka & Stream Processing Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การผลิตข้อความไปยัง Kafka
- การใช้ข้อความจาก Kafka
- ทำความเข้าใจพาร์ทิชันและออฟเซ็ต
- คีย์ข้อความและกลยุทธ์การแบ่งพาร์ทิชัน