<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Trying to use  Hive EXPLODE function to &amp;quot;unfold&amp;quot; an array within a field into multiple rows in Archives of Support Questions (Read Only)</title>
    <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103696#M29893</link>
    <description>&lt;PRE&gt;Thanks a lot for helping me!  I'm still trying to figure this out.  So this query: 

Select quote_id,
get_json_object(message_full,'$.event.quote.vehicles.coverages.limits.type') as coverage_type
from dcbi_dev
where event_class = 'events.quote.QuoteCreated' and quote_id = '57226f1e01a9c82283d02ff8' 
;

Returns this: (see table)

How would I modify my code to return 3 lines with the same quote_id and "Deductible" for the coverage_type for row 1, "PerDay" for row 2, and "PerOccurence" for row 3? 


Thanks again for the help





&lt;/PRE&gt;
&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;quote_id	&lt;/TD&gt;&lt;TD&gt;coverage_type&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;57226f1e01a9c82283d02ff8	&lt;/TD&gt;&lt;TD&gt;["Deductible","PerDay","PerOccurrence"]&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
    <pubDate>Tue, 31 May 2016 07:26:50 GMT</pubDate>
    <dc:creator>steve_kaufman</dc:creator>
    <dc:date>2016-05-31T07:26:50Z</dc:date>
    <item>
      <title>Trying to use  Hive EXPLODE function to "unfold" an array within a field into multiple rows</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103694#M29891</link>
      <description>&lt;P&gt;Hi, &lt;/P&gt;&lt;P&gt;I am new to Hive.  We are using NIFI to bring data in from JSON messages into Hortonworks HDFS. &lt;/P&gt;&lt;P&gt;Our JSON messages are nested many levels deep.  Using get_json_object, I am pulling back data from a table that holds coverage data for an insurance policy. &lt;/P&gt;&lt;P&gt;
Here is my query:&lt;/P&gt;&lt;P&gt;
---------------start query-------------------- &lt;/P&gt;&lt;P&gt;with QuoteCreated as &lt;/P&gt;&lt;P&gt;
(
Select 
quote_id, &lt;/P&gt;&lt;P&gt;get_json_object(message_full,'$.event.quote.vehicles.coverages.limits.type') as coverage_type &lt;/P&gt;&lt;P&gt;from  dcbi_dev &lt;/P&gt;&lt;P&gt;where
event_class = 'events.quote.QuoteCreated'
  and quote_id = '57226f1e01a9c82283d02ff8'
) &lt;/P&gt;&lt;P&gt;select quote_id, coverage_type &lt;/P&gt;&lt;P&gt;-- this doesn't work --&amp;gt; explode(coverage_type)
from QuoteCreated; &lt;/P&gt;&lt;P&gt;---------------end query-------------------- &lt;/P&gt;&lt;P&gt;When I run this query in Hive (from Ambari), it returns single row with these values: &lt;/P&gt;&lt;P style="margin-left: 20px;"&gt;
quote_id = 57226f1e01a9c82283d02ff8 &lt;/P&gt;&lt;P style="margin-left: 20px;"&gt;coverage_type = 
["BodilyInjury","PropertyDamage","RentalReimbursement"]&lt;/P&gt;&lt;P&gt;I want to have, instead, 3 rows with the same quote_id, and each row will have one of the 3 values for the coverage_type &lt;/P&gt;&lt;P&gt;I've been attempting to use the Explode function, but when I do, it gives me this error: &lt;/P&gt;&lt;P style="margin-left: 20px;"&gt;
"Error while compiling statement: FAILED: SemanticException [Error 10081]: UDTF's are not supported outside the SELECT clause, nor nested in expressions [ERROR_STATUS]" &lt;/P&gt;&lt;P&gt;Research led me to believe that my "array" is, in fact, being stored as a string.  I've been unable to convert it to an array or figure out how to make this work. &lt;/P&gt;&lt;P&gt;
Can anyone help me figure out how to do this?  This would be awesome if we could use explode to unfold arrays into multiple rows for queries and reporting. 
Thanks very much for any help.&lt;/P&gt;</description>
      <pubDate>Thu, 26 May 2016 23:49:50 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103694#M29891</guid>
      <dc:creator>steve_kaufman</dc:creator>
      <dc:date>2016-05-26T23:49:50Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use  Hive EXPLODE function to "unfold" an array within a field into multiple rows</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103695#M29892</link>
      <description>&lt;P&gt;@stevekaufman, you need to use lateral view along with explode to generate 3 rows with same quote_id. Also, explode() only takes array() or map() as input. So, you should convert coverage_type to one of these formats.&lt;/P&gt;&lt;P&gt;Here's similar example:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;hive&amp;gt; select get_json_object(json_table.json,'$') from json_table;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;OK &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;{"id":"1","colorsArray":["red","green","blue","cyan"]} &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Time taken: 0.199 seconds, Fetched: 1 row(s) &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;hive&amp;gt; with q as  &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;       &amp;gt; ( &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;       &amp;gt; select get_json_object(json_table.json, '$.id') as id, &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;       &amp;gt; get_json_object(json_table.json, '$.colorsArray') as colorsArray from json_table &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;       &amp;gt; ) &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;       &amp;gt; select id, b from q lateral view explode(split(substr(q.colorsArray,2,length(q.colorsArray) - 2),',')) exploded as b;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;
Query ID = hrt_qa_20160527223241_c69a0710-3d8d-4cb0-b63c-3d930f94b751 &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Total jobs = 1
Launching Job 1 out of 1
Status: Running (Executing on YARN cluster with App id application_1464124515927_0014)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;
-------------------------------------------------------------------------------- &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;        VERTICES      STATUS  TOTAL  COMPLETED  RUNNING  PENDING  FAILED  KILLED&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;
-------------------------------------------------------------------------------- &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Map 1 ..........   SUCCEEDED      1          1        0        0       0       0 &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;--------------------------------------------------------------------------------&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;
VERTICES: 01/01  [==========================&amp;gt;&amp;gt;] 100%  ELAPSED TIME: 6.75 s&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;    
-------------------------------------------------------------------------------- &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;OK &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1     "red" &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1     "green" &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1     "blue" &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;1     "cyan" &lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;Time taken: 7.631 seconds, Fetched: 4 row(s)) from json_table;&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 28 May 2016 05:54:23 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103695#M29892</guid>
      <dc:creator>jvaria</dc:creator>
      <dc:date>2016-05-28T05:54:23Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use  Hive EXPLODE function to "unfold" an array within a field into multiple rows</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103696#M29893</link>
      <description>&lt;PRE&gt;Thanks a lot for helping me!  I'm still trying to figure this out.  So this query: 

Select quote_id,
get_json_object(message_full,'$.event.quote.vehicles.coverages.limits.type') as coverage_type
from dcbi_dev
where event_class = 'events.quote.QuoteCreated' and quote_id = '57226f1e01a9c82283d02ff8' 
;

Returns this: (see table)

How would I modify my code to return 3 lines with the same quote_id and "Deductible" for the coverage_type for row 1, "PerDay" for row 2, and "PerOccurence" for row 3? 


Thanks again for the help





&lt;/PRE&gt;
&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;quote_id	&lt;/TD&gt;&lt;TD&gt;coverage_type&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;57226f1e01a9c82283d02ff8	&lt;/TD&gt;&lt;TD&gt;["Deductible","PerDay","PerOccurrence"]&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Tue, 31 May 2016 07:26:50 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103696#M29893</guid>
      <dc:creator>steve_kaufman</dc:creator>
      <dc:date>2016-05-31T07:26:50Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use  Hive EXPLODE function to "unfold" an array within a field into multiple rows</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103697#M29894</link>
      <description>&lt;P&gt;You should change your query to this:&lt;/P&gt;&lt;P&gt;&lt;EM&gt;with q as&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;(&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;select quote_id,&lt;/P&gt;&lt;P&gt;get_json_object(message_full,'$.event.quote.vehicles.coverages.limits.type') as coverage_type&lt;/P&gt;&lt;P&gt;from dcbi_dev&lt;/P&gt;&lt;P&gt;where event_class = 'events.quote.QuoteCreated' and quote_id = '57226f1e01a9c82283d02ff8' &lt;/P&gt;&lt;P&gt;&lt;EM&gt; )&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;select &lt;/EM&gt;quote_id&lt;EM&gt;, b from q lateral view explode(split(substr(q.&lt;/EM&gt;coverage_type&lt;EM&gt;,2,length(q.&lt;/EM&gt;coverage_type&lt;EM&gt;) - 2),',')) exploded as b;&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Jun 2016 01:21:51 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103697#M29894</guid>
      <dc:creator>jvaria</dc:creator>
      <dc:date>2016-06-01T01:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use  Hive EXPLODE function to "unfold" an array within a field into multiple rows</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103698#M29895</link>
      <description>&lt;P&gt;I cannot even say how much this helps me.  Thanks so much &lt;/P&gt;</description>
      <pubDate>Fri, 03 Jun 2016 01:04:03 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/103698#M29895</guid>
      <dc:creator>steve_kaufman</dc:creator>
      <dc:date>2016-06-03T01:04:03Z</dc:date>
    </item>
    <item>
      <title>Re: Trying to use  Hive EXPLODE function to "unfold" an array within a field into multiple rows</title>
      <link>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/293805#M29896</link>
      <description>&lt;P&gt;you&amp;nbsp;can&amp;nbsp;try&lt;BR /&gt;&lt;A href="https://my.oschina.net/leejun2005/blog/120463" target="_blank"&gt;https://my.oschina.net/leejun2005/blog/120463&lt;/A&gt;&lt;/P&gt;&lt;P&gt;For example,&lt;/P&gt;&lt;P&gt;select a.timestamp, get_json_object(a.appevents, '$.eventid'), get_json_object(a.appenvets, '$.eventname') from log a;&lt;BR /&gt;should be changed to&lt;/P&gt;&lt;P&gt;select a.timestamp, b.*&lt;BR /&gt;from log a lateral view json_tuple(a.appevent, 'eventid', 'eventname') b as f1, f2;&lt;BR /&gt;UDTF(User-Defined Table-Generating Functions) 用来解决 输入一行输出多行(On-to-many maping) 的需求。&lt;/P&gt;</description>
      <pubDate>Mon, 13 Apr 2020 11:56:21 GMT</pubDate>
      <guid>https://community.cloudera.com/t5/Archives-of-Support-Questions/Trying-to-use-Hive-EXPLODE-function-to-quot-unfold-quot-an/m-p/293805#M29896</guid>
      <dc:creator>Lis</dc:creator>
      <dc:date>2020-04-13T11:56:21Z</dc:date>
    </item>
  </channel>
</rss>

