我有一栏 desc_txt
在我的表格中,它的内容和xml的内容很相似,如下图所示----。
desc_txt
-----------
<td><strong>Criticality</strong></td><td>High</td></tr><td><strong>Country</strong></td><td>India</td></tr><tr><td><strong>City</strong></td><td>Indore</td>
要求从该表创建一个新的表视图,该表有额外的列,如 Criticality
, Country
, City
以及列值,如 High
, India
, Indore
在HiveImpala中如何实现?
这可以分两步完成。我假设你只有四列要拉。
with t as (
SELECT rtrim(ltrim(
regexp_replace( replace( trim(
regexp_replace(
regexp_replace("<td><strong>Criticality</strong></td><td>High</td></tr><td><strong>Country</strong></td><td>India</td></tr><tr><td><strong>City</strong></td><td>Indore</td>","</?[^>]*>",",")
,',,',',') ), ' ,', ',' ), '(,){2,}', ','),','),',')
str)
select split_part(str, ',', 1) as first_col,
split_part(str, ',', 2) as second_col,
split_part(str, ',', 3) as third_col,
split_part(str, ',', 4) as fourth_col
from t
这个查询很棘手--首先它用逗号替换所有标签,然后用单个逗号替换多个逗号,再从字符串的开头和结尾去掉逗号,然后用分割函数根据逗号分割整个字符串并创建单独的列。HTH...