Link Search Menu Expand Document

Get Started

Table of contents

  1. Setup the environment
  2. Create a new Kotlin project
  3. Configure the project
  4. Create your first pipeline
  5. Run the job
  6. Inspect the results
  7. What’s next

Setup the environment

Before starting, you should check your development environment:

  1. Download and install the Java Development Kit (JDK) version 8. Verify that the JAVA_HOME environment variable points to your JDK installation.
  2. Download and install Apache Maven by following Maven’s installation guide.
We recommend using IntelliJ IDEA to just create a new project from the archetype (see the next section).

Create a new Kotlin project

Use Maven to create a new Kotlin project via the following command:

$ mvn archetype:generate \
      -DarchetypeGroupId=org.jetbrains.kotlin \
      -DarchetypeArtifactId=kotlin-archetype-jvm \
      -DarchetypeVersion=1.3.72 \
      -DgroupId=org.example \
      -DartifactId=kio-word-count \
      -Dversion="0.1" \
      -Dpackage=org.example.kio \
      -DinteractiveMode=false

This script will create a new directory kio-word-count with pom.xml and some other files.

Configure the project

At the next step it’s needed to add the following lines to the pom.xml file:

  • Property for Kotlin compiler to set target JVM version (into the properties block):
    <kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
    
  • Dependency description for Kio (into the dependencies block):
    <dependency>
        <groupId>ru.chermenin.kio</groupId>
        <artifactId>kio-core</artifactId>
        <version>x.y.z</version>
    </dependency>
    

Create your first pipeline

  1. Add a new file WordCount.kt to the /src/main/kotlin/org/example/kio directory.
  2. Edit it to define a new WordCount object and the main method:
    package org.example.kio
    object WordCount {
      @JvmStatic
      fun main(args: Array<String>) {
      }
    }
    
  3. Create the Kio context:
    val kio = Kio.fromArguments(args)
    
  4. Read lines from the inputs:
    val lines = kio.read().text(kio.arguments.required("input"))
    
  5. Split line to words:
    val words = lines.flatMap { it.split("\\W+".toRegex()) }
    
  6. Filter empty values:
    val filtered = words.filter { it.isNotBlank() }
    
  7. Count words:
    val counts = filtered.countByValue()
    
  8. Format the results:
    val results = counts.map { "${it.key}: ${it.value}" }
    
  9. Write the results to the output:
    results.write().text(kio.arguments.required("output"))
    
  10. And execute the pipeline:
    kio.execute().waitUntilFinish()
    

Run the job

To execute the job run the command with input and output arguments:

$ mvn compile exec:java -Dexec.mainClass=org.example.kio.WordCount \
       -Dexec.args="--input=pom.xml --output=counts"

Inspect the results

Once the pipeline has completed, you can view the output in multiple files prefixed by counts:

$ more counts*
executions: 2
count: 2
scope: 4
main: 1
dependencies: 2
plugins: 2
kotlin: 17
...

What’s next

Learn more about Kio viewing the Developers Guide and feel free to help us with the project.