Hive 入门-常用语句整理

黎东
L、先森
2019-01-18 0 2171

1)表

--Table:内部表

--Partition:分区表

--External Table:外部表

--Bucket Table:桶表

2)视图

是一个逻辑概念,类似于我们的表

 

===内部表

--与数据库中的Table在概念上是类似的。

--每一个Table在Hive中都有一个相应的存储数据。

--所有的Table数据(不包括External Table)都保存在这个目录中。

--删除表的时候,元数据与数据都会被删除。


在Create/Alter表的时候,可以为表以及分区的文件指定不同的格式

• Storage Formats
• Row Formats
• SerDe
STORED AS file_format
– STORED AS PARQUET
– STORED AS ORC
– STORED AS SEQUENCEFILE
– STORED AS AVRO
– STORED AS TEXTFILE


创建外部表

CREATE EXTERNAL TABLE `myhive`.`shqz_resource_change` (
	`log_ymd` int,
	`log_ym` int,
	`year` int,
	`month` int,
	`day` int,
	`hour` int,
	`minute` int,
	`week` int,
	`app_channel` string,
	`channel_id` int,
	`server` int,
	`account_id` string,
	`role_id` string,
	`role_name` string,
	`role_level` int,
	`reason_time` int,
	`change` int,
	`reason` string,
	`bonus_money` int,
	`bonus_gold` int,
	`bonus_item` string,
	`bonus_exp` int,
	`bonus_zuanshi` int,
	`jifen` int,
	`renpin` int,
	`babypiece` int,
	`group_id` int
)
PARTITIONED BY (
	`ym` int,
	`ymd` int
)
ROW FORMAT DELIMITED
	FIELDS TERMINATED BY '\t'
STORED AS `TEXTFILE`
LOCATION '/user/hive/warehouse/myhive.db/ResourceChange'

载入本地数据

load data local inpath '/home/orco/tempdata/user.data' overwrite into table user_dimension;

载入HDFS数据

load data inpath '/user/orco/practice_1/user.data' overwrite into table user_dimension;

一次增加一个分区

alter table testljb add partition (age=2);

一次增加多个分区

alter table testljb add partition(age=3) partition(age=4);

删除分区

alter table testljb drop partition(age=1);

修复分区

msck repair table table_name;

查询分区

show partitions table_name;

修改列的顺序

ALTER TABLE table_name CHANGE col_old_name col_new_name column_type AFTER column_name;
 
/* will change column a's name to a1, a's data type to string, and put it after column b. The new table's structure is: b int, a1 string, c int*/
ALTER TABLE test_change CHANGE a a1 STRING AFTER b; 
/* will change column b's name to b1, and put it as the first column. The new table's structure is: b1 int, a int, c int*/
ALTER TABLE test_change CHANGE b b1 INT FIRST;

添加列

ALTER TABLE table_name ADD COLUMNS (col_name STRING);  //在所有存在的列后面,但是在分区列之前添加一列


大数据