Hive的DDL操作
创建表(四种表)
- 内部表
创建产品表
create table t_product(id int,name string,price double,category string)
row format delimited
fields terminated by ','
stored as textfile;
导入数据(从本地)
load data local inpath '/home/hadoop/product_data' into table t_product;
导入数据(从hdfs)
load data inpath '/data/hive/test/product_data' into table t_product;
查看表数据
select * from t_product;
删除表
drop table t_product;
- 外部表
创建手机表
create external table t_phone(id int,name string,price double)
row format delimited
fields terminated by ','
stored as textfile
location '/hive/test/';
注:在hdfs的指定位置上创建表
导入数据
load data local inpath '/home/hadoop/phone_data' into table t_phone;
- 分区表
创建表(分区表)
根据月份分区
create table t_order(id int,name string,cost double)
partitioned by (month string)
row format delimited
fields terminated by ',';
导入数据到分区6
load data local inpath '/home/hadoop/phone_data' into table t_order
partition(month='6');
查看所有订单的分区
show partitions t_order;
- 桶表
创建表(桶表)
create table t_product_bucket(id int,name string ,price string,category string)
clustered by(id) into 3 buckets
row format delimited
fields terminated by ',';
桶表汇中的数据,只能从其他表中用子查询进行插入
set hive.enforce.bucketing=true;
insert into table t_product_bucket select * from t_product;
查询2上的数据
select * from t_product_bucket tablesample(bucket 2 out of 3 on id);
创建表(其它常用表)
- 子查询创建表
create table t