0

I'm learning the Apache Flink. Here is the Hello World of Flink: https://ci.apache.org/projects/flink/flink-docs-stable/getting-started/tutorials/local_setup.html

This example is a program, which counts the words in every 5 seconds.

If we want to run this sample, we need to do the following steps:

  • Execute nc -l 9000 on one terminal (A);
  • Execute ./bin/flink run examples/streaming/SocketWindowWordCount.jar --port 9000 on another terminal (B);
  • Go to the terminal A and type some words.

If we Ctrl-c on the terminal A, this sample will be terminated.

I want to know if it is possible to type the words programmatically at the terminal A. For example, I want to type the word lol per second at the terminal A, what should I do?

The code below won't work.

#!/bin/bash

while true; do
    echo 'lol' | nc -l 9000
    sleep 1
done

Of course, I may try to modify the SocketWindowWordCount.java to do so but for now, for some reason, I can not change the java code.

0
3

Pipe the whole loop into nc:

while true; do
    echo 'lol'
    sleep 1
done | nc -l 9000

This will start a single instance of nc, listening for connections on port 9000, and send “lol” once per second to it.

Note that “lol”s will accumulate until the connection is opened, so you might see a number of “lol”s send immediately on connection. You could add a delay at the start:

(sleep 5
 while true; do
     echo 'lol'
     sleep 1
 done) | nc -l 9000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.