Spark落地到hive表时saveAsTable与insertInto的区别

SaveAsTable

//Api的解释

Saves the content of the `DataFrame` as the specified table.
*
* In the case the table already exists, behavior of this function depends on the
* save mode, specified by the `mode` function (default to throwing an exception).
* When `mode` is `Overwrite`, the schema of the `DataFrame` does not need to be
* the same as that of the existing table.
*
* When `mode` is `Append`, if there is an existing table, we will use the format and options of
* the existing table. The column order in the schema of the `DataFrame` doesn't need to be same
* as that of the existing table. Unlike `insertInto`, `saveAsTable` will use the column names to
* find the correct column positions.

意思就是说,当hive中已经存在目标表,无论SaveMode是append还是overwrite,不需要schema一样,只要列名存在就行,会根据列名进行匹配覆盖数据

举个例子:
-----当hive中列名是i,j时
  scala> Seq((1, 2)).toDF("i", "j").write.mode("overwrite").saveAsTable("t1")
*    scala> Seq((3, 4)).toDF("j", "i").write.mode("append").saveAsTable("t1")
*    scala> sql("select * from t1").show
*    +---+---+
*    |  i|  j|
*    +---+---+
*    |  1|  2|
*    |  4|  3|
*    +---+---+

InsertInto

//Api解释
Inserts the content of the `DataFrame` to the specified table. It requires that
* the schema of the `DataFrame` is the same as the schema of the table.
*
* @note Unlike `saveAsTable`, `insertInto` ignores the column names and just uses position-based
* resolution. For example:

意思就是说,当hive中存在目标表时,无论SaveMode是append还是overwrite,需要当前DF的schema与目标表的schema必须一致

举个例子:
-----当hive中列名是i,j时,schema为(int,int)

* {{{
*    scala> Seq((1, 2)).toDF("i", "j").write.mode("overwrite").saveAsTable("t1")
*    scala> Seq((3, 4)).toDF("j", "i").write.insertInto("t1")
*    scala> Seq((5, 6)).toDF("a", "b").write.insertInto("t1")
*    scala> sql("select * from t1").show
*    +---+---+
*    |  i|  j|
*    +---+---+
*    |  5|  6|
*    |  3|  4|
*    |  1|  2|
*    +---+---+
* }}}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值