Spark Scala:无法导入 sqlContext.implicits._

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34854317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 07:57:00  来源:igfitidea点击:

Spark Scala : Unable to import sqlContext.implicits._

scalamavenapache-sparkapache-spark-sql

提问by sudhir

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

我尝试了以下代码但无法导入sqlContext.implicits._- 它引发错误(在 Scala IDE 中),无法构建代码:

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

隐式值不是 org.apache.spark.sql.SQLContext 的成员

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

我需要在 中添加任何依赖项pom.xml吗?

Spark version 1.5.2

Spark 版本 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

我的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.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>

采纳答案by Niemand

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

您使用旧版本的 Spark-SQL。将其更改为:

<dependency>
    <groupId>org.apache.spark</groupId>
    <artifactId>spark-sql_2.10</artifactId>
    <version>1.5.2</version>
</dependency>

回答by kasi viswanath

first create

首先创建

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

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

现在我们有了sqlContextwrt sc(这将在我们启动 spark-shell 时自动可用),

import sqlContext.implicits._ 

回答by Marsellus Wallace

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

随着 Spark 2.0.0(2016 年 7 月 26 日)的发布,现在应该使用以下内容:

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

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

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

回答by Arun Goudar

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>