----------------------------------------
基本常用操作:
--描述表
DESCRIBE tabel_name;
--查看分区情况
SHOW PARTITIONS table_name;
--查看当前使用数据库
SELECT current_database();
--查看建表语句
SHOW CREATE TABLE table_name
-------------------------------------------
1.建表
(1)hash分区
--主键两个字段,分区字段未指定 hash分区
create table kudu_first_table(
id int,
name string,
age int,
gender string,
primary key(id,name)
) partition by hash partitions 4
stored as kudu;
--主键两个字段,分区字段指定,hash分区
create table specify_partition_column(
id int,
name string,
age int,
gender string,
primary key(id,name)
) partition by hash(id) partitions 3
stored as kudu;
--主键两个字段,分区字段指定一个字段,hash分区
create table specify_partition_one_column(
id int,
name string,
age int,
gender string,
primary key(id)
) partition by hash(id) partitions 3
stored as kudu;
**区别:未指定分区字段时,其分区字段默认是主键,若主键有两个列则分区字段为两个,指定分区字段时,需要分区列是主键的子集;否则会报错「 Only key columns can be used in PARTITION BY」
**不指定分区:表依然会创建,但是只有一个分区,会提示「Unpartitioned Kudu tables are inefficient for large data sizes.」
(2)range分区:主要针对时间进行range分区
CREATE TABLE cust_behavior (
_id BIGINT PRIMARY KEY,
salary STRING,
edu_level INT,
usergender STRING,
`group` STRING,
city STRING,
postcode STRING,
last_purchase_price FLOAT,
last_purchase_date BIGINT,
category STRING,
sku STRING,
rating INT,
fulfilled_date BIGINT
)
PARTITION BY RANGE (_id)
(
PARTITION VALUES < 1439560049342,
PARTITION 1439560049342 <= VALUES < 1439566253755,
PARTITION 1439566253755 <= VALUES < 1439572458168,
PARTITION 1439572458168 <= VALUES < 1439578662581,
PARTITION 1439578662581 <= VALUES < 1439584866994,
PARTITION 1439584866994 <= VALUES < 1439591071407,
PARTITION 1439591071407 <= VALUES
)
STORED AS KUDU;
**优势:可以根据数据的具体情况建立分区,比如:建立2017年之前的分区,2017-2018,2018-2019,2019-2020,2020-2021,。。。
**劣势:如果使用单级range分区的话,容易产生数据热点问题(可混合hash分区使用)、
在range分区中,如果有不止一个字段作为分区字段的话也可以,语法暂时不清楚
如果插入一条主键的值不落在任何range区间时会插入失败,并报错
(3)混合分区
create table tw_details4(
user_id string,
event_date string,
event string,
properties string,
customer_id int,
project_id int,
primary key(event_date,event,user_id)
) partition by hash(user_id) partitions 3, range(event_date)(
partition values < '2017-01-01',
partition '2017-01-01' <= values < '2018-01-01',
partition '2018-01-01' <= values < '2019-01-01',
partition '2019-01-01' <= values < '2020-01-01',
partition '2020-01-01' <= values < '2021-01-01'
) stored as kudu;
**优势:可以根据时间进行检索,来减少需要scan的tablet,插入的时候不会只有一个tabletserver产生热点
(4)CTAS方式创建表
CREATE TABLE kudu_ti_event_fact_copy
primary key(user_id,event_date)
partition by hash(user_id) partitions 3
stored as kudu
as select user_id,event_date,properties
from auto3.ti_event_fact;
2.创建数据库
impala创建数据库与hive一样,create database db_name,但是这个数据库只是一个impala端的namespace,kudu官网中没有提到数据库的概念,猜测可能是没有这个概念
impala中创建表的时候比如在test数据库中创建table_test对应在kudu中为 test:table_test
3.插入数据
(1)insert into table1 values(v1,v2,v3)
(2)insert into table1 select v1,v2,v3 from table2;
**(3)upsert into table1 values(v1,v2,v3) --根据主键判定,若已经存在则更新,若不存在则插入
4.更改column
(1)update语法
UPDATE kudu_first_table set age = 32 where id= 2;
UPDATE kudu_first_table set age = 31 where gender= 'female';
其中where条件后面的column不是主键也可以但是更改的范围会扩大
**主键中不支持更改,只能删除后重新添加
(2)upsert语法
upsert into table1 values(v1,v2,v3)
**需要更新所有字段
5.更改表
(1)修改表名,修改的只是表在impala中的映射名
alter table kudu_ti_event_fact_copy rename to kudu_ti_event_fact_copy_rename;
(2)修改kudu存储的表名,但是不会改变在impala端的映射表名,也就是在impala中依然访问更改之前的表名
ALTER TABLE kudu_ti_event_fact_copy_rename
SET TBLPROPERTIES('kudu.table_name' = 'kudu_ti_event_fact_copy');
(3)修改列属性
-- --**不支持---
(4)添加列
alter table kudu_ti_event_fact_copy_rename add columns(method string,time_stamp string);
(5)删除列
ALTER table kudu_ti_event_fact_copy_rename drop column method;
(6)删除分区
ALTER TABLE range_partition_table DROP RANGE PARTITION VALUES < '2017-01-01';
(7)添加分区
alter table range_partition_table add range partition values < '2017-01-01';