繁体   English   中英

使用spark从案例类列表创建配置单元表

[英]create a hive table from list of case class using spark

我试图从案例类的列表中创建一个配置单元表。 但是它不允许指定数据库名称。 抛出以下错误。

Spark版本:1.6.2

错误:诊断:用户类引发异常:org.apache.spark.sql.AnalysisException:未找到表:mytempTable; 第1行pos 58

请让我知道将map方法的输出保存到与case类具有相同结构的配置单元表中的方法。

注意:在给定输入的map方法(实际上是getElem()方法)中填充recordArray列表

    object testing extends Serializable {
          var recordArray=List[Record]();
           def main(args:Array[String])
          {
          val inputpath = args(0).toString();
          val outputpath=args(1).toString();


          val conf = new SparkConf().setAppName("jsonParsing")
          val sc = new SparkContext(conf)
          val sqlContext= new SQLContext(sc)
          val hsc = new HiveContext(sc)

          val input = sc.textFile(inputpath)
          //val input=sc.textFile("file:///Users/Documents/Work/data/mydata.txt")
         // input.collect().foreach(println)
         val = input.map(data=>getElem(parse(data,false))) 
   val recordRDD = sc.parallelize(recordArray)
//
     val recordDF=sqlContext.createDataFrame(recordRDD)
    recordDF.registerTempTable("mytempTable") 
     hsc.sql("create table dev_db.ingestion as select * from mytempTable")
        }

    case class Record(summary_key: String, key: String,array_name_position:Int,Parent_Level_1:String,Parent_level_2:String,Parent_Level_3:String,Parent_level_4:String,Parent_level_5:String,
            param_name_position:Integer,Array_name:String,paramname:String,paramvalue:String)
    }

您需要拥有/创建一个HiveContext

import org.apache.spark.sql.hive.HiveContext;
HiveContext sqlContext = new org.apache.spark.sql.hive.HiveContext(sc.sc());

然后直接保存数据框或选择要存储为配置单元表的列

recordDF是数据帧

recordDF.write().mode("overwrite").saveAsTable("schemaName.tableName");

要么

recordDF.select(recordDF.col("col1"),recordDF.col("col2"), recordDF.col("col3")) .write().mode("overwrite").saveAsTable("schemaName.tableName");

要么

recordDF.write().mode(SaveMode.Overwrite).saveAsTable("dbName.tableName");

保存模式为追加/忽略/覆盖/ ErrorIfExists

我在此处添加了Spark文档中HiveContext的定义,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM