在Spark CLI中初始化HiveContext

时间:2016-07-04 06:42:21

标签: scala apache-spark hive command-line-interface apache-spark-sql

默认情况下,在命令行界面中初始化Spark时,SparkContext初始化为sc,SQLContext初始化为sqlContext。

但我需要HiveContext,因为我正在使用SparkContext不支持的函数collect_list,但HiveContext支持它。由于HiveContext是SparkContext的超类,它应该有效,但它不是。

如何使用Spark CLI在Scala中初始化HiveContext

3 个答案:

答案 0 :(得分:2)

在spark-shell中,sqlContext默认是HiveContext的一个实例。您可以在我之前的回答here中了解相关内容。

然而,火花1.5.2中没有collect_list。它是在火花1.6中引入的,所以你可以找到它是正常的。

参考:https://github.com/apache/spark/blob/v1.6.2/sql/core/src/main/scala/org/apache/spark/sql/functions.scala#L213

此外,您不需要在shell中导入org.apache.spark.sql.functions._。它是默认导入的。

答案 1 :(得分:1)

sqlContext是一个HiveContext

%

<强> [编辑]

使用前导入功能。

scala> sqlContext
res11: org.apache.spark.sql.SQLContext = org.apache.spark.sql.hive.HiveContext@4756c8f3

答案 2 :(得分:1)

您可以按照以下步骤执行此操作:

import org.apache.spark.sql.hive.HiveContext
val sqlContext = new HiveContext(sc)
val depts = sqlContext.sql("select * from departments")`