---
metadata:
  - name: generator
    content: Diplodoc Platform v5.70.2
alternate:
  - https://ytsaurus.tech/docs/en/flow/java/examples/wordcount.md
  - https://ytsaurus.tech/docs/ru/flow/java/examples/wordcount.md
  - href: https://ytsaurus.tech/docs/en/flow/java/examples/wordcount.md
    type: text/markdown
    title: Markdown version
  - href: https://ytsaurus.tech/docs/en/llms.txt?revision=0987aa47985046d89a7b7ec44c4bbc08e6a3b783
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://ytsaurus.tech/docs/en/llms.txt?revision=0987aa47985046d89a7b7ec44c4bbc08e6a3b783

<!-- source: en/_includes/flow/java/examples/wordcount.md -->
# Word Count in YTsaurus Flow (Java)

The [pipeline](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#pipeline) reads a [stream](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#stream-and-computation) of words and counts how many times each word appears, using a YSON state. This example shows how to configure a [companion](https://ytsaurus.tech/docs/en/flow/concepts/glossary.md#companion) with Spring Boot.

[Source code (Java)](https://github.com/ytsaurus/ytsaurus/tree/main/yt/yt/flow/examples/java/word_count)
[Source code (Kotlin)](https://github.com/ytsaurus/ytsaurus/tree/main/yt/yt/flow/examples/kotlin/word_count)

## Components

### WordCountApplication

This is the Spring Boot application entry point:

{% list tabs group=lang %}

- Java

  ```java
  @SpringBootApplication
  public class WordCountApplication {
      public static void main(String[] args) {
          new SpringApplicationBuilder(WordCountApplication.class)
                  .run(args);
      }
  }
  ```

- Kotlin

  ```kotlin
  @SpringBootApplication
  open class WordCountApplication {
      companion object {
          @JvmStatic
          fun main(args: Array<String>) {
              SpringApplicationBuilder(WordCountApplication::class.java)
                  .run(*args)
          }
      }
  }
  ```

{% endlist %}

### Stream registration

Typed streams are registered declaratively. `Word` is the message model for the `words` stream: `@FlowMessage` associates it with that stream, and `@Entity` marks it as a JPA entity. Spring Boot scans and registers these classes. The `mapper` computation is registered with `@FlowComputation` on `WordCountMapper`:

{% list tabs group=lang %}

- Java

  ```java
  @Entity
  @FlowMessage(streamIds = {"words"})
  public class Word {
      private String word;
  
      public Word() {
      }
  
      public Word(String word) {
          this.word = word;
      }
  
      public String getWord() {
          return word;
      }
  
      public void setWord(String word) {
          this.word = word;
      }
  }
  ```

- Kotlin

  ```kotlin
  @Entity
  @FlowMessage(streamIds = ["words"])
  class Word {
      var word: String? = null
  
      constructor()
  
      constructor(word: String?) {
          this.word = word
      }
  }
  ```

{% endlist %}

- `reader` is a source computation without a process function; the C++ worker reads and parses its input.
- `mapper` is implemented by `WordCountMapper` with `@FlowComputation(id = "mapper")`.
- `@FlowMessage(streamIds = {"words"})` associates the Java class with the stream; the Kotlin form uses `streamIds = ["words"]`.

### WordCountMapper

This is the processing function that counts words using the [YsonStateAccessor](https://ytsaurus.tech/docs/en/flow/java/state.md#yson-state):

{% list tabs group=lang %}

- Java

  ```java
      @Override
      public void onMessage(ExtendedMessage message, OutputCollector output, RuntimeContext ctx) {
          Word input = message.getPayload();
          StateAccessor<WordCountState> stateAccessor = ctx.getState(WORD_STATE, message);
          var state = stateAccessor.getOrDefault(new WordCountState(input.getWord(), 0));
          state.setCount(state.getCount() + 1);
      }
  ```

- Kotlin

  ```kotlin
      override fun onMessage(message: ExtendedMessage, output: OutputCollector, ctx: RuntimeContext) {
          val input: Word = message.getPayload()
          val stateAccessor = ctx.getState(WORD_STATE, message)
          val state = stateAccessor.getOrDefault(WordCountState(input.word, 0))
          state.count += 1
      }
  ```

{% endlist %}

The `@FlowComputation(id = "mapper")` annotation registers the class as a computation and makes it a Spring bean (it’s meta-annotated with `@Component`).

## Key patterns

- **Spring Boot auto-config**: you don’t need to manually create `PipelineContext` and `GrpcServerExecution`.
- **@FlowComputation**: the processing function becomes both a Spring bean and a computation; you can use dependency injection.
- **@FlowMessage**: associates a message model with a named stream.

## Running

The `WordCountApplication` class runs in two modes. With `YT_FLOW_MODE` unset, it starts the pipeline through the runner:

```bash
./run.sh tech.ytsaurus.flow.examples.wordcount.WordCountApplication --config pipeline.yson --flow-bin flow_server
```

When a worker starts the class configured as `main_class` with `YT_FLOW_MODE=Worker`, it runs as the companion process and starts the gRPC server.
<!-- endsource: en/_includes/flow/java/examples/wordcount.md -->

<!-- source: en/_includes/flow/java/examples/wordcount_also.md -->
## See also

- [Quick start](https://ytsaurus.tech/docs/en/flow/java/getting-started.md)
- [Computation](https://ytsaurus.tech/docs/en/flow/java/computation.md)
- [Stateful processing](https://ytsaurus.tech/docs/en/flow/concepts/stateful.md)
<!-- endsource: en/_includes/flow/java/examples/wordcount_also.md -->
