kudu sql 实操

impala操作kudu

建表

  • hash分区
CREATE TABLE xiniu.my_first_table
(
  id BIGINT,
  name STRING,
  PRIMARY KEY(id)
)
PARTITION BY HASH PARTITIONS 16
STORED AS KUDU;

kudu sql 实操_第1张图片

  • hash分区且指定副本数
CREATE TABLE xiniu.replicastable1
(
  id BIGINT,
  name STRING,
  PRIMARY KEY(id)
)
PARTITION BY HASH PARTITIONS 16
STORED AS KUDU
TBLPROPERTIES ('kudu.num_tablet_replicas' = '1')
;

默认副本数为3,查看副本数"kudu cluster ksck worker-2"

kudu sql 实操_第2张图片

  • create as select
-- impala创建源表
create table xiniu.old_table (
    ts timestamp,
    name string,
    value string
);

-- create as old_table
create table xiniu.new_table
primary key (ts,name)
partition by hash (name) partitions 5
stored as kudu
as
select ts,name,value from old_table;

kudu sql 实操_第3张图片

  • range分区
create table xiniu.range_table(
    id bigint,
    name string,
    primary key (id)
)
partition by range (id)
(
    partition value = 1,
    partition value = 100,
    partition value = 200,
    partition value = 300,
    partition value = 400,
    partition value = 500
)
stored as kudu
tblproperties ('kudu.num_tablet_replicas' = '1');

kudu sql 实操_第4张图片

kudu sql 实操_第5张图片

  • 组合分区
-- 组合主键必须放在表字段的最前面
-- 分组键必须为主键
create table xiniu.combin_partition_table(
    id bigint,
    age int,
    name string,
    primary key (id,age)
)
partition by hash (id) partitions 5,
range (age)
(
    partition VALUES < 10,
    partition 10 <= VALUES < 20,
    partition 20 <= VALUES < 30,
    partition 30 <= VALUES < 40,
    partition 40 <= VALUES < 60
)
stored as kudu
tblproperties ('kudu.num_tablet_replicas' = '3')
;

kudu sql 实操_第6张图片

  • 增加分区
alter table xiniu.combin_partition_table add range partition 60 < values < 100;

file

  • 删除分区
alter table xiniu.combin_partition_table drop range partition value = 60;

file

插入数据

-- 单条插入
INSERT INTO my_first_table VALUES (99, "sarah");
-- 多条插入
INSERT INTO my_first_table VALUES (1, "john"), (2, "jane"), (3, "jim");
-- upsert 如果主键存在则修改,如果不存在则新增
UPSERT INTO my_first_table VALUES (99, "zoe");
UPSERT INTO my_first_table VALUES (88, "zoe");

kudu sql 实操_第7张图片

file

修改数据

-- update语法仅限于在impala中操作kudu表
UPDATE xiniu.my_first_table SET name="bob" where id = 3;

kudu sql 实操_第8张图片

kudu sql 实操_第9张图片

删除数据

-- delete语法仅限于在impala中操作kudu表
DELETE FROM xiniu.my_first_table WHERE id < 3;

kudu sql 实操_第10张图片

修改表名

ALTER TABLE xiniu.my_first_table RENAME TO xiniu.my_new_table;

kudu sql 实操_第11张图片

impala关联kudu表

create EXTERNAL table xiniu.testbyapimapping 
stored as kudu
TBLPROPERTIES (
  'kudu.table_name' = 'xiniu.testbyapi'
);

kudu sql 实操_第12张图片

海汼部落原创文章,原文链接:http://www.hainiubl.com/topics/75594

你可能感兴趣的:(hadoop,hadoop)