Hive - 将单元格拆分为多列

Hive - split cell into multiple column

我参考了之前的帖子将一栏分成两栏。看起来我指的示例是 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;

使用substrinstr

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

这里有一个例子:

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/