1 pom
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>1.10.1</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.12</artifactId>
<version>1.10.1</version>
</dependency>
</dependencies>
2 word.txt
flink flink flink flink flink flink flink flink flink flink flink flink flink
spark spark spark spark spark spark spark spark spark spark spark spark spark spark spark spark
hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop hadoop
yarn yarn yarn yarn yarn yarn yarn
kafka kafka kafka kafka kafka kafka kafka kafka
flume flume flume flume flume flume
3 WordCount
package com.rosh.flink.wc;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.operators.AggregateOperator;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.util.Collector;
public class WordCount {
public static void main(String[] args) throws Exception {
ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
String inputPath = "data/input/word.txt";
DataSource<String> dataSource = env.readTextFile(inputPath);
AggregateOperator<Tuple2<String, Integer>> result = dataSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
@Override
public void flatMap(String line, Collector<Tuple2<String, Integer>> out) throws Exception {
String[] words = line.split(" ");
for (String word : words) {
out.collect(new Tuple2<>(word, 1));
}
}
}).groupBy(0).sum(1);
result.print();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/56d0267a7ae65aa28483431c52a01c9c.png)
4 StreamWordCount
package com.rosh.flink.wc;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
public class StreamWordCount {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
System.out.println("并行度 :" + env.getParallelism());
String inputPath = "data/input/word.txt";
DataStreamSource<String> dataStreamSource = env.readTextFile(inputPath);
SingleOutputStreamOperator<Tuple2<String, Integer>> result = dataStreamSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
@Override
public void flatMap(String line, Collector<Tuple2<String, Integer>> out) throws Exception {
String[] words = line.split(" ");
for (String word : words) {
out.collect(new Tuple2<>(word, 1));
}
}
}).keyBy(0)
.sum(1);
result.print();
env.execute();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8568f2faacc160a4ba2bf44ef3cacb66.png)
5 NC
#win10 执行命令
nc -L -p 9999
package com.rosh.flink.wc;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
public class NCWordCount {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
System.out.println("并行度 :" + env.getParallelism());
DataStreamSource<String> dataStreamSource = env.socketTextStream("127.0.0.1", 9999);
SingleOutputStreamOperator<Tuple2<String, Integer>> result = dataStreamSource.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
@Override
public void flatMap(String line, Collector<Tuple2<String, Integer>> out) throws Exception {
String[] words = line.split(" ");
for (String word : words) {
out.collect(new Tuple2<>(word, 1));
}
}
}).keyBy(0)
.sum(1);
result.print();
env.execute();
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ce8817648fe8d4838e7db3cd9bab0eca.png)