代码
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.DataSet;
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 executionEnvironment = ExecutionEnvironment.getExecutionEnvironment();
executionEnvironment.setParallelism(1);
DataSet<String> dataSource = executionEnvironment.readTextFile("***\\src\\main\\resources\\hello.txt");
AggregateOperator<Tuple2<String, Integer>> resultDataSet = dataSource.flatMap(new MyFlatMapFunction()).groupBy(0).sum(1);
resultDataSet.print();
}
public static class MyFlatMapFunction implements FlatMapFunction<String, Tuple2<String, Integer>> {
@Override
public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
String[] values = s.split(" ");
for (String value: values) {
collector.collect(new Tuple2(value,1));
}
}
}
}
数据格式: 输出: 若要排序,可将代码改为:
SortPartitionOperator<Tuple2<String, Integer>> resultDataSet =
dataSource.flatMap(new MyFlatMapFunction()).groupBy(0).sum(1).sortPartition(1, Order.DESCENDING);
结果:
|