【问题标题】:Hive - split cell into multiple columnHive - 将单元格拆分为多列
【发布时间】:2017-11-24 10:20:37
【问题描述】:

我曾参考之前的帖子将一列分成两列。看起来我所指的示例是 sql,与 Hive 相比可能有所不同。如何将下面的 orig_data 转换为结果数据?

            orig_data

name        location    code
Andrew M    NY          145-ABG
Paul C      NY          1787-ATG
Kate M      NY          3874-WV

            results

name        location    ID      per
Andrew M    NY          145     ABG
Paul C      NY          1787    ATG
Kate M      NY          3874    WV

select 
  name, location,
  left(code, charindex('-',code) as id, --not working
  right(code, charindex('-',code) as per, --not working
from 
  orig_table;

【问题讨论】:

    标签: sql hive hiveql


    【解决方案1】:

    使用substrinstr

    select 
    name, location,
    substr(code, 1, instr(code,'-')-1) as id, 
    substr(code, instr(code,'-')+1) as per
    from orig_table;
    

    【讨论】:

    • 删除 len(code) 后,它从第 4 行开始工作...谢谢!
    【解决方案2】:

    这是一个例子:

    INSERT INTO TABLE bdp.optrans_tbl
    SELECT
    CONCTNS.splitted_cnctns[0] AS con1,
    CONCTNS.splitted_cnctns[1] AS con2,
    CONCTNS.splitted_cnctns[2] AS con3
    FROM
    (SELECT
    split(connections,',') AS splitted_cnctns FROM bdp.transact_tbl)CONCTNS;
    

    了解更多,请查看http://bigdataprogrammers.com/split-one-column-into-multiple-columns-in-hive/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 2021-09-24
      • 1970-01-01
      • 2019-01-05
      • 2017-04-16
      相关资源
      最近更新 更多