Apache Kafka & Stream Processing Fundamentals · บทเรียน

คำค้นแบบโต้ตอบและคลังสถานะ

เรียนรู้ว่า Kafka Streams เปิดให้ค้นหาคลังสถานะภายในโดยตรงได้อย่างไร ทำให้แอปกระแสข้อมูลกลายเป็นมุมมองที่สร้างเป็นข้อมูลจริงและมีเวลาแฝงต่ำ

บทเรียน 4 จาก 413 ขั้นตอน

คำค้นแบบโต้ตอบและคลังสถานะ เป็นบทเรียน Apache Kafka & Stream Processing Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Apache Kafka & Stream Processing Fundamentals และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Apache Kafka & Stream Processing Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

State Stores Recap

Stateful operations like aggregations and joins keep their data in state stores — local key-value stores backed by changelog topics for fault tolerance.

Normally results flow out to a topic, but they also live right inside your app.

What Are Interactive Queries?

Interactive Queries (IQ) let your application read those local state stores directly — no extra database, no re-consuming a topic.

Your streaming app effectively becomes a queryable materialized view.

Naming a Store

To query a store you must name it during materialization.

KTable<String, Long> counts = builder
    .stream("clicks")
    .groupByKey()
    .count(Materialized.as("clicks-store"));

Getting a Read Handle

After the app is running, fetch a read-only view of the store from the KafkaStreams instance.

ReadOnlyKeyValueStore<String, Long> store =
    streams.store(StoreQueryParameters.fromNameAndType(
        "clicks-store",
        QueryableStoreTypes.keyValueStore()));

Point Lookups & Range Scans

Once you have the store, query it like a map.

Long value = store.get("user-42");

KeyValueIterator<String, Long> all = store.all();
while (all.hasNext()) {
  KeyValue<String, Long> kv = all.next();
}
all.close();

The Distribution Problem

State is partitioned across app instances. A given key lives on only one instance.

If you query the wrong instance, you won't find the key — so the app needs to know who owns each key.

Discovering Key Owners

Kafka Streams can tell you which instance hosts a key, given the store name and key serializer.

KeyQueryMetadata meta = streams.queryMetadataForKey(
    "clicks-store", "user-42", Serdes.String().serializer());
HostInfo host = meta.activeHost();

Exposing application.server

Set application.server so each instance advertises its host and port. This metadata powers cross-instance routing.

props.put(StreamsConfig.APPLICATION_SERVER_CONFIG,
    "node1.internal:8080");

Building a Query REST Layer

A typical pattern: wrap the app in an HTTP server. On a request, find the owning host. If it's local, read the store; otherwise proxy to the remote instance.

Handling Rebalances

During rebalances, stores may be migrating and temporarily unavailable, raising InvalidStateStoreException.

  • Retry with backoff.
  • Check KafkaStreams.State.RUNNING before querying.

When to Use IQ

Interactive Queries shine when you want:

  • Low-latency reads of aggregated state.
  • To avoid a separate serving database.
  • A self-contained, scalable materialized view.

For complex ad-hoc queries, a dedicated store may still be better.

Quick Check

Test your understanding of interactive queries.

Recap

You learned Interactive Queries.

  • Name a store, then get a read-only handle from KafkaStreams.
  • State is partitioned; use queryMetadataForKey to find owners.
  • Set application.server and proxy cross-instance requests.
  • Handle rebalance exceptions with retries.
เริ่มต้นได้ฟรี

เรียนรู้ 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 Streams เปิดให้ค้นหาคลังสถานะภายในโดยตรงได้อย่างไร ทำให้แอปกระแสข้อมูลกลายเป็นมุมมองที่สร้างเป็นข้อมูลจริงและมีเวลาแฝงต่ำ คุณปฏิบัติ 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 ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การดำเนินการแบ่งช่วงเวลาใน Kafka Streams
  2. การเชื่อมรวมและการรวมข้อมูลในสตรีม
  3. บทนำสู่ KSQL สำหรับการวิเคราะห์สตรีม
  4. คำค้นแบบโต้ตอบและคลังสถานะ
← กลับไปที่ Apache Kafka & Stream Processing Fundamentals