Java中,如何收集星火过滤掉值 [英] Java, How to collect filtered out values in Spark
本文介绍了Java中,如何收集星火过滤掉值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的输入是一个字符串的集合,说:
My Input is a collection of strings, Say :
Chicago
Washington
Seattle
San Francisco
Chicago
Newyork
Dallas
我有低于code这过滤掉某些值:
I have the below code which filters out certain values:
List<String> filteredValues = new ArrayList<String>();
JavaRDD<String> filteredOutput = inputText.filter(line -> {
if(line.toLowerCase().contains("chicago")) {
filteredValues.add(line);
return false;
}
return true;
});
System.out.println("Filtered values are" + filteredValues.toString());
}
}
我的预期filteredValues列表包含 [芝加哥,芝加哥。
但它是空的。有人可以帮助我。
But its empty. Can someone help me out.
推荐答案
Spark是更多的功能,所以更新变量将无法正常工作。*您应该使用从输出滤波
和呼叫收集
。
Spark is more functional, so updating a variable won't work.* You should use the output from the filter
and call collect
.
另外,你需要,如果你想保持一个项目以返回true。看来你已经有了这个倒退。
Also, you need to return true if you want to keep an item. It appears you've got this backwards.
*在变量被复制到每个工人关闭。因此,更新仅劳动者本地副本。
*closed over variables are COPIED to each worker. So the updates are only the worker local copy.
这篇关于Java中,如何收集星火过滤掉值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文