我正在使用带有Scala插件和spark库的IntelliJ社区版.我还在学习Spark并且正在使用Scala工作表.
我写了下面的代码,删除字符串中的标点符号:
def removePunctuation(text: String): String = {
val punctPattern = "[^a-zA-Z0-9\\s]".r
punctPattern.replaceAllIn(text, "").toLowerCase
}
Run Code Online (Sandbox Code Playgroud)
然后我读了一个文本文件并尝试删除标点符号:
val myfile = sc.textFile("/home/ubuntu/data.txt",4).map(removePunctuation)
Run Code Online (Sandbox Code Playgroud)
这给出了如下错误,任何帮助将不胜感激:
org.apache.spark.SparkException:org.apache.spark.util上的org.apache.spark.util.ClosureCleaner $ .ensureSerializable(/home/ubuntu/src/main/scala/Test.sc:294)中的任务不可序列化.ClosureCleaner $ .org $ apache $ spark $ util $ ClosureCleaner $$ clean(/home/ubuntu/src/main/scala/Test.sc:284)org.apache.spark.util.ClosureCleaner $ .clean(/ home /ubuntu/src/main/scala/Test.sc:104)在org.apache.spark的org.apache.spark.SparkContext.clean(/home/ubuntu/src/main/scala/Test.sc:2090). rdd.RDD $$ anonfun $ map $ 1.apply(/home/ubuntu/src/main/scala/Test.sc:366)org.apache.spark.rdd.RDD $$ anonfun $ map $ 1.apply(/ home /ubuntu/src/main/scala/Test.sc:365)在org.apache.spark.rdd.RDDOperationScope $ .withScope(/home/ubuntu/src/main/scala/Test.sc:147)的#worksheet# .#worksheet#(/ home/ubuntu/src/main/scala/Test.sc:108)引起:java.io.NotSerializableException:A $ A21 $ A $ A21序列化堆栈: - 对象不可序列化(类:A $ A21 $ A $ A21,价值:A $ A21 $ A $ A21 @ …
我编写了一个函数来接受以下类型的值: (1, Array(1.0,2.0,3.0)) 这是一个元组,其中 Int 是第一个值,接下来是一个双精度数组。
我也希望它也接受整数数组。我写的函数如下:
def getCountsAndAverages[T](Parameter: Tuple2[Int, Array[T]])(implicit n:Numeric[T]) = {
(Parameter._1, (Parameter._2.size, Parameter._2.map(n.toDouble).sum/Parameter._2.size))
}
Run Code Online (Sandbox Code Playgroud)
元组的第一个参数是一个数字,然后是它在文件中出现的次数的数组。
它适用于示例案例,但是我正在读取一个文本文件,该文件的数据格式与此功能工作所需的格式相同。我正在使用“地图”操作调用此函数:
parsedfile.map(getCountsAndAverages)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
?could not find implicit value for parameter n: Numeric[T]
?not enough arguments for method getCountsAndAverages: (implicit n: Numeric[T])(Int, (Int, Double)). Unspecified value parameter n.
Run Code Online (Sandbox Code Playgroud)
我将不胜感激任何帮助或建议