# 查询数据库
show databases;
# 查询表
show tables;
#使用数据库
use database_name ;
# 查看表结构
desc table_name;
创建表
创建表的格式
格式:
CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name
[(col_name data_type [COMMENT col_comment], ...)]
[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]
[ROW FORMAT row_format]
[STORED AS file_format]
[LOCATION hdfs_path]
# 字段解释:
[external] # 申明是否为外部表
[if not exists] # 如果表存在,则不创建了
[(col_name data_type [COMMENT col_comment], ...)] # 分别是 字段名,字段类型,字段注释
[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] # 在clustered by 里的字段上建立 num_buckets个桶,记录是由 sorted by里的字段排序的。
[ROW FORMAT row_format] # 指定分隔符,可以是以下几个:
: DELIMITED [FIELDS TERMINATED BY char]
[COLLECTION ITEMS TERMINATED BY char]
[MAP KEYS TERMINATED BY char]
[STORED AS file_format] # 指定load文件的类型,分为:
textfile 是 纯文本文件
sequence 是 压缩文件
[LOCATION hdfs_path] # 向表里加载数据,hdfs_path是一个hdfs上的目录,不能是文件,hive会依据默认配置的hdfs路径,自动将整个目录下的文件都加载到表中。
创建表的实例
创建一个简单的内部表
createtableifnotexists T1(
id int,
name string,
salary double comment 'comment是字段注释'
)comment 'comment是表注释'row format delimited
fields terminated by','
stored as textfile;
创建外部表
createexternaltableifnotexists T1(
id int,
name string,
salary double comment 'comment是字段注释'
)comment 'comment是表注释'row format
delimited fields terminated by','
stored as textfile;
使用复杂类型创建表
createexternaltableifnotexists T2(
id int,
course array<string>,
score map<string,int>
)
row format
delimited fields terminated by','
collection items terminated by'|'
map keys terminated by':'
stored as textfile;
# 数据文件内容
1001,语文|数学|英语,语文|56,语文:102|数学:2033|英语:30
1002,语文|数学|英语,语文|156,语文:120|数学:2033|英语:30
1003,语文|数学|英语,语文|1156,语文:210|数学:3320|英语:30
1004,语文|数学|英语,语文|1156,语文:2210|数学:203|英语:30
1005,语文|数学|英语,语文|5116,语文:22210|数学:230|英语:30
# 导入数据文件
load data local inpath '/home/datanode/hiveTest/test01' overwrite intotable t2;
创建一个带分区的内部表
createtableifnotexists T3(
id int,
name string
)
partitioned by (classid int)
row format
delimited fields terminated by','
stored as textfile;
创建一个带桶的内部表
createtable T4(
id int ,
name string,
sex string,
age int
)
partitioned by (city string)
clustered by(age) sorted by(name) into5 buckets
row format
delimited fields terminated by','
stored as textfile;