[英]Spark Java Accumulator not incrementing
刚刚开始使用 Spark-Java 的小步骤。 下面是一个单词计数程序,它包含一个停用词列表,可以跳过列表中的单词。 我有 2 个累加器来计算跳过的单词和未跳过的单词。
但是,程序结束时的Sysout
始终将两个累加器值都设为 0 。
请指出我哪里出错了。
public static void main(String[] args) throws FileNotFoundException {
SparkConf conf = new SparkConf();
conf.setAppName("Third App - Word Count WITH BroadCast and Accumulator");
JavaSparkContext jsc = new JavaSparkContext(conf);
JavaRDD<String> fileRDD = jsc.textFile("hello.txt");
JavaRDD<String> words = fileRDD.flatMap(new FlatMapFunction<String, String>() {
public Iterable<String> call(String aLine) throws Exception {
return Arrays.asList(aLine.split(" "));
}
});
String[] stopWordArray = getStopWordArray();
final Accumulator<Integer> skipAccumulator = jsc.accumulator(0);
final Accumulator<Integer> unSkipAccumulator = jsc.accumulator(0);
final Broadcast<String[]> stopWordBroadCast = jsc.broadcast(stopWordArray);
JavaRDD<String> filteredWords = words.filter(new Function<String, Boolean>() {
public Boolean call(String inString) throws Exception {
boolean filterCondition = !Arrays.asList(stopWordBroadCast.getValue()).contains(inString);
if(!filterCondition){
System.out.println("Filtered a stop word ");
skipAccumulator.add(1);
}else{
unSkipAccumulator.add(1);
}
return filterCondition;
}
});
System.out.println("$$$$$$$$$$$$$$$Filtered Count "+skipAccumulator.value());
System.out.println("$$$$$$$$$$$$$$$ UN Filtered Count "+unSkipAccumulator.value());
/* rest of code - works fine */
jsc.stop();
jsc.close();
}
我正在制作一个可运行的 jar 并使用以下命令在 Hortonworks Sandbox 2.4 上提交作业
spark-submit jarname
- - - - - - 编辑 - - - - - - - -
注释部分中的其余代码
JavaPairRDD<String, Integer> wordOccurrence = filteredWords.mapToPair(new PairFunction<String, String, Integer>() {
public Tuple2<String, Integer> call(String inWord) throws Exception {
return new Tuple2<String, Integer>(inWord, 1);
}
});
JavaPairRDD<String, Integer> summed = wordOccurrence.reduceByKey(new Function2<Integer, Integer, Integer>() {
public Integer call(Integer a, Integer b) throws Exception {
return a+b;
}
});
summed.saveAsTextFile("hello-out");
您错过了发布重要部分/* rest of code - works fine */
。 我可以保证您正在调用其余代码中的某些操作。 这会触发 DAG 使用累加器执行代码。 尝试在 println 之前添加一个filteredWords.collect
,您应该会看到输出。 请记住,Spark 在转换上是惰性的,并且只在操作上执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.