一、数据库操作
create database database_name //创建数据库 drop database database_name //删除数据库 user database_name //使用数据库 describe database database_name //查看数据库详细信息
二、表操作
创建表
create [external] table table_name [(col_name data_type [comment col comment],...)] [comment table_comment] [partitioned by (col_name data_type,...)] [clustered by (col_name,...) sorted by (col_name [ASC|DESC],...) into num_buckets buckets] [row format row_format] [field terminated by] [stored as file_format] [location hdfs_path]
external 指定创建外部表
clustered by 指定分桶
row format 指定列分割信息,ROW FORMAT DELIMITED 代表一行是一条记录
field terminated by 指定列分隔符
stored as 指定数据存储文件格式,文件数据是纯文本,使用 STORED AS TEXTFILE。如果数据需要压缩,使用STORED AS SEQUENCEFILE
三、导入数据
1、从本地导入数据【复制操作】
load data local inpath '/filename_path' [overwrite] into table table_name
2、从hdfs导入数据【移动操作】
load data inpath 'hdfs://filename_path' [overwrite] intp table table_name hadoop fs -put filename_path /user/hive/warehouse/tb_order
[ /user/hive/warehouse/该路径为hive数据库在hdfs上的存储路径具体参照配置]
3、从已有表导入数据
into [overwrite] table mytable [patition()] select * from from_table;
4、创建表的时候导入其他表的数据
create table [if not exists] table_name as select * from from_table;
四、导出数据
1、导出到本地文件系统
insert [overwrite] local directory 'path' select * from hive_table
2、导出到hdfs
insert [overwrite] directory "path" select * from hive_table
3、导出到另一张hive表
insert into to_table_name select * from from_table_name
五、外部表
已经在hdfs存在的文件,不需要挪动到hive的默认路径就可以创建外部表,外部表删除的时候不会删除hdfs上的文件只会删除表结构
创建外部表
create external table (id int,name string) row format delimited fields terminated by '\t' stored as textfile location '/path'
六、分区表
创建分区表可以优化数据统计
create table (id int,name string) partitioned by (time string) row format delimited fields terminated by '\t' stored as textfile 上传本地数据到指定分区 load data local inpath 'path' [overwrite] into table table_name partition (time='20210330')
七、分桶表
#设置变量,设置分桶为true, 设置reduce数量是分桶的数量个数
set hive.enforce.bucketing = true;
set mapreduce.job.reduces=4;
create table (id int,name string) partitioned by (time string) clustered by (id) sorted by (name) into 2 buckets row format delimited fields terminated by '\t' stored as textfile
注意: OVERWRITE关键字;目标表(或者分区)中的内容会被删除,然后再将 filepath 指向的文件/目录中的内容添加到表/分区中
八、表的修改
1、增加分区 alter table table_name add partiton(time='20210330'),.. 2、删除分区 alter table table_name drop partiton(time='20210330'),.. 3、表重命名 alter table table_name rename to new_table_name 4、增加列 alter table table_name add columns (version string,..)//新增字段在所有列后,在partiton列前 5、替换所有列 alter table table_name replace columns (id int,name string)//之前的列全被替换成了新的字段列
show partitons table_name //查看表分区
更新中。。。