如何将System.console用作Apache Flink源

时间:2019-01-18 17:20:11

标签: java apache-flink flink-streaming

Apache Flink提供了许多不同的流媒体源,但我想知道是否可以将控制台用作数据源。我还没有在线找到任何示例。

我想出了这个

DataStream<String> consoleInput = flinkEnv.addSource(new SourceFunction<String>() {

            @Override
            public void run(SourceContext<String> ctx) throws Exception {
                Scanner sc = new Scanner(System.in);
                while (true)
                    ctx.collect(sc.nextLine());
            }

            @Override
            public void cancel() {
            }
        });

我想知道这是否还可以,或者还有更好的方法。

1 个答案:

答案 0 :(得分:0)

Using the console as a data source is certainly doable, but has the issue that your application won't be fault tolerant, since Flink won't be able rewind and replay the input stream in the event of a failure.

But this often done for prototypes and experiments, typically with a SocketTextStreamFunction, as in

env.addSource(new SocketTextStreamFunction("localhost", 9999, "\n", -1))

You can then use netcat to attach the console to port 9999

nc -lk 9999

or, as some versions of netcat require,

nc -l -p 9000