Maxcompute最簡單的造數據的方法是insert into values語句,這一般也是我最常用的。在不支持這個語句之前的更早的版本,使用的是union all的方法。如果不想實際寫入數據到,則可以使用from values 和 with 表達式。
示例1:通過insert … values操作向特定分區內插入數據。
命令示例如下:
--創建分區表srcp。
create table if not exists srcp (key string,value bigint) partitioned by (p string);--向分區表srcp添加分區。
alter table srcp add if not exists partition (p='abc');--向表srcp的指定分區abc中插入數據。
insert into table srcp partition (p='abc') values ('a',1),('b',2),('c',3);--查詢表srcp。
select * from srcp where p='abc';--返回結果。
+------------+------------+------------+
| key | value | p |
+------------+------------+------------+
| a | 1 | abc |
| b | 2 | abc |
| c | 3 | abc |
+------------+------------+------------+
示例2:通過values table操作插入數據。
命令示例如下:
--創建分區表srcp。
create table if not exists srcp (key string,value bigint) partitioned by (p string);--向表srcp中插入數據。
insert into table srcp partition (p) select concat(a,b), length(a)+length(b),'20170102' from values ('d',4),('e',5),('f',6) t(a,b);--查詢表srcp。
select * from srcp where p='20170102';--返回結果。
+------------+------------+------------+
| key | value | p |
+------------+------------+------------+
| d4 | 2 | 20170102 |
| e5 | 2 | 20170102 |
| f6 | 2 | 20170102 |
+------------+------------+------------+
with t as (select 1 c union all select 2 c) select * from t;
--等價于如下語句。
select * from values (1), (2) t(c);--返回結果。
+------------+
| c |
+------------+
| 1 |
| 2 |
+------------+
-- 利用代碼表轉文本
with za as (
select * from values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15)
,(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26),(27),(28),(29),(30)
,(31),(32),(33),(34),(35),(36),(37),(38),(39),(40),(41),(42),(43),(44),(45)
,(46),(47),(48),(49),(50),(51),(52),(53),(54),(55),(56),(57),(58),(59),(60)
,(61),(62),(63)
t(c0)
)
,ta as (
select * from values ('zhangsan',4),('lisi',5),('wangmazi',6) t(a,b))
select k,a,b,cfrom(
select 100 + row_number() over(partition by 1)-1 as k,cast(round(rand()*3,0) as bigint)+3 as cfrom za -- 63limit 3
)tb join ta on ta.b=tb.c
;
返回:
k a b c
101 lisi 5 5
102 wangmazi 6 6
103 zhangsan 4 4-- 利用unixtimetamp轉日期時間
with za as (
select * from values(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15)
,(16),(17),(18),(19),(20),(21),(22),(23),(24),(25),(26),(27),(28),(29),(30)
,(31),(32),(33),(34),(35),(36),(37),(38),(39),(40),(41),(42),(43),(44),(45)
,(46),(47),(48),(49),(50),(51),(52),(53),(54),(55),(56),(57),(58),(59),(60)
,(61),(62),(63)
t(c0)
)
select k
,from_unixtime(1617120000) as t
,from_unixtime(1617120000
+3600000 * c ) -- 小時as b
,cfrom(
select 100 + row_number() over(partition by 1)-1 as k,cast(round(rand()*3,0) as bigint)+3 as cfrom za -- 63limit 3
)tb
;
返回:
k t b c
100 2021-03-31 00:00:00 2021-03-31 03:00:00 3
101 2021-03-31 00:00:00 2021-03-31 05:00:00 5
102 2021-03-31 00:00:00 2021-03-31 06:00:00 6
drop table if exists t_tac_lacid;
create table if not exists t_tac_lacid (id bigint,tac bigint,lacid bigint);insert overwrite table t_tac_lacid
select /*+mapjoin(t2)*/row_number() over(partition by 1)+100000 as rn
,t1.c0+6001 as tac
,t2.c0+1201 as lacid
from (select row_number() over(partition by 1)-1 as c0 from zb1 limit 2300)t1
join (select row_number() over(partition by 1)-1 as c0 from zb1 limit 100)t2
;
-- 230000
set odps.sql.mapper.cpu=200;
set odps.sql.mapper.memory=8192;
set odps.sql.mapper.split.size=4;
set odps.sql.reducer.cpu=200;
set odps.sql.reducer.memory=8192;