【问题标题】:SPARK 1.6 Insert into existing Hive table (non-partitioned)SPARK 1.6 插入现有 Hive 表(非分区)
【发布时间】:2017-12-08 14:14:55
【问题描述】:

鉴于我可以让下面的这些单例插入语句像另一个堆栈溢出问题一样工作(谢谢),那么

  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。对创建一个 la CTAS 表不感兴趣,而是按照上面的方法插入,但是批量插入,例如

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

【问题讨论】:

  • 如果您对 CTAS 不感兴趣,请参阅以下方法。
  • 一切正常,只是没有插入任何内容,但没有错误。
  • 对不起!我还没有看到这条消息..在下面的答案中的 write.mode 之前,您可以打印 df.show() 以便我们可以验证数据是否存在于 df 中。如果它在那里,它也应该插入。
  • 既然你说没有错误也没有插入,我更新了下面的代码,请检查。另外请添加@myname 通知我,否则我不会收到通知。谢谢!

标签: apache-spark hive


【解决方案1】:

据我了解,您想将一些数据插入到 e360_models.employee 然后您想选择一些列并 再次插入 default.ged_555 并且你不想做 CTAS 从 e360_models.employee 准备一个数据框,然后像下面那样做

// 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()

【讨论】:

  • val dataframe = sqlContext.sql("select * from mytempTable"); org.apache.spark.sql.AnalysisException:找不到表:mytempTable;第 1 行第 14 行
  • 我在 cloudera VM quickstart 上得到了上述内容,databricks 似乎更好,不知道该怎么想
  • mytemptable 只是一个例子。在此之前你需要注册!
  • 如果我正确理解了您的要求,您就不需要 CTAS 并且您正在寻找另一种方法。问题有点不清楚。你能详细说明一下吗?什么是 e360_models.employee 和什么是 default.ged_555
  • 是的,我已经注册了,但我认为我缺少 SQL 和 Hive 上下文,可能是一个小错误。让我检查一下
猜你喜欢
  • 2019-02-16
  • 2019-10-27
  • 2016-12-13
  • 2018-01-03
  • 2015-09-13
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多