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

การดำเนินการแบบไร้สถานะเทียบกับแบบมีสถานะ

ทำความเข้าใจวิธีที่ Kafka Streams ประมวลผลข้อมูลทั้งแบบรักษาและไม่รักษาสถานะภายในระหว่างระเบียน

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

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

Stateless vs. Stateful Streams

In Kafka Streams, how you process data falls into two main categories: stateless and stateful operations.

Understanding this distinction is crucial for building efficient and correct real-time data pipelines. It impacts how your application remembers (or forgets) past events.

What are Stateless Operations?

Stateless operations process each incoming record independently. They don't remember any past records or maintain an internal state across events.

Think of it like a simple function: you give it an input, and it produces an output, without needing any memory of previous inputs.

Stateless Example: Map & Filter

Common stateless operations include map, filter, flatMap, and peek. They transform or filter records one by one.

Here's a simple Kafka Streams app using mapValues to convert all incoming message values to uppercase:

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.KStream;

import java.util.Properties;

public class StatelessMapApp {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG,
              "stateless-map-app");
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG,
              "localhost:9092");
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_BY_KEY_CLASS_CONFIG,
              Serdes.String().getClass());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_BY_KEY_CLASS_CONFIG,
              Serdes.String().getClass());

    StreamsBuilder builder = new StreamsBuilder();
    KStream<String, String> source = builder.stream("input-topic");

    // Stateless operation: mapValues
    KStream<String, String> upperCaseStream =
        source.mapValues(value -> value.toUpperCase());

    upperCaseStream.to("output-topic");

    Topology topology = builder.build();
    KafkaStreams streams = new KafkaStreams(topology, props);
    streams.start();
    // In a real app, add a shutdown hook.
  }
}

When to Use Stateless Operations

Stateless operations are ideal for:

  • Simple Transformations: Changing data format, type conversion.
  • Filtering: Removing unwanted records.
  • Data Cleansing: Basic sanitization of individual records.

They are generally simpler to implement and have less overhead because no state needs to be managed.

What are Stateful Operations?

Stateful operations are those that need to remember past records or combine information across multiple records to produce a result.

They maintain an internal state, which is stored locally within the Kafka Streams application instance. This state allows them to perform aggregations, joins, and windowing.

Stateful Example: Counting Events

A classic example of a stateful operation is count(). To count events per key, the application must remember previous counts for each key.

This operation transforms a KStream into a KTable, which represents a changelog of aggregated results.

import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;

import java.util.Properties;

public class StatefulCountApp {
  public static void main(String[] args) {
    Properties props = new Properties();
    props.put(StreamsConfig.APPLICATION_ID_CONFIG,
              "stateful-count-app");
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG,
              "localhost:9092");
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_BY_KEY_CLASS_CONFIG,
              Serdes.String().getClass());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_BY_KEY_CLASS_CONFIG,
              Serdes.String().getClass());

    StreamsBuilder builder = new StreamsBuilder();
    KStream<String, String> source = builder.stream("input-topic");

    // Stateful operation: count by key
    KTable<String, Long> countsTable = source
        .groupByKey()
        .count(Materialized.as("counts-store")); // A named state store

    countsTable.toStream().to("output-topic");

    Topology topology = builder.build();
    KafkaStreams streams = new KafkaStreams(topology, props);
    streams.start();
    // In a real app, add a shutdown hook.
  }
}

More Stateful Operations

Besides count(), other common stateful operations include:

  • Aggregations: reduce(), aggregate() (e.g., calculating sums, averages).
  • Joins: Combining data from two streams or a stream and a table based on a common key.
  • Windowing: Grouping records that fall within a defined time frame (e.g., 5-minute window).

These operations all rely on maintaining state to function correctly.

Kafka Streams State Stores

Kafka Streams manages state using internal state stores. These are typically backed by a local key-value store like RocksDB.

For fault tolerance, Kafka Streams also uses internal Kafka topics (called changelog topics) to continuously back up the state store. If an application instance fails, its state can be restored from the changelog topic by a new instance.

Quick Check: Identify Operations

Which of these Kafka Streams operations is considered stateless?

Recap: Stateless vs. Stateful

We've explored the key differences between stateless and stateful operations in Kafka Streams:

  • Stateless: Processes records individually, no memory of past events. Ideal for simple transformations and filtering.
  • Stateful: Requires internal memory (state stores) to combine or remember information across records. Essential for aggregations, joins, and windowing.

Choosing the right type of operation is fundamental to designing robust and efficient stream processing applications.

คำถามที่พบบ่อย

บทเรียน “การดำเนินการแบบไร้สถานะเทียบกับแบบมีสถานะ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การดำเนินการแบบไร้สถานะเทียบกับแบบมีสถานะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การดำเนินการแบบไร้สถานะเทียบกับแบบมีสถานะ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Apache Kafka & Stream Processing Fundamentals นี้ได้ไหม

ได้ บทเรียน Apache Kafka & Stream Processing Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การสร้างแอป Kafka Streams อย่างง่าย
  2. แนวคิด KStream และ KTable
  3. การดำเนินการแบบไร้สถานะเทียบกับแบบมีสถานะ
  4. Serdes และการทำให้ข้อมูลเป็นอนุกรมใน Kafka Streams
← กลับไปที่ Apache Kafka & Stream Processing Fundamentals