How do I create a hive table without any intermedi

2019-02-15 23:13发布

问题:

I want to create and populate a hive table without loading anything from disk.

Specifically, I have

set idlist = (1,2,3);
set values = (2,3,5);

I want to create a table with 9 rows:

id value
1  2
1  3
1  5
2  2
2  3
2  5
3  2
3  3
3  5

Again, I do not want to write a csv file and load it into hive.

Use cases:

  1. iteration
  2. creating small test samples for SO questions

回答1:

create table my_table(id int, value int);

insert overwrite table my_table
select a.id as id, b.value as value from (
  select count(*) from my_table
) t
lateral view explode(array(1,2,3)) a as id
lateral view explode(array(2,3,5)) b as value;


回答2:

As explained in Hive insert query like SQL:

drop table if exists tmp__one;
create table tmp__one as select 1 as one
from <an_already_existing_non_empty_table> limit 1;

drop table if exists tmp__ids;
create table tmp__ids as select stack(3,1,2,3) as id 
from tmp__one;

drop table if exists tmp__vals;
create table tmp__vals as select stack(3,2,3,5) as val
from tmp__one;

drop table if exists my_table;
create table my_table as select id, val
from tmp__ids cross join tmp__vals;

select * from my_table;

drop table tmp__one;
drop table tmp__ids;
drop table tmp__vals;

Alas, strictly speaking, this requires <an_already_existing_non_empty_table>...



标签: sql hive