Java 使用Spark函数实现的序列化问题
我在理解Java中的Spark函数实现时遇到困难。提供三种使用Java 使用Spark函数实现的序列化问题,java,apache-spark,Java,Apache Spark,我在理解Java中的Spark函数实现时遇到困难。提供三种使用map和reduce中函数的方法: 兰姆达大道 通过内联类实现Function和Function2 通过内部类实现Function和Function2 问题是我无法使2.和3.工作。 例如,此代码: public int countInline(String path) { String master = "local"; SparkConf conf = new SparkConf().setAppName("ch
map
和reduce
中函数的方法:
Function
和Function2
Function
和Function2
2.
和3.
工作。
例如,此代码:
public int countInline(String path) {
String master = "local";
SparkConf conf = new SparkConf().setAppName("charCounterInLine")
.setMaster(master);
JavaSparkContext sc = new JavaSparkContext(conf);
JavaRDD<String> lines = sc.textFile(path);
JavaRDD<Integer> lineLengths = lines
.map(new Function<String, Integer>() {
public Integer call(String s) {
return s.length();
}
});
return lineLengths.reduce(new Function2<Integer, Integer, Integer>() {
public Integer call(Integer a, Integer b) {
return a + b;
}
}); // the line causing the error
}
现在,我可以通过在公共外部类中实现Function
和Function2
来避免这个问题。然而,这与其说是一个深思熟虑的决定,不如说是一个幸运的猜测。此外,由于我无法使文档示例正常工作,我想有些事情我不明白
最后,我的问题是:
- 如何使
和2.
工作3.
- 为什么只有
工作lambda
- 是否有其他方法使用
功能
Task not serializable: java.io.NotSerializableException: Hadoop.Spark.basique.CharCounter
当您将函数定义为内部类时,它们的封闭对象将被拉入函数闭包并序列化。如果该类不可序列化或包含不可序列化字段,则会遇到此错误
您在这里有几个选项:
- 将封闭对象的不可序列化字段标记为瞬态
- 将函数定义为外部类
- 将您的函数定义为
Scala>sc.parallelize(1到1000).count()
。有什么想法吗?我们在Java文件中有一个udf引用,必须同时添加static transient UDF2 blah=new UDF2(){…}
来消除这个错误。
Task not serializable: java.io.NotSerializableException: Hadoop.Spark.basique.CharCounter