error:')'预期,但'('找到

问题描述 投票:-9回答:2

我正在尝试使用Spark中的scala执行简单的单词计数。但是我得到了这两个错误。我对scala还是比较陌生,无法弄清楚。

error: ')' expected but '(' found.
         println("sparkcontext created")
                    ^
error: ';' expected but 'val' found.
          val lines = sc.textFile(inputFile)
               ^  

我试图运行的代码是,

import org.apache.spark.SparkContext
import org.apache.spark.SparkContext._
import org.apache.spark.SparkConf

object SparkWordCount 
{
    def main(args: Array[String]) 
    {
        //checking to see if the user has entered all the required args
        if (args.length < 2) 
        {
            System.err.println("Usage: <Input_File> <Output_File>")
            System.exit(1)
        }

        val inputFile = args(0)
        val outputFile = args(1)

        // Here we create a new SparkContext instance called sc.
        val sc = new SparkContext(spark://hdn1001.local:7077, "Scala Word Count",System.getenv("SPARK_HOME"), SparkContext.jarOfClass(this.getClass))
        println("sparkcontext created")

        // Input File
        println("Parsing InputFile")
        val lines = sc.textFile(inputFile)
        println("Parsing InputFile completed")

        // split each document into words       
        val words = lines.flatMap(line => line.split(" "))
        println("Split each line of the InputFile into words")

        //Count the occurrence of each word
        val result = words.map(word => (word, 1))
                          .reduceByKey((x,y) => x+y)
        println("WordCount completed")

        // save the output to a file
        result.saveAsTextFile(outputFile)
    }
}

我无法弄清楚。我确实检查了一些有关scala语法的文档,但不了解那里的问题。

感谢balaji,我确实解决了这个问题。

scala syntax apache-spark
2个回答
1
投票

只需将spark主网址包装在双引号中,因为它只是一个字符串。因此它应该看起来像,

val sc = new SparkContext("spark://hdn1001.local:7077", "Scala Word Count",System.getenv("SPARK_HOME"), SparkContext.jarOfClass(this.getClass))

0
投票

我有一个类似的问题,但是您可能缺少某些导入文件。我的问题是它发布了与您相同的错误,我的代码看起来像这样:

val x = s“(从d中选择a,b,c)”

当我将其写为val x = s“(从d中选择a,b,c时)”,它解决了错误。

© www.soinside.com 2019 - 2024. All rights reserved.