Apache Kafka & Stream Processing Fundamentals · درس

مفاتيح الرسائل واستراتيجيات التقسيم

تعلّم كيف يستخدم Kafka مفاتيح الرسائل لتوجيه السجلات إلى الأقسام، وكيف يضمن ذلك الترتيب، وكيف تصمم المفاتيح ومقسّمات مخصّصة.

الدرس 4 من 413 خطوة

مفاتيح الرسائل واستراتيجيات التقسيم درس مجاني في Apache Kafka & Stream Processing Fundamentals على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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) % numPartitions

No 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 ordered

Choosing 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.VipPartitioner

Partition 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 مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «مفاتيح الرسائل واستراتيجيات التقسيم» مجاني؟

نعم — نص درس «مفاتيح الرسائل واستراتيجيات التقسيم» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Apache Kafka & Stream Processing Fundamentals، انتقل إلى CoddyKit PRO. تتضمن دورة Apache Kafka & Stream Processing Fundamentals 4 دروس في المجموع.

ماذا ستتعلم في «مفاتيح الرسائل واستراتيجيات التقسيم»؟

تعلّم كيف يستخدم Kafka مفاتيح الرسائل لتوجيه السجلات إلى الأقسام، وكيف يضمن ذلك الترتيب، وكيف تصمم المفاتيح ومقسّمات مخصّصة. تتمرن على Apache Kafka & Stream Processing Fundamentals مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. إنتاج الرسائل إلى Kafka
  2. استهلاك الرسائل من Kafka
  3. فهم الأقسام والإزاحات
  4. مفاتيح الرسائل واستراتيجيات التقسيم
← العودة إلى Apache Kafka & Stream Processing Fundamentals