【问题标题】:SELECT COL HIVE SQL VALUE WHERE VALUES <5000选择 COL HIVE SQL 值,其中值 <5000
【发布时间】:2017-06-12 02:07:03
【问题描述】:

我正在学习 HIVE,但遇到了一个我似乎无法找到可行答案的问题。我必须从表中提取所有仅包含

create table lineorder (
  lo_orderkey          int,
  lo_linenumber        int,
  lo_custkey           int,
  lo_partkey           int,
  lo_suppkey           int,
  lo_orderdate         int,
  lo_orderpriority     varchar(15),
  lo_shippriority      varchar(1),
  lo_quantity          int,
  lo_extendedprice     int,
  lo_ordertotalprice   int,
  lo_discount          int,
  lo_revenue           int,
  lo_supplycost        int,
  lo_tax               int,
  lo_commitdate         int,
  lo_shipmode          varchar(10)
)

Data in tbl format

【问题讨论】:

  • 提示:使用WHERE。至于只选择数字列,最简单的方法是在SELECT 语句中列出您想要的列。
  • 编辑您的问题并显示您希望为样本数据生成的结果。
  • 我很想手动选择它们,但不幸的是我必须通过 SQL 语句来完成。
  • 你试过什么?什么不起作用?
  • 我也没试过,我把cmets和建议反馈给我的教授进一步阐述,我正在等待他的回复。

标签: sql hive mapreduce apache-pig


【解决方案1】:

条件列选择是一个糟糕可怕不好非常糟糕的主意。

话虽如此,这里有一个演示。

with    t as 
        (
            select      stack
                        (
                            3

                           ,10 ,100  ,1000 ,'X' ,null
                           ,20 ,null ,2000 ,'Y' ,200000
                           ,30 ,300  ,3000 ,'Z' ,300000
                        ) as (c1,c2,c3,c4,c5)
        )


select  regexp_replace
        (
            printf(concat('%s',repeat(concat(unhex(1),'%s'),field(unhex(1),t.*,unhex(1))-2)),*)
           ,concat('([^\\x01]*)',repeat('\\x01([^\\x01]*)',field(unhex(1),t.*,unhex(1))-2))
           ,c.included_columns
        )       as record

from    t

       cross join      (select  ltrim
                                (
                                    regexp_replace
                                    (
                                        concat_ws(' ',sort_array(collect_set(printf('$%010d',pos+1))))
                                       ,concat
                                        (
                                            '( ?('
                                           ,concat_ws
                                            (
                                                '|'
                                               ,collect_set
                                                (
                                                    case 
                                                        when    cast(pe.val as int) >= 5000
                                                             or cast(pe.val as int) is null

                                                        then    printf('\\$%010d',pos+1)
                                                    end
                                                )
                                            ) 
                                           ,'))|(?<=\\$)0+'
                                        )
                                       ,''
                                    ) 
                                )       as included_columns

                        from    t
                                lateral view posexplode(split(printf(concat('%s',repeat(concat(unhex(1),'%s'),field(unhex(1),*,unhex(1))-2)),*),'\\x01')) pe
                        ) c

+---------+
| record  |
+---------+
| 10 1000 |
| 20 2000 |
| 30 3000 |
+---------+

【讨论】:

    【解决方案2】:

    我认为 hive 不支持函数中的变量替换。因此,您必须编写一个 shell 脚本来执行第一个返回所需列的查询。然后您可以将其分配给 shell 脚本中的一个变量,然后创建一个新查询以在本地目录中创建文件并通过 hive -e 运行它来自 bash。

    create table t1(x int , y int) ; // table used for below query
    

    示例 bash 脚本:

    cols =hive -e 'select concat_ws(',', case when min(x) > 5000 then 'x' end , case when min(y) > 5000 then 'y' end) from t1'
    query ="INSERT OVERWRITE LOCAL DIRECTORY <directory name> ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' select  $cols from t1 "
    hive -e query 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 2021-12-16
      相关资源
      最近更新 更多