【Hive】HiveQL:数据定义

HiveQL是Hive查询语言,和普遍使用的所有SQL方言一样,它不完全遵守任一种ANSI SQL标准的修订版。Hive不支持行级插入操作、更新操作和删除操作,Hive也不支持事务,Hive增加了在Hadoop背景下的可以提供更高性能的扩展,以及一些个性化的扩展,甚至还增加了一些外部程序。

一 Hive中的数据库操作

Hive中数据库的概念本质上仅仅是表的一个目录或者命名空间,然而,对于具有很多组和用户的大集群来说,这是非常有用的,因为可以避免表命名冲突,通常会使用数据库来将生产表组织成逻辑组。

如果用户没有显式指定数据库,那么将会使用默认数据块default。

1 Create Database
1)语法结构
CREATE (DATABASE|SCHEMA) [IF NOT EXISTS] database_name
  [COMMENT database_comment]
  [LOCATION hdfs_path]
  [WITH DBPROPERTIES (property_name=property_value, ...)];
2)示例
--创建数据库
hive> create database hive;
OK
Time taken: 0.193 seconds
--查看创建的数据库文件
hive> dfs -ls /user/hive/warehouse/;
Found 2 items
drwxrwxrwx   - hadoop supergroup          0 2018-06-20 17:23 /user/hive/warehouse/hive.db
Hive会为每个数据库创建一个目录,数据库中的表将会以这个数据库目录的子目录的形式存储,有一个例外是default数据库中的表,因为这个数据库本身没有自己的目录。

数据库所在的目录位于配置文件hive-site.xml属性hive.metastore.warehouse.dir所指定的顶层目录之后,默认配置为/user/hive/warehouse。

2 Show Database
1)查看所有数据库
hive> show databases;
OK
database_name
default
hive
test
test1
Time taken: 0.027 seconds, Fetched: 4 row(s)
2)模糊匹配,显示符合条件的数据库
hive> show databases like 'te*';
OK
database_name
test
test1
Time taken: 0.027 seconds, Fetched: 2 row(s)
3 Drop Database
1)语法结构
DROP (DATABASE|SCHEMA) [IF EXISTS] database_name [RESTRICT|CASCADE];
2)示例
--当数据库中存在表时,删除会报错,需要使用Cascade
hive> drop database hive;
FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.exec.DDLTask. InvalidOperationException(message:Database hive is not empty. One or more tables exist.)
hive> drop database hive cascade;
OK
Time taken: 2.279 seconds
--如果数据库为空,直接删除
hive> drop database test1;
OK
Time taken: 0.186 seconds
4 ALTER Database
1)语法结构
ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, ...);   -- (Note: SCHEMA added in Hive 0.14.0)
 
ALTER (DATABASE|SCHEMA) database_name SET OWNER [USER|ROLE] user_or_role;   -- (Note: Hive 0.13.0 and later; SCHEMA added in Hive 0.14.0)
  
ALTER (DATABASE|SCHEMA) database_name SET LOCATION hdfs_path; -- (Note: Hive 2.2.1, 2.4.0 and later)
2 示例
hive> alter database hive set DBPROPERTIES('Created by '='Alen','Create Date'='2018-06-20');
OK
Time taken: 0.188 seconds
5 Use Database

1)语法结构
USE database_name;
USE DEFAULT;
2)示例
hive> use hive;
OK
Time taken: 0.048 seconds
hive> select current_database();
OK
_c0
hive
Time taken: 0.165 seconds, Fetched: 1 row(s)
注:可通过 set hive.cli.print.current.db=true;设置提示。

二 Hive中的表操作

1 创建表
1)语法结构
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name    -- (Note: TEMPORARY available in Hive 0.14.0 and later)
  [(col_name data_type [COMMENT col_comment], ... [constraint_specification])]
  [COMMENT table_comment]
  [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)]
  [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS]
  [SKEWED BY (col_name, col_name, ...)                  -- (Note: Available in Hive 0.10.0 and later)]
     ON ((col_value, col_value, ...), (col_value, col_value, ...), ...)
     [STORED AS DIRECTORIES]
  [
   [ROW FORMAT row_format] 
   [STORED AS file_format]
     | STORED BY 'storage.handler.class.name' [WITH SERDEPROPERTIES (...)]  -- (Note: Available in Hive 0.6.0 and later)
  ]
  [LOCATION hdfs_path]
  [TBLPROPERTIES (property_name=property_value, ...)]   -- (Note: Available in Hive 0.6.0 and later)
  [AS select_statement];   -- (Note: Available in Hive 0.5.0 and later; not supported for external tables)
 
CREATE [TEMPORARY] [EXTERNAL] TABLE [IF NOT EXISTS] [db_name.]table_name
  LIKE existing_table_or_view_name
  [LOCATION hdfs_path];
2)示例
hive> create table emp(id int,name string);
OK
Time taken: 0.238 seconds
3)查看表结构信息
--简化表结构
hive> desc emp;
OK
col_name	data_type	comment
id                  	int                 	                    
name                	string              	                    
Time taken: 0.105 seconds, Fetched: 2 row(s)
--可添加formatted查看更多信息
hive> desc formatted emp;
OK
col_name	data_type	comment
# col_name            	data_type           	comment             
	 	 
id                  	int                 	                    
name                	string              	                    
	 	 
# Detailed Table Information	 	 
Database:           	hive                	 
Owner:              	hadoop              	 
CreateTime:         	Wed Jun 20 17:56:05 CST 2018	 
LastAccessTime:     	UNKNOWN             	 
Retention:          	0                   	 
Location:           	hdfs://localhost:9000/user/hive/warehouse/hive.db/emp	 
Table Type:         	MANAGED_TABLE       	 
Table Parameters:	 	 
	COLUMN_STATS_ACCURATE	{\"BASIC_STATS\":\"true\"}
	numFiles            	0                   
	numRows             	0                   
	rawDataSize         	0                   
	totalSize           	0                   
	transient_lastDdlTime	1529488565          
	 	 
# Storage Information	 	 
SerDe Library:      	org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe	 
InputFormat:        	org.apache.hadoop.mapred.TextInputFormat	 
OutputFormat:       	org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat	 
Compressed:         	No                  	 
Num Buckets:        	-1                  	 
Bucket Columns:     	[]                  	 
Sort Columns:       	[]                  	 
Storage Desc Params:	 	 
	serialization.format	1                   
Time taken: 0.136 seconds, Fetched: 31 row(s)
4)管理表
上面创建的表是管理表,也称为内部表,因为这种表,Hive会(或多或少)控制着数据的生命周期,默认情况下,HIve会将这些表存储在由配置项hive.metastore.warehouse.dir所定义的目录的子目录下。
当删除管理表时,也会删除这个表中的数据。
5)外部表
使用关键字external创建,location子句用于指示Hive数据存放于哪个路径下,因为表是外部的,所以HIve并非完全拥有这份数据,因此,删除表并不会删除数据,只是删除了表的元数据信息。
2 删除表
hive> drop table if exists emp;
OK
Time taken: 4.664 seconds
3 修改表
大多数表属性可以通过alter table语句进行修改,这种操作会修改元数据,但不会修改数本身。
1)表重命名
hive> alter table emp rename to t_emp;
OK
Time taken: 0.654 seconds
2)增加列
hive> alter table t_emp add columns (sex string);
OK
Time taken: 0.938 seconds
hive> select *from t_emp;
OK
t_emp.id	t_emp.name	t_emp.sex
3	peter	NULL
1	alen	NULL
Time taken: 0.682 seconds, Fetched: 2 row(s)
3)修改列信息
用户可以对某个字段进行重命名、并修改其位置、类型或注释:
hive> alter table t_emp change column sex sex string
    > comment 'The column type changed by alen'
    > after id;
OK
Time taken: 0.502 seconds
hive> select *from t_emp;
OK
t_emp.id	t_emp.sex	t_emp.name
3	peter	NULL
1	alen	NULL
Time taken: 0.638 seconds, Fetched: 2 row(s)
注:该命令修改的只是元数据信息。
hive> alter table t_emp change column sex sex string after name;
OK
Time taken: 0.493 seconds
hive> select *from t_emp;
OK
t_emp.id	t_emp.name	t_emp.sex
3	peter	NULL
1	alen	NULL
Time taken: 0.572 seconds, Fetched: 2 row(s)
4)删除或替换列
hive> alter table t_emp replace columns(
    > eid int comment 'id of emp',
    > ename string comment 'name ');
OK
Time taken: 0.304 seconds
hive> select *from t_emp;
OK
t_emp.eid	t_emp.ename
3	peter
1	alen
Time taken: 0.845 seconds, Fetched: 2 row(s)
hive> alter table t_emp replace columns(
    > id int comment 'id of emp',
    > name string comment 'name of emp',
    > age int);
OK
Time taken: 0.325 seconds
hive> select *from t_emp;
OK
t_emp.id	t_emp.name	t_emp.age
3	peter	NULL
1	alen	NULL
Time taken: 0.551 seconds, Fetched: 2 row(s)
5)修改表属性
用户可以增加附加的表属性或修改已存在的属性,但无法删除属性。
hive> alter table emp set tblproperties(
    > 'notes'='Created by alen',
    > 'Cdate'='2018-06-21');
OK
Time taken: 0.344 seconds

猜你喜欢

转载自blog.csdn.net/alen_liu_sz/article/details/80754377