首页 > 解决方案 > 基于 Spark Scala 中的 3 个场景在 Hive 表中插入/更新记录

问题描述

我有一个源表,我想根据以下场景将数据更新/插入到输出表中。

源表:

Name|age|dept|sal |school|college|deg|blood_group
aaa |10 |ece |1000|svv   |sas    |be |0+
bbb |20 |it  |2000|svv   |sas    |be |A+



scenario 1: If value name,age,dept doesn't exists on output table,create new record
scenario 2: If value name,age,dept exists on output table , if no changes in school,college then do nothing
scenario 3: If value name,age,dept exists on output table , if changes in school,college then do nothing then create new record



I want to insert data's into output table based on above scenario using either spark sql or spark scala dataframe.

Please suggest me.

标签: apache-sparkapache-spark-sqlspark-streaming

解决方案


我不确定这是否可行

  1. 尝试在编写之前先在代码中调用 Hive 表,然后从中创建一个 Table /Dataframe 并将其命名为prior_df

    from pyspark.sql import HiveContext
         hive_context = HiveContext(sc)
         bank = hive_context.table("default.bank")
         bank.show()
    
bank.registerTempTable("bank_temp")
hive_context.sql("select * from bank_temp").show()
  1. 现在,加入prior_df 表,因为您已经有了使用withColumn 的条件,并且当条件同样为“无操作/过滤”事务创建一个新列时。prior_df 将帮助您获得先前的值

  2. 将新的 DF 写入配置单元表位置


推荐阅读