标签:use insert blog port ast ring apache table sql语句
case class Person(name:String,col1:Int,col2:String) def main(args:Array[String]){ val sc = new org.apache.spark.SparkContext val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc) import hiveContext.implicits._ hiveContext.sql("use DataBaseName") val data = sc.textFile("path").map(x=>x.split("\\s+")).map(x=>Person(x(0),x(1).toInt,x(2))) data.toDF()insertInto("tableName") }
将DataFrame数据写入hive指定数据表的分区中
将数据写入分区表的思路是:首先将DataFrame数据写入临时表,之后是由hiveContext.sql语句将数据写入hive分区表中。具体操作如下:
case class Person(name:String,col1:Int,col2:String) def main(args:Array[String]):Unit={ val sc = new org.apache.spark.SparkContext val hiveContext = new org.apache.spark.sql.hive.HiveContext(sc) import hiveContext.implicits._ hiveContext.sql("use DataBaseName") val data = sc.textFile("path").map(x=>x.split("\\s+")).map(x=>Person(x(0),x(1).toInt,x(2))) data.toDF().registerTempTable("table1") hiveContext.sql("insert into table2 partition(date=‘2015-04-02‘) select name,col1,col2 from table1") }
声明本文转自:http://www.aboutyun.com/thread-12392-1-1.html
Hive:Spark中如何实现将rdd结果插入到hive1.3.1表中
标签:use insert blog port ast ring apache table sql语句
原文地址:http://www.cnblogs.com/yy3b2007com/p/6082048.html