splitting a text string to matching columns in presto

末鹿安然 提交于 2020-01-06 07:53:08

问题


I have a report from a presto query that gives me information in a string

The raw data looks something like this:

c_pre=CI2UhdX95uACFcKIdwodZ8QETQ;gtm=2od241;auiddc=*;u1=cz;u10=Not
Available;u11=Not Available;u12=1;u13=Not Available;u14=SGD;u15=Not
Available;u3=pdp;u4=undefined;u6=Not Available;~oref=https://www.bbc.com/

I found a excel workaround that splits this into seperate columns. screenshot attached for reference

This process still takes quite a long time to do, and I was hoping to use the presto dashboard to automate this.

All of the items with the same u prefix (u3, u13, etc in the example above ) has to go into the same column, otherwise I would just do a string slice. Unfortunately, different strings have a different result depending on the data stored, resulting in different u prefixes in the same column.Screenshot to show why this is confusing

Excel query:

=IFERROR(
RIGHT(
INDEX(RD!2:2,1,
MATCH('Data LU'!A$1&"="&"*",RD!2:2,0)),
(LEN(
INDEX(RD!2:2,1,
MATCH('Data LU'!A$1&"="&"*",RD!2:2,0)))-(LEN(A$1)+1))),"")

if that helps


回答1:


Assuming you know the set of all possible prefixes, you could do something like this:

with t as (select split_to_map(<column>,';','=') map from <table>)
select
    element_at(map, 'u1') as u1,
    element_at(map, 'u2') as u2,
    element_at(map, 'u3') as u3,
    ...
from t

Unfortunately, there's currently (as of version 304) no way to explode the values into separate columns dynamically.

You can find the documentation for split_to_map and element_at here: https://prestosql.io/docs/current/functions/string.html#split_to_map https://prestosql.io/docs/current/functions/map.html



来源:https://stackoverflow.com/questions/55046879/splitting-a-text-string-to-matching-columns-in-presto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!