我正在尝试将 the examples 中所示的 filter 添加到我的程序中:

val logFile = "/tmp/master.txt"
val sc = new JavaSparkContext("local[4]", "Twitter Analyzer", "/home/welcome/Downloads/spark-1.1.0/",Array("target/scala-2.10/Simple-assembly-0.1.0.jar"))
val twitterFeed = sc.textFile(logFile).cache()

while (iterator.hasNext) {
  val value = iterator.next()
  val numAs = twitterFeed.filter(line => line.contains(value))
  numAs.saveAsTextFile("/tmp/output/positive/" + value)
}

我得到如下编译错误:
[info] Compiling 1 Scala source to /home/siva/file1/target/scala-2.10/classes...
[error] /home/siva/file1/src/main/scala/com/chimpler/example/twitter/Tweet.scala:27: missing parameter type
[error]     val numAs = twitterFeed.filter(line => line.contains(value))
[error]                                    ^
[error] one error found
[error] (compile:compile) Compilation failed
[error] Total time: 5 s, completed 19 Sep, 2014 1:31:26 PM

有任何想法吗?

最佳答案

As @groverboy advised in the comment 你真的应该使用 org.apache.spark.SparkContext 来代替。 Spark Programming Guide 的 Initializing Spark 对此也很清楚。

import org.apache.spark._

val conf = new SparkConf()
  .setMaster("local[4]")
  .setAppName("Twitter Analyzer")
  .setSparkHome("/home/welcome/Downloads/spark-1.1.0/")
  .setJars(Seq("target/scala-2.10/Simple-assembly-0.1.0.jar"))
val sc = new SparkContext(conf)

这样做的原因是 Scala 中的类型推断需要类型上下文来推断 line 参数的类型。
val numAs = twitterFeed.filter(line => line.contains(value))

它显然是 String 类型,但是使用 SparkContext 的 Java 版本 - JavaSparkContext - 您只会丢失类型信息。

如果您使用 SparkContext,则上述行可以进一步简化为:
val numAs = twitterFeed.filter(_.contains(value))

甚至:
twitterFeed.filter(_ contains value)

所有的好东西都在 SparkContext 之外。

关于scala - 为什么 Scala 编译器会因缺少 JavaSparkContext 过滤器的参数类型而失败?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25929231/

10-12 19:40
查看更多