1

我正在尝试在 spark (scala) 中创建一个表,然后从两个现有数据帧中插入值,但我得到了这个异常:

Exception in thread "main" org.apache.spark.sql.AnalysisException: Hive support is required to CREATE Hive TABLE (AS SELECT);;
'CreateTable `stat_type_predicate_percentage`, ErrorIfExists 

这是代码:

case class stat_type_predicate_percentage (type1: Option[String], predicate: Option[String], outin: Option[INT], percentage: Option[FLOAT])
object LoadFiles1 {

 def main(args: Array[String]) {
    val sc = new SparkContext("local[*]", "LoadFiles1") 
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    val warehouseLocation = new File("spark-warehouse").getAbsolutePath
    val spark = SparkSession
        .builder()
        .appName("Spark Hive Example")
        .config("spark.sql.warehouse.dir", warehouseLocation)
        .enableHiveSupport()
        .getOrCreate()       

import sqlContext.implicits._    
import org.apache.spark.sql._       
import org.apache.spark.sql.Row;
import org.apache.spark.sql.types.{StructType,StructField,StringType};

//statistics 
val create = spark.sql("CREATE TABLE stat_type_predicate_percentage (type1 String, predicate String, outin INT, percentage FLOAT) USING hive")
val insert1 = spark.sql("INSERT INTO stat_type_predicate_percentage SELECT types.type, res.predicate, 0, 1.0*COUNT(subject)/(SELECT COUNT(subject) FROM MappingBasedProperties AS resinner WHERE res.predicate = resinner.predicate) FROM MappingBasedProperties AS res, MappingBasedTypes AS types WHERE res.subject = types.resource GROUP BY res.predicate,types.type")

val select = spark.sql("SELECT * from stat_type_predicate_percentage" ) 
  }

我该如何解决?

4

2 回答 2

3

--- 你必须在你的 sparksession 中启用 hive 支持

val spark = new SparkSession
    .Builder()
      .appName("JOB2")
      .master("local")
      .enableHiveSupport()
      .getOrCreate()
于 2020-04-13T01:34:08.820 回答
1

这个问题可能是双重的,你可能想做@Tanjin 在评论中建议的事情,之后它可能会起作用(尝试添加.config("spark.sql.catalogImplementation","hive")到你的SparkSession.builder),但是如果你真的想使用现有的配置单元实例和它自己的元数据,你会能够从您的工作之外进行查询。或者您可能已经想使用您可能想要添加到配置 hive-site.xml 的现有表。

此配置文件包含您可能想要的一些属性,例如 hive.metastore.uris,这将使您的上下文添加一个新表,该表将保存在存储中。由于包含表和位置的元存储,它将能够从您的配置单元实例中的表中读取。

于 2019-03-03T15:23:36.190 回答