2017-06-12 12 views
1

私はHIVEについて学んでいます。私は質問に出くわしました。実行可能な答えが見つからないようです。私は、テーブルから整数値< 5000のみを含む数値列をすべて抽出し、スペースで区切られたテキストファイルを作成する必要があります。私はテキストファイルの作成と行の選択に精通していますが、私が慣れていない特定のパラメータを満たす列を選択すると、どんな助けや指導も頂けます!以下では、テーブルの構造をリストアップしました。また、表形式のデータを添付した画像もあります。 OUTPUTのために私はすべての列を通過し、整数のパラメータが、私はハイブがで変数置換をサポートしていないと思うLESS THAN 5000SELECT COL HIVE SQL VALUE WHERE VALUE <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) 
) 

Data in tbl format

+0

ヒント: 'WHERE'使用してください。数値列のみを選択する場合は、SELECT文に必要な列を列挙するのが最も簡単です。 –

+0

質問を編集し、サンプルデータ用に作成したい結果を表示します。 –

+0

私はそれらを手動で選択するのが大好きですが、残念ながら私はSQL文でそれを行う必要があります。 – Dano

答えて

0

値会う列のみを返す必要があります関数。したがって、必要な列を返す最初のクエリを実行するシェルスクリプトを作成する必要があります。シェルスクリプトの変数にそれを割り当てて、ローカルディレクトリにファイルを作成するための新しいクエリを作成し、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 
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 | 
+---------+ 
関連する問題