Borja
Borja

Reputation: 204

How to load Flink Streaming data from Zeppelin

I'm starting to use Flink on Zeppelin and trying to run the simplest program in streaming: wordcount. When I run this code in local mode using the terminal, it works.

Here is how I do it: https://ci.apache.org/projects/flink/flink-docs-release-1.2/quickstart/setup_quickstart.html

This is the code:

object SocketWindowWordCount {

  /** Main program method */
  def main(args: Array[String]) : Unit = {

    // the host and the port to connect to
    var hostname: String = "localhost"
    var port: Int = 0

    try {
      val params = ParameterTool.fromArgs(args)
      hostname = if (params.has("hostname")) params.get("hostname") else "localhost"va
      port = params.getInt("port")
    } catch {
      case e: Exception => {
        System.err.println("No port specified. Please run 'SocketWindowWordCount " +
          "--hostname <hostname> --port <port>', where hostname (localhost by default) and port " +
          "is the address of the text server")
        System.err.println("To start a simple text server, run 'netcat -l <port>' " +
          "and type the input text into the command line")
        return
      }
    }

    // get the execution environment
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    // get input data by connecting to the socket
    val text = env.socketTextStream(hostname, port, '\n')

    // parse the data, group it, window it, and aggregate the counts 
    val windowCounts = text
          .flatMap { w => w.split("\\s") }
          .map { w => WordWithCount(w, 1) }
          .keyBy("word")
          .timeWindow(Time.seconds(5))
          .sum("count")

    // print the results with a single thread, rather than in parallel
    windowCounts.print().setParallelism(1)

    env.execute("Socket Window WordCount")
  }

  /** Data type for words with count */
  case class WordWithCount(word: String, count: Long)
}

However, when I try to run this code on Zeppelin... I do not how to do it. I guess I need to open a socket in terminal too but I do not how make that Flink connect with this port. Should I open several windows on Flink?

I can summary the question in: how to load flink streaming data from Zeppelin???

I need add some Scala method at the end like:

import scala.io.Source

val programRunning = Source.from"Socket"...?

Thanks in advance for your help! :)

Upvotes: 2

Views: 1100

Answers (1)

David Anderson
David Anderson

Reputation: 43524

In a terminal, you can use

nc -lk 9999

to establish a service bound to port 9999 that will send whatever it receives on stdin to its clients.

Apache Zeppelin: A friendlier way to Flink might also be helpful.

Upvotes: 1

Related Questions