En alguna ocasión 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.
Una sentencia típica de creación de una tabla de este tipo podría ser la siguiente:
CREATE table all_data (id int, product struct<name:string,type:string, info_date:string, value:double>, person struct<name:string,type:string, info_date:string, value:double>, buy struct<name:string,type:string, info_date:string, value:double>) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',' STORED AS TEXTFILE;
Para realizar una inserción de datos de prueba en esta tabla se puede utilizar el siguiente truco que sólo requiere de la existencia de una tabla (independientemente de que tenga o no datos)
INSERT INTO all_data SELECT 1,
named_struct('name','product', 'type','string', 'info_date','2019-09-03', 'value',100.00),
named_struct('name','person', 'type','string', 'info_date','2019-09-03', 'value',101.00),
named_struct('name','buy', 'type','string', 'info_date','2019-09-03', 'value',102.00)
from dummy_TBL limit 1;
INSERT INTO all_data select 2,
> named_struct('name','product_2', 'type','string', 'info_date','2019-09-03', 'value',2100.00),
> named_struct('name','person_2', 'type','string', 'info_date','2019-09-03', 'value',2101.00),
> named_struct('name','buy_2', 'type','string', 'info_date','2019-09-03', 'value',2102.00)
> from dummy_TBL limit 1;
Para realizar la consulta de la información de la tabla se utiliza la siguiente sintáxis:
SELECT all_data.person.value, all_data.person.type, all_data.person.name, all_data.product.value FROM all_data;
Que devolverá lo siguiente…
OK
101.0 string person 100.0
Time taken: 0.167 seconds, Fetched: 1 row(s)
SELECT * FROM all_data WHERE all_data.person LIKE 'person%';
OK
1 {«name»:»product»,»type»:»string»,»info_date»:»2019-09-03″,»value»:100.0} {«name»:»person»,»type»:»string»,»info_date»:»2019-09-03″,»value»:101.0} {«name»:»buy»,»type»:»string»,»info_date»:»2019-09-03″,»value»:102.0}
2 {«name»:»product_2″,»type»:»string»,»info_date»:»2019-09-03″,»value»:2100.0} {«name»:»person_2″,»type»:»string»,»info_date»:»2019-09-03″,»value»:2101.0} {«name»:»buy_2″,»type»:»string»,»info_date»:»2019-09-03″,»value»:2102.0}
Time taken: 0.146 seconds, Fetched: 2 row(s)
SELECT * FROM all_data WHERE all_data.person.name LIKE '%_2';
OK
2 {«name»:»product_2″,»type»:»string»,»info_date»:»2019-09-03″,»value»:2100.0} {«name»:»person_2″,»type»:»string»,»info_date»:»2019-09-03″,»value»:2101.0} {«name»:»buy_2″,»type»:»string»,»info_date»:»2019-09-03″,»value»:2102.0}
Time taken: 0.097 seconds, Fetched: 1 row(s)