9

I tried the below code and cannot import sqlContext.implicits._ - it throws an error (in the Scala IDE), unable to build the code:

value implicits is not a member of org.apache.spark.sql.SQLContext

Do I need to add any dependencies in pom.xml?

Spark version 1.5.2

package com.Spark.ConnectToHadoop

import org.apache.spark.SparkConf
import org.apache.spark.SparkConf
import org.apache.spark._
import org.apache.spark.sql._
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.sql.SQLContext
import org.apache.spark.rdd.RDD
//import groovy.sql.Sql.CreateStatementCommand

//import org.apache.spark.SparkConf


object CountWords  {

  def main(args:Array[String]){

    val objConf = new SparkConf().setAppName("Spark Connection").setMaster("spark://IP:7077")
    var sc = new SparkContext(objConf)
val objHiveContext = new HiveContext(sc)
objHiveContext.sql("USE test")
var rdd= objHiveContext.sql("select * from Table1")
val options=Map("path" -> "hdfs://URL/apps/hive/warehouse/test.db/TableName")
//val sqlContext = new org.apache.spark.sql.SQLContext(sc)
   val sqlContext = new SQLContext(sc)
    import sqlContext.implicits._      //Error
val dataframe = rdd.toDF()
dataframe.write.format("orc").options(options).mode(SaveMode.Overwrite).saveAsTable("TableName")      
  }
}

My pom.xml file is as follows

<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.Sudhir.Maven1</groupId>
  <artifactId>SparkDemo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SparkDemo</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_2.10</artifactId>
      <version>1.5.2</version>
    </dependency> 
    <dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.10</artifactId>
    <version>1.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-mllib_2.10</artifactId>
    <version>1.5.2</version>
</dependency>

<dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-streaming_2.10</artifactId>
      <version>0.9.1</version>
    </dependency>

     <dependency>
        <groupId>org.apache.spark</groupId>
        <artifactId>spark-hive_2.10</artifactId>
        <version>1.2.1</version>
    </dependency>
  <dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.2.1</version>
</dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>     

  </dependencies>
</project>
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
sudhir
  • 1,387
  • 3
  • 25
  • 43

5 Answers5

7

first create

val sqlContext = new org.apache.spark.sql.SQLContext(sc)

now we have sqlContext w.r.t sc (this will be available automatically when we launch spark-shell) now,

import sqlContext.implicits._ 
Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
  • 2
    Welcome to SO! Your reply looks like a comment rather than a answer. Once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment](http://stackoverflow.com/help/privileges/comment) on any post. Also check this [what can I do instead](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). If you intended to answer, read this [how-to-answer](http://stackoverflow.com/help/how-to-answer) for providing quality answer. – thewaywewere May 03 '17 at 13:55
6

With the release of Spark 2.0.0 (July 26, 2016) one should now use the following:

import spark.implicits._  // spark = SparkSession.builder().getOrCreate()

https://databricks.com/blog/2016/08/15/how-to-use-sparksession-in-apache-spark-2-0.html

Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
3

You use an old version of Spark-SQL. Change it to:

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.10</artifactId>
    <version>1.5.2</version>
</dependency>
TheMP
  • 8,257
  • 9
  • 44
  • 73
  • 1
    Or, better, add a property `spark.version`, so when switching to a new Spark version, you only need to change it in one place. – Alexey Romanov Jan 18 '16 at 14:21
  • Thanks, I resolved that, but stuck with below exception : I even added dependency with spark-catalyst_2.10 : Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/spark/sql/catalyst/analysis/OverrideFunctionRegistry – sudhir Jan 19 '16 at 05:34
  • Are you sure that all of your spark deps are version 1.5.2? Perhaps you really should do as @Alexey Romanov suggests and introduce a spark.version variable in your pom. – TheMP Jan 19 '16 at 07:52
  • Ya don't know exactly , how to check that? Earlier we had spark version 1.4.1 and it was fine, recently it was upgraded to 1.5.2 added in properties too 1.5.2 – sudhir Jan 19 '16 at 08:07
  • I se that you already created a new question - http://stackoverflow.com/questions/34871015/exception-in-thread-main-java-lang-noclassdeffounderror-org-apache-spark-sql . Please paste your pom there and we can try to sort this out. – TheMP Jan 19 '16 at 09:11
1

For someone using sbt to build, update the library versions to

libraryDependencies ++= Seq(
  "org.apache.spark" % "spark-core_2.12" % "2.4.6" % "provided",
  "org.apache.spark" % "spark-sql_2.12" % "2.4.6" % "provided"
)

And then import SqlImplicits as below.

val spark = SparkSession.builder()
      .appName("appName")
      .getOrCreate()

    import spark.sqlContext.implicits._;
Gayan Kavirathne
  • 2,909
  • 2
  • 18
  • 26
0

You can also use

<properties>
   <spark.version>2.2.0</spark.version>
</properties>


<dependency>
    <groupId>org.apache.spark</groupId>
     <artifactId>spark-core_2.11</artifactId>
      <version>${spark.version}</version>
 </dependency>
  <dependency>   
      <groupId>org.apache.spark</groupId>
       <artifactId>spark-sql_2.11</artifactId>
       <version>${spark.version}</version>
  </dependency>
Arun Goudar
  • 361
  • 3
  • 5