简体   繁体   English

使用Spark Scala更新Table Hive

[英]Update Table Hive Using Spark Scala

I need to update a Table Hive like 我需要update一个Table Hive

update A from B
    set 
        Col5 = A.Col2, 
        Col2 =   B.Col2, 
        DT_Change = B.DT, 
        Col3 = B.Col3, 
        Col4 = B.Col4
where A.Col1 = B.Col1  and  A.Col2 <> B.Col2

Using Scala Spark RDD 使用Scala Spark RDD

How can I do this ? 我怎样才能做到这一点 ?

I want to split this question in to two questions to explain it simple. 我想将这个问题分为两个问题,以使其简单解释。 First question : How to write Spark RDD data to Hive table ? 第一个问题:如何将Spark RDD数据写入Hive表? The simplest way is to convert the RDD in to Spark SQL (dataframe) using method rdd.toDF() . 最简单的方法是使用rdd.toDF()方法将RDD转换为Spark SQL(数据帧rdd.toDF() Then register the dataframe as temptable using df.registerTempTable("temp_table") . 然后使用df.registerTempTable("temp_table")将数据帧注册为临时df.registerTempTable("temp_table") Now you can query from the temptable and insert in to hive table using sqlContext.sql("insert into table my_table select * from temp_table") . 现在,您可以从临时表中查询并使用sqlContext.sql("insert into table my_table select * from temp_table") Second question: How to update Hive table from Spark ? 第二个问题:如何从Spark更新Hive表? As of now, Hive is not a best fit for record level updates. 到目前为止,Hive并不是最适合记录级别的更新。 Updates can only be performed on tables that support ACID. 更新只能在支持ACID的表上执行。 One primary limitation is only ORC format supports updating Hive tables. 一个主要限制是仅ORC格式支持更新Hive表。 You can find some information on it from https://cwiki.apache.org/confluence/display/Hive/Hive+Transactions You can refer How to Updata an ORC Hive table form Spark using Scala for this. 您可以从https://cwiki.apache.org/confluence/display/Hive/Hive+Transactions找到一些有关它的信息。 为此,您可以参考如何使用Scala如何Spark刷新ORC Hive表

Few methods might have deprecated with spark 2.x and you can check spark 2.0 documentation for the latest methods. 很少有方法会在spark 2.x中弃用,您可以在spark 2.0文档中查看最新方法。 While there could be better approaches, this is the simplest approach that I can think of which works. 尽管可能会有更好的方法,但这是我能想到的最简单的方法。

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

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