Here we have a hyphened string like 0-1-3
.... and the length is not fixed,
also we have a DETAIL table in hive to explain the meaning of each code.
DETAIL
| code | desc |
+ ---- + ---- +
| 0 | AAA |
| 1 | BBB |
| 2 | CCC |
| 3 | DDD |
Now we need a hive query to convert the code string into a description string.
For example: the case 0-1-3
should get a string like AAA-BBB-DDD
.
any advice on how to get that ?
Split
your string to get an array,explode
array and join with detail table (CTE is used in my example instead of it, use normal table instead) to get desc joined with code. Then assemble string usingcollect_list(desc)
to get an array +concat_ws()
to get concatenated string:Result:
In case you need to preserve the original order, then use
posexplode
it returns the element as well as its position in the original array. Then you can order by record ID and pos beforecollect_list()
.If your string is a table column then use lateral view to select exploded values.
This is more complicated example with order preserved and lateral view.
Result:
Note that
distribute
+sort
is being used instead of simplyorder by str, pos
. distribute + sort works in fully distributed mode,order by
will work also correct but on single reducer.