选择值小于5000的列配置单元sql值

qyyhg6bp  于 2021-06-24  发布在  Pig
关注(0)|答案(2)|浏览(240)

我正在学习Hive,我遇到了一个问题,我似乎找不到一个可行的答案。我必须从表中提取所有只包含小于5000的整数值的数值列,并创建一个以空格分隔的文本文件。我很熟悉创建文本文件和选择行,但选择符合我不熟悉的特定参数的列,任何帮助或指导将不胜感激!下面我列出了表格的结构。另外,还有一张图片以表格形式显示数据。对于输出,我需要遍历所有列,只返回满足小于5000的整数值参数的列。

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)
)

tbl格式的数据

0tdrvxhp

0tdrvxhp1#

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

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
6fe3ivhb

6fe3ivhb2#

条件列选择是一个可怕的,可怕的,不好的,非常糟糕的主意。
话虽如此,这里是一个演示。

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 |
+---------+

相关问题