HIVE中的分裂功能

时间:2017-09-14 15:00:07

标签: hive hiveql

我想把地址分成两列,如streetno和streetname,比如说ex 从客户

中选择地址1

前身的地址

2719 STONE CREEK DR

并将其存储为街道号码2719和街道名称STONE CREEK DR

使用split(address1 ,'[\ ]')[0]不仅仅拆分了街道。

它只是我们用来查看数据的select语句。

示例数据:

address1
100 HORACE GREELEY RD  
55 School Street
2893 MASHIE CIR
1200 JEWEL DR
201 W RIVER RD

预期输出

+--------------+---------------------+
| streetnumber |     streetname      |
+--------------+---------------------+
|          100 | HORACE GREELEY RD   |
|           55 | School Street       |
|         2893 | MASHIE CIR          |
|         1200 | JEWEL DR            |
|          201 | W RIVER RD          |
+--------------+---------------------+

1 个答案:

答案 0 :(得分:0)

select  regexp_extract(address1,'(.*?)\\s',1)   as streetnumber
       ,regexp_extract(address1,'\\s(.*)' ,1)   as streetname 

from    mytable
;
+----------------+--------------------+
| streetnumber   |     streetname     |
+----------------+--------------------+
| 100            | HORACE GREELEY RD  |
| 55             | School Street      |
| 2893           | MASHIE CIR         |
| 1200           | JEWEL DR           |
| 201            | W RIVER RD         |
+----------------+--------------------+

select  regexp_extract(address1,'.*?(?=\\s)',0) as streetnumber
       ,regexp_extract(address1,'(?<=\\s).*',0) as streetname 

from    mytable
;
+----------------+--------------------+
| streetnumber   |     streetname     |
+----------------+--------------------+
| 100            | HORACE GREELEY RD  |
| 55             | School Street      |
| 2893           | MASHIE CIR         |
| 1200           | JEWEL DR           |
| 201            | W RIVER RD         |
+----------------+--------------------+

select  split(address1,'\\s')[0]      as streetnumber
       ,split(address1,'^.*?\\s')[1]  as streetname 

from    mytable
;
+----------------+--------------------+
| streetnumber   |     streetname     |
+----------------+--------------------+
| 100            | HORACE GREELEY RD  |
| 55             | School Street      |
| 2893           | MASHIE CIR         |
| 1200           | JEWEL DR           |
| 201            | W RIVER RD         |
+----------------+--------------------+