2016-11-12 39 views
3

既然我可以得到這些單插入語句下面的工作從另一個堆棧溢出問題(感謝),然後SPARK 1.6插入到現有的蜂房表(非分區)

val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc) 
    sqlContext.sql("CREATE TABLE IF NOT EXISTS e360_models.employee(id INT, name STRING, age INT) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'") 

    sqlContext.sql("insert into table e360_models.employee select t.* from (select 1210, 'rahul', 55) t") 
    sqlContext.sql("insert into table e360_models.employee select t.* from (select 1211, 'sriram pv', 35) t") 
    sqlContext.sql("insert into table e360_models.employee select t.* from (select 1212, 'gowri', 59) t") 

    val result = sqlContext.sql("FROM e360_models.employee SELECT id, name, age") 
    result.show() 

什麼樣,如果想要從註冊爲臨時表的SPARK DF中選擇一個插入選擇到已經存在的Hive表中?我似乎無法得到它的工作。事實上有可能嗎?

使用1.6 SPARK。對創建CTAS表並不感興趣,而是按照上述方式插入,但是可以批量插入,例如,

sqlContext.sql("INSERT INTO TABLE default.ged_555 SELECT t.* FROM mytempTable t") 
+0

如果你不感興趣的CTAS請參見下面的方法。 –

+0

它的所有作品除了沒有插入任何東西,但沒有錯誤。 – thebluephantom

+0

對不起!我還沒有看到這個消息..在下面的答案write.mode之前,你可以打印df.show(),以便數據存在於DF或不,我們可以驗證。如果它在那裏,它也應該插入。 –

回答

1

我瞭解您想要在 e360_models.employee插入一些數據,然後要選擇一些列 再次插入到default.ged_555,你也不要」不想從e360_models.employee做CTAS 準備一個數據幀,然後做LIK下文E

// since you are using hive I used hiveContext below... 
val dataframe = hiveContext.sql("select * from e360_models.employee "); 

df.show(10) // to verify whether data is there in dataframe or not 



df.printSchema(); // print schema as well for debug purpose. 
    dataframe.write.mode(SaveMode.OverWrite).insertInto("default.ged_555") 

val sampleDataFrame = hiveContext.sql("select * from default.get_555"); 

// again do print 10 records to verify your result for debug purpose 
sampleDataFrame.show() 
// again print schema of the target table 
sampleDataFrame.printSchema() 
+0

val dataframe = sqlContext.sql(「select * from mytempTable」); org.apache.spark.sql.AnalysisException:未找到表:mytempTable;隊列1個POS 14 – thebluephantom

+0

我得到上述上的Cloudera VM快速入門,databricks似乎好一些,不知道在想什麼 – thebluephantom

+0

mytemptable僅僅是一個例子。在此之前你需要註冊! –