基于Socket的数据源,在之前的demo中已经演示了,我们直接看代码:
package org.itzhimei.source;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
import java.util.Arrays;
/**
* Stream Source From File
*/
public class StreamSourceFromSocket {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//从文件中获取数据
DataStream<String> dataStreamSource = env.socketTextStream("localhost", 7777);
SingleOutputStreamOperator<Tuple2<String, Integer>> sum = dataStreamSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
@Override
public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
String[] words = s.split(" ");
Arrays.stream(words).forEach((String sp) -> collector.collect(new Tuple2<String, Integer>(sp, 1)));
}
}).keyBy(item -> item.f0)
.sum(1);
sum.print();
env.execute();
}
}
测试数据
hello Flink
hello Java
how are you
I’m fine thank you and you
I’m Ok
输出结果
16> (Flink,1)
6> (thank,1)
15> (and,1)
9> (fine,1)
3> (I'm,1)
3> (I'm,2)
10> (you,1)
1> (Ok,1)
5> (hello,1)
5> (hello,2)
10> (you,2)
8> (are,1)
10> (you,3)
13> (Java,1)
11> (how,1)