{"id":336,"date":"2019-09-03T15:43:43","date_gmt":"2019-09-03T14:43:43","guid":{"rendered":"http:\/\/justmakeit.es\/?p=336"},"modified":"2019-09-03T15:43:43","modified_gmt":"2019-09-03T14:43:43","slug":"hive-tablas-con-struct","status":"publish","type":"post","link":"http:\/\/justmakeit.es\/?p=336","title":{"rendered":"Hive tablas con Struct"},"content":{"rendered":"\n<p>En alguna ocasi\u00f3n es posible encontrarse con tablas que tienen datos complejos como campos; esto puede ser debido al modo en el que los datos son ingestados en HDFS.<\/p>\n\n\n\n<p>Una sentencia t\u00edpica de creaci\u00f3n de una tabla de este tipo podr\u00eda ser la siguiente:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">CREATE table all_data (id int, product struct&lt;name:string,type:string, info_date:string, value:double>, person struct&lt;name:string,type:string, info_date:string, value:double>, buy struct&lt;name:string,type:string, info_date:string, value:double>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE; <\/pre>\n\n\n\n<p>Para realizar una inserci\u00f3n de datos de prueba en esta tabla se puede utilizar el siguiente truco que s\u00f3lo requiere de la existencia de una tabla (independientemente de que tenga o no datos) <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INSERT INTO all_data SELECT 1,<br> named_struct('name','product', 'type','string', 'info_date','2019-09-03', 'value',100.00),<br> named_struct('name','person', 'type','string', 'info_date','2019-09-03', 'value',101.00),<br> named_struct('name','buy', 'type','string', 'info_date','2019-09-03', 'value',102.00)<br> from dummy_TBL limit 1;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">INSERT INTO all_data select 2,<br>     > named_struct('name','product_2', 'type','string', 'info_date','2019-09-03', 'value',2100.00),<br>     > named_struct('name','person_2', 'type','string', 'info_date','2019-09-03', 'value',2101.00),<br>     > named_struct('name','buy_2', 'type','string', 'info_date','2019-09-03', 'value',2102.00)<br>     > from dummy_TBL limit 1;<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Para realizar la consulta de la informaci\u00f3n de la tabla se utiliza la siguiente sint\u00e1xis:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT all_data.person.value, all_data.person.type, all_data.person.name, all_data.product.value FROM all_data;<\/pre>\n\n\n\n<p>Que devolver\u00e1 lo siguiente&#8230;<\/p>\n\n\n\n<p>OK<br>\n101.0    string  person  100.0<br>\nTime taken: 0.167 seconds, Fetched: 1 row(s)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT * FROM all_data WHERE all_data.person LIKE 'person%';<\/pre>\n\n\n\n<p>OK<br> 1    {\u00abname\u00bb:\u00bbproduct\u00bb,\u00bbtype\u00bb:\u00bbstring\u00bb,\u00bbinfo_date\u00bb:\u00bb2019-09-03&#8243;,\u00bbvalue\u00bb:100.0}   {\u00abname\u00bb:\u00bbperson\u00bb,\u00bbtype\u00bb:\u00bbstring\u00bb,\u00bbinfo_date\u00bb:\u00bb2019-09-03&#8243;,\u00bbvalue\u00bb:101.0}    {\u00abname\u00bb:\u00bbbuy\u00bb,\u00bbtype\u00bb:\u00bbstring\u00bb,\u00bbinfo_date\u00bb:\u00bb2019-09-03&#8243;,\u00bbvalue\u00bb:102.0}<br> 2    {\u00abname\u00bb:\u00bbproduct_2&#8243;,\u00bbtype\u00bb:\u00bbstring\u00bb,\u00bbinfo_date\u00bb:\u00bb2019-09-03&#8243;,\u00bbvalue\u00bb:2100.0}    {\u00abname\u00bb:\u00bbperson_2&#8243;,\u00bbtype\u00bb:\u00bbstring\u00bb,\u00bbinfo_date\u00bb:\u00bb2019-09-03&#8243;,\u00bbvalue\u00bb:2101.0} {\u00abname\u00bb:\u00bbbuy_2&#8243;,\u00bbtype\u00bb:\u00bbstring\u00bb,\u00bbinfo_date\u00bb:\u00bb2019-09-03&#8243;,\u00bbvalue\u00bb:2102.0}<br> Time taken: 0.146 seconds, Fetched: 2 row(s)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT * FROM all_data WHERE all_data.person.name LIKE '%_2';<\/pre>\n\n\n\n<p>OK<br> 2    {\u00abname\u00bb:\u00bbproduct_2&#8243;,\u00bbtype\u00bb:\u00bbstring\u00bb,\u00bbinfo_date\u00bb:\u00bb2019-09-03&#8243;,\u00bbvalue\u00bb:2100.0}    {\u00abname\u00bb:\u00bbperson_2&#8243;,\u00bbtype\u00bb:\u00bbstring\u00bb,\u00bbinfo_date\u00bb:\u00bb2019-09-03&#8243;,\u00bbvalue\u00bb:2101.0} {\u00abname\u00bb:\u00bbbuy_2&#8243;,\u00bbtype\u00bb:\u00bbstring\u00bb,\u00bbinfo_date\u00bb:\u00bb2019-09-03&#8243;,\u00bbvalue\u00bb:2102.0}<br> Time taken: 0.097 seconds, Fetched: 1 row(s)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Uso y disfrute de tablas con datos complejos en Hive<\/p>\n","protected":false},"author":1,"featured_media":305,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[37,38],"class_list":["post-336","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programacion","tag-hive","tag-struct"],"_links":{"self":[{"href":"http:\/\/justmakeit.es\/index.php?rest_route=\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/justmakeit.es\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/justmakeit.es\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/justmakeit.es\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/justmakeit.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=336"}],"version-history":[{"count":2,"href":"http:\/\/justmakeit.es\/index.php?rest_route=\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":338,"href":"http:\/\/justmakeit.es\/index.php?rest_route=\/wp\/v2\/posts\/336\/revisions\/338"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/justmakeit.es\/index.php?rest_route=\/wp\/v2\/media\/305"}],"wp:attachment":[{"href":"http:\/\/justmakeit.es\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/justmakeit.es\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/justmakeit.es\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}