0
votes

I have created a Maven project to run a wordcount spark-scala program. Here when I create my SparkConf it gives me an error "org.apache.spark.SparkConf does not have constructor". Similar for SparkContext (org.apache.spark.SparkContext has no constructor)

I have imported both SparkContext and SparkConf and also written in the proper constructor format.This could be a Maven issue but no such error pops up related to that.

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


object WordCount {

def main(args: Array[String]) {

val cf = new SparkConf().setAppName("WordCount").setMaster("local")

val sc = new SparkContext(cf)

val rawData = sc.textFile("C:/Users/siddharth.shankar/Documents/input.txt")

val words = rawData.flatMap(line => line.split(" "))

val wordCount = words.map(word => (word, 1)).reduceByKey(_ + _)

wordCount.foreach(println)


  } 

}

Here is my pom.xml

   <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <modelVersion>4.0.0</modelVersion>
 <groupId>com.devinline.spark</groupId>
 <artifactId>SparkSample2</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>SparkSample Maven Webapp</name>
 <url>http://maven.apache.org</url>
 <dependencies>
   <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-core_2.11</artifactId>
    <version>2.4.0</version>
    </dependency>
   <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
   </dependency>
  <dependency>
   <groupId>org.apache.hadoop</groupId>
   <artifactId>hadoop-winutils</artifactId>
   <version>2.7.1</version>
  </dependency>  
</dependencies>
  <build>
    <finalName>SparkSample2</finalName>
  </build>
 </project>

I don't know what the issue is here as if I apply the same program as a regular spark-scala(no maven) application the program runs without errors.

3
Please post your pom.xml file here. - Vladislav Varslavans

3 Answers

0
votes

Check your scala version for both the cases is same or not? It seams like version issue. I execute this code with maven it works fine with scala 2.11. even with spark submit also it is working.

0
votes

Try adding:

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
...
        <dependency>
            <groupId>org.scala-lang</groupId>
            <artifactId>scala-library</artifactId>
            <version>${scala.version}</version>
            <scope>provided</scope>
        </dependency>
...
<build>
        <sourceDirectory>src/main/scala</sourceDirectory>

...
<plugins>
            <plugin>
                <!-- see http://davidb.github.com/scala-maven-plugin -->
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>3.3.1</version>
                    <configuration>
                        <compilerPlugins>
                            <compilerPlugin>
                                <groupId>com.artima.supersafe</groupId>
                                <artifactId>supersafe_${scala.version}</artifactId>
                                <version>1.1.3</version>
                            </compilerPlugin>
                        </compilerPlugins>
                    </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <args>
                                <arg>-feature</arg>
                                <arg>-deprecation</arg>
                                <arg>-dependencyfile</arg>
                                <arg>${project.build.directory}/.scala_dependencies</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
0
votes
    </dependency>   
            <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.11.8</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-reflect</artifactId>
        <version>2.11.8</version>
    </dependency>
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-sql_2.11</artifactId>
        <version>2.1.1</version>
    </dependency>           
    <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-core_2.11</artifactId>
        <version>2.1.1</version>
    </dependency>

Use these dependencies will work fine.