1

I have a table with two columns and i am trying get multiple rows with the values in the array.My table is like this.

date                  users
2019-01-01       ["U00001","U00002","U00002"]

I am trying to get the output like below

date               users
2019-01-01       "U00001"
2019-01-01       "U00002"
2019-01-01       "U00003"

I am using below query

SELECT date, user FROM  table1
LATERAL VIEW  explode(users)  myTable2 AS user;

I am not able to get the output that i expected above and
my query results are showing like this.

date                 user
2019-01-01    "U00001","U00002","U00002"

my column data types are

column         data_type
date            string
user            Array
leftjoin
  • 36,950
  • 8
  • 57
  • 116
Rahul
  • 467
  • 1
  • 8
  • 24

1 Answers1

0

date and user are reserverd words in Hive, use backticks. Also (check my example) lateral view should have an alias (u), column exploded an alias user:

with your_data as (--use your table instead of this 
select stack(1, '2019-01-01', array("U00001","U00002","U00002")) as(`date`, users)
)

select t.`date`, u.`user` 
  from your_data t --use your table instead
       lateral view explode(t.users) u as `user` ;

If users is of type STRING, then remove square brackets and double-quotes, then split and explode:

with your_data as (--use your table instead of this 
select stack(1, '2019-01-01', '["U00001","U00002","U00002"]') as (`date`, users)
)

select t.`date`, u.`user` 
  from your_data t --use your table instead
       lateral view explode(split(regexp_replace(t.users,'\\[|\\]|\\"',''),',')) u as `user` ;

Result:

t.date      u.user
2019-01-01  U00001
2019-01-01  U00002
2019-01-01  U00002
leftjoin
  • 36,950
  • 8
  • 57
  • 116
  • Thanks @LeftJoin , I've tried this ,but my users column is type of string and it looks like this '"U00001","U00002","U00003"' ,since explode will take only array or map type ,i tried to convert users column to array type and i did this Select array('"U00001","U00002","U00003"') from table and i got the output like this ["\"U00001\",\"U00002\",\"U00003\""] ,not understanding why i am getting '\' in the output – Rahul Mar 24 '20 at 16:38
  • @Rahul What exactly USER column contains? This string `'["U00001","U00002","U00002"]' `? then why are you using array(something) – leftjoin Mar 24 '20 at 17:58
  • @Rahul Updated answer for string type – leftjoin Mar 24 '20 at 18:09
  • @Rahul and answering your question why array('"U00001","U00002","U00003"') results in ["\"U00001\",\"U00002\",\"U00003\""]? Because array printed as a JSON string, each element is enclosed in quotes. Your array of SINGLE element contains double-quotes inside, which Hive shields with slash to produce correct JSON. Double-quotes in JSON should be shielded according to JSON spec, because double-quotes are special character. To produce array from string as yours, you need split functionm not array(). – leftjoin Mar 24 '20 at 18:34
  • Select split('"U00001","U00002","U00003"',',') is also giving the same results ["\"U00001\"","\"U00002\"","\"U00003\""] – Rahul Mar 24 '20 at 19:33
  • @Rahul Values contain enclosed double-quotes, they are shielded and quoted again. Remove them like in my answer. (in the second example). – leftjoin Mar 25 '20 at 05:10
  • @Rahul SPLIT produces array and array is printed as JSON, enclosed quotes are shielded. Consider this example for better understanding: Select split('"U00001","U00002","U00003"',',')[0] - it returns 0 array element and without shielding: "U00001". It is scalar value, printed as is. If you want to convert array to string wo shielding, use some function like concat_ws – leftjoin Mar 25 '20 at 05:16
  • @Rahul Example with concatenating array: Select concat_ws(',',split('"U00001","U00002","U00003"',',')). Result is "U00001","U00002","U00003" - without shielding. STRINGS are printed as is. Complex types require conversion. If you do not convert explicitly, then default method toString executed, and in this method array converted to JSON string, quotes are shielded. Explicitly convert complex types to strings using some function if you want some other behavior than it's own toString – leftjoin Mar 25 '20 at 05:24
  • Got it Thanks @leftjoin – Rahul Mar 25 '20 at 19:14