Word Count in YTsaurus Flow (Java)

The pipeline reads a stream of words and counts how many times each word appears, using a YSON state. This example shows how to configure a companion with Spring Boot.

Source code (Java)
Source code (Kotlin)

Components

WordCountApplication

This is the Spring Boot application entry point:

@SpringBootApplication
public class WordCountApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(WordCountApplication.class)
                .run(args);
    }
}
@SpringBootApplication
open class WordCountApplication {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            SpringApplicationBuilder(WordCountApplication::class.java)
                .run(*args)
        }
    }
}

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:

@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;
    }
}
@Entity
@FlowMessage(streamIds = ["words"])
class Word {
    var word: String? = null

    constructor()

    constructor(word: String?) {
        this.word = word
    }
}
  • 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:

    @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);
    }
    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
    }

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:

./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.

See also