Apache flink 在ApacheFlink中,以下几种进行字数统计的方法有什么区别?
ApacheFlink为DataSet提供了许多操作。理解集群中的数据是如何处理的有点困难。例如,WordCount有不同的实现。有什么区别 如果有一些文档来解释集群中这些实现的数据流,这将非常有用Apache flink 在ApacheFlink中,以下几种进行字数统计的方法有什么区别?,apache-flink,Apache Flink,ApacheFlink为DataSet提供了许多操作。理解集群中的数据是如何处理的有点困难。例如,WordCount有不同的实现。有什么区别 如果有一些文档来解释集群中这些实现的数据流,这将非常有用 // get input data DataSet<String> text = env.fromElements( "To be, or not to be,--that is the question:--", "Whe
// get input data
DataSet<String> text = env.fromElements(
"To be, or not to be,--that is the question:--",
"Whether 'tis nobler in the mind to suffer",
"The slings and arrows of outrageous fortune",
"Or to take arms against a sea of troubles,"
);
// WordCount 1
text.flatMap(new LineSplitter()).groupBy(0).sum(1).print();
// WordCount 2
text.flatMap(new LineSplitter()).groupBy(0).aggregate(Aggregations.SUM, 1).print();
// WordCount 3
text.flatMap(new LineSplitter()).groupBy(0)
.reduce(new ReduceFunction<Tuple2<String, Integer>>() {
@Override
public Tuple2<String, Integer> reduce(Tuple2<String, Integer> t1, Tuple2<String, Integer> t2) throws Exception {
return new Tuple2<String, Integer>(t1.f0, t1.f1+t2.f1);
}
}).print();
// WordCount 4
text.flatMap(new LineSplitter()).groupBy(0)
.reduceGroup(new GroupReduceFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>() {
@Override
public void reduce(Iterable<Tuple2<String, Integer>> iterable, Collector<Tuple2<String, Integer>> collector) throws Exception {
int prefixSum = 0;
String key = null;
for (Tuple2<String, Integer> t : iterable) {
prefixSum += t.f1;
key = t.f0;
}
collector.collect(new Tuple2<String, Integer>(key, prefixSum));
}
}).print();
// WordCount 5
text.flatMap(new LineSplitter())
.reduceGroup(new GroupReduceFunction<Tuple2<String, Integer>, Tuple2<String, Integer>>() {
@Override
public void reduce(Iterable<Tuple2<String, Integer>> iterable, Collector<Tuple2<String, Integer>> collector) throws Exception {
HashMap<String, Integer> map = new HashMap<String, Integer>();
for(Tuple2<String, Integer> t : iterable){
if(map.containsKey(t.f0)){
map.replace(t.f0, map.get(t.f0)+t.f1);
} else {
map.put(t.f0, t.f1);
}
}
for(Map.Entry<String, Integer> pair : map.entrySet()){
collector.collect(new Tuple2<String, Integer>(pair.getKey(), pair.getValue()));
}
}
}).print();
//获取输入数据
数据集文本=env.fromElements(
“生存还是毁灭,这是个问题:——”,
“受苦是否更高尚”,
“无耻命运的投石器和箭”,
“或是拿起武器对抗麻烦之海”
);
//字数1
text.flatMap(newlinesplitter()).groupBy(0.sum(1.print());
//字数2
text.flatMap(new LineSplitter()).groupBy(0).aggregate(Aggregations.SUM,1.print();
//字数3
text.flatMap(新的LineSplitter()).groupBy(0)
.reduce(新的ReduceFunction(){
@凌驾
公共tuple2reduce(tuple2t1,tuple2t2)引发异常{
返回新的Tuple2(t1.f0,t1.f1+t2.f1);
}
}).print();
//字数4
text.flatMap(新的LineSplitter()).groupBy(0)
.reduceGroup(新的GroupReduceFunction(){
@凌驾
公共void reduce(Iterable Iterable,收集器)引发异常{
int prefixSum=0;
字符串键=null;
for(tuple2t:iterable){
前缀sum+=t.f1;
key=t.f0;
}
collector.collect(新的Tuple2(键,前缀));
}
}).print();
//字数5
text.flatMap(新的LineSplitter())
.reduceGroup(新的GroupReduceFunction(){
@凌驾
公共void reduce(Iterable Iterable,收集器)引发异常{
HashMap=newHashMap();
for(tuple2t:iterable){
if(地图集装箱箱(t.f0)){
map.replace(t.f0,map.get(t.f0)+t.f1);
}否则{
map.put(t.f0,t.f1);
}
}
for(Map.Entry对:Map.entrySet()){
collector.collect(新的Tuple2(pair.getKey(),pair.getValue());
}
}
}).print();
除了WordCount 5之外,所有程序的执行都与常规MapReduce WordCount程序非常相似(基于哈希的无序排列和基于排序的分组)
- WordCount 1是WordCount 2的语法糖
- WordCount 2在内部使用与WordCount 4中类似的
执行。唯一的区别是内部的GroupReduceFunction
实现了GroupReduceFunction
接口,以支持部分聚合Combinable
- WordCount 3使用一个
,其执行方式与ReduceFunction
类似。但是,由于接口不同,GroupReduceFunction
始终是可组合的(不需要单独的ReduceFunction
方法)combine
- WordCount 4的执行方式与常规MapReduce程序一样:使用哈希分区和基于排序的分组进行洗牌。由于
没有实现GroupReduceFunction
接口,因此执行此程序时没有进行本地预聚合,因此效率低于前三个程序Combinable
- WordCount 5效率非常低,不应使用,因为无法并行执行
。由于没有GroupReduceFunction
调用,所有数据都被发送到同一个Reducer,并作为一个大的组进行处理。首先,这会很慢,因为它是在单个线程中执行的,并且受到单个机器的网络吞吐量的限制。其次,如果由于分组是使用内存中的groupBy()
完成的,不同键的数量增长过大,则该程序很容易失败李>HashMap