数据库相关linux
#/user/hive/warehouse/db_name.db create database if not exists db_name #指定HDFS目录 做为hive的数据库目录 create database if not exists db_name location '/t' #删除空数据库 drop database db_name #删除非空的数据库 drop database db_name cascade; #显示全部数据库 show databases #应用数据库 use db_name
#查看表的信息 desc table_name describe table_name #查看表中更为详细的信息 desc extended table_name #格式化查看表中更为详细的信息 desc formatted table_name
管理表 ()sql
基本语法数据库
create table if not exists table_name( id int, name string )row format delimited fields termimated by '\t';
基于as子查询的建表方式函数
#1. 之后面子查询的查询列,做为表的结构
#2. 同时会把查询结果的数据,插入新表中
create table if not exists table_name as select id from t_user;
基于like关键字建表优化
#复制t_user的表结构,但不复制数据
create table if not exists table_name like t_user;
指定表格位置[技巧]spa
#默认建表的位置 在当前数据库中
#/user/hive/warehouse/baizhi_140.db/t_user
#/user/hive/warehouse/baizhi_140.db/t_user_as
#/user/hive/warehouse/baizhi_140.db/t_user_like
#指定表格的建立位置 location
create table if not exists table_name( id int, name string )row format delimited fields termimated by '\t' location '/test1';
#存在目录 套上一张表 [重点]
create table if not exists t_user_suns( id int, name string )row format delimited fields terminated by '\t' location '/suns';
注意[技巧]code
hdfs上同一个目录下的全部文件,hive表统一操做处理
外部表orm
#基本建立语法
create external table if not exists table_name( id int, name string )row format delimited fields termimated by '\t';
#子查询方式 create external table if not exists table_name as select id from t_user;
#like建立方式 create external table if not exists table_name like t_user;
管理表 与 外部表的区别blog
#删除管理表 ,HDFS上的目录一样删除 drop table t_user_as; #删除外部表,HDFS上的目录及数据保留,删除了metastore drop table t_user_ex;排序
3.分区表 (优化)
create table t_user_part( id int, name string) partitioned by (time string) row format delimited fields terminated by '\t'; load data local inpath '/root/data3' into table t_user_part partition (time='18'); load data local inpath '/root/data3' into table t_user_part partition (time='19'); select * from t_user_part #操做具体分区 where 分区条件 select count(*) from t_user_part where time='18' and id >3 ;
桶表 (抽样,了解)
临时表(
数据的导入 import
load data local[重点]
#linux系统中向hdfs中的hive导入数据 load data local inpath '' into table table_name
load data
#从hdfs向hive导入数据 load data inpath 'hdfs_path' into table table_name #本质上就是把某一个目录下的文件 移动到 新表的目录中 load data inpath '/suns/data3' into table t_user_hdfs
建立表的过程当中 经过as
insert 关键字 导入数据[重点]
# 与 as 经过子查询导入数据 关键区别在于经过insert操做,表已经存在. insert into table t_user_2 select id,name from t_user;
经过hdfs put文件
bin/hdfs dfs -put /root/data3 /user/hive/warsehouse/baizhi_140/t_user
数据的导出
insert方式【】
insert overwrite local directory '/root/xiaohei' select name from t_user; #底层应用mr,那么全部xiaohei目录不能存在,自动生成文件名 000000_0 insert overwrite directory 'hdfs_path' select name from t_user;
Hive导入 导出命令【】
经过hdfs get文件
bin/hdfs dfs -get /user/hive/warsehouse/baizhi_140/t_user /root
经过hive的启动参数导出数据
bin/hive --database 'baizhi125' -f /root/hive.sql > /root/result
1. 基本sql select * from t_user #不启动mr select id,name from t_user #启动mr 2. 条件查询 select * from t_user where name='sss'; 3. 谓词 between and in not in is null is not null select * from t_user where id between 1 and 3 select * from t_user where id in (1,2) select * from t_user where id is null 4. 比较运算 > < >= <= != 5. 逻辑运算 and or 6. 排序 order by desc|asc 7. 分页 limit select * from t_user limit 2; 8. sql函数 show functions; select substring(name,1,1) from t_user select upper(name) from t_user 9. 多表联合查询 inner join left outer join right outer join full join select e.id,e.name,d.id,d.dname from t_emp e inner join t_dept d on e.d_id = d.id; 10. count avg sum max min