MAPREDUCE服务 MRS-INSERT:示例

时间:2024-04-22 14:52:06

示例

  • 创建fruit和fruit_copy表:
    create table fruit (name varchar,price double);
    create table fruit_copy (name varchar,price double);
  • 向fruit表中插入一行数据:
    insert into fruit values('LIchee',32);
    -- 兼容写法示例,带上table关键字
    insert into table fruit values('Cherry',88);
  • 向fruit表中插入多行数据:
    insert into fruit values('banana',10),('peach',6),('lemon',12),('apple',7);
  • 将fruit表中的数据行加载到fruit_copy表中,执行后表中有5条记录:
    insert into fruit_copy select * from fruit;
  • 先清空fruit_copy表,再将fruit中的数据加载到表中,执行之后表中有2条记录:
    insert overwrite fruit_copy select *  from fruit limit 2;
  • 对于varchar类型,仅当目标表定义的列字段长度大于源表的实际字段长度时,才可以使用INSERT... SELECT...的形式从源表中查数据并且插入到目标表:
    create table varchar50(c1 varchar(50));
    insert into varchar50 values('hetuEngine');
    create table varchar100(c1 varchar(100));
    insert into varchar100 select * from varchar50;
  • 分区表使用insert overwrite语句时,只会清理插入值所在分区的数据,而不是整个表:
    --创建表
    create table test_part (id int, alias varchar) partitioned by (dept_id int, status varchar);
    
    insert into test_part  partition(dept_id=10, status='good') values (1, 'xyz'), (2, 'abc');
    
    select * from test_part order by id;
     id | alias | dept_id | status 
    ----|-------|---------|--------
      1 | xyz   |      10 | good   
      2 | abc   |      10 | good   
    (2 rows)
    
    --清理分区partition(dept_id=25, status='overwrite'),并插入一条数据
    insert overwrite test_part (id, alias, dept_id, status) values (3, 'uvw', 25, 'overwrite');
     select * from test_part ;
     id | alias | dept_id |  status   
    ----|-------|---------|-----------
      1 | xyz   |      10 | good      
      2 | abc   |      10 | good      
      3 | uvw   |      25 | overwrite
    
    --清理分区partition(dept_id=10, status='good'),并插入一条数据
    insert overwrite test_part (id, alias, dept_id, status) values (4, 'new', 10, 'good');
    select * from test_part ordr;
     id | alias | dept_id |  status   
    ----|-------|---------|-----------
      3 | uvw   |      25 | overwrite 
      4 | new   |      10 | good      
    (2 rows)
    
    --分区表插入数据
    create table test_p_1(name string, age int) partitioned by (provice string, city string);
    
     create table test_p_2(name string, age int) partitioned by (provice string, city string);
     
    -- 填充数据到test_p_1
     insert into test_p_1 partition (provice = 'hebei', city= 'baoding') values ('xiaobei',15),( 'xiaoming',22);
    -- 根据test_p_1 插入数据到test_p_2
    
    -- 方式一
    from test_p_1 insert into table test_p_2 partition (provice = 'hebei', city= 'baoding') select name,age;
     
    -- 方式二
    insert into test_p_2 partition(provice = 'hebei', city= 'baoding') select name,age from test_p_1;
support.huaweicloud.com/cmpntguide-lts-mrs/mrs_01_2499053.html