【问题标题】:How to write a hive sql that insert into table2 if there's a specific row in table1如果table1中有特定行,如何编写插入table2的hive sql
【发布时间】:2021-06-30 14:48:48
【问题描述】:

我面临一个蜂巢问题。

  1. 我会在 sql 之后得到一个 0 或 1

"从 table1 中选择 count(*) 其中 ..."

  1. 如果结果是1,那么我就执行sql

"Insert Into table2 partition(d) (select xxxx from table 1 where ... 按 t) 分组"

否则什么都不做。

我的问题是如何将这两个 sql 一起写成一个 sql。我只能写一个长sql。

我尝试将第一个sql放到sql2中的where条件中,但是抛出了一个错误,说不支持在子查询中对table1进行操作(记不太清楚了,像这样)。

对于有经验的程序员来说,这听起来很简单,但我刚开始学习 hive 2 天。

【问题讨论】:

    标签: hive hiveql


    【解决方案1】:

    如果插入覆盖表分区中的选择不返回行,则没有任何内容被覆盖。

    因此,只需计算您在同一数据集中的计数并对其进行过滤,如果您想在插入之前聚合不同级别的数据,请使用分析功能

    Insert Into table2 partition(d) 
    select col1, col2, sum(col3), etc, etc, partition_col
    from
    (
    select --some columns here,
           --Assign the same count to all rows
           count(case when your_boolean_condition then 1 else null end) over () as cnt
      from table 1
    ) s 
     where cnt=1 --If this is not satisfied, no overwrite will happen 
       AND more_conditions 
    group by ...
    

    另一种可能的方法是对您的计数使用交叉联接:

    insert Into table2 partition(d) 
    select xxxx ... ... ..., partition_column as d 
    from 
    (
    select t.*, c.cnt
    table1 t cross join (select count(*) cnt from table1 where condition) c
    )s
    where cnt=1 <and another_condition>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 2014-12-07
      • 1970-01-01
      相关资源
      最近更新 更多