如何在没有任何中间文件的情况下创建配置单元表?

时间:2014-01-28 17:58:59

标签: sql hive

我想创建并填充一个配置单元表而不从磁盘加载任何东西。

具体来说,我有

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

我想创建一个包含9行的表:

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

同样,我想写一个csv文件并将其加载到配置单元。

用例:

  1. 迭代
  2. 为SO问题创建小型测试样本

2 个答案:

答案 0 :(得分:3)

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;

答案 1 :(得分:0)

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;

唉,严格来说,这需要<an_already_existing_non_empty_table> ......