Hive部分
退出cli:ctrl+C
查看hdfs:dfs -ls /
查看本地:! ls /
查看历史命令:cd /root 或者 cd /home/data cat .hivehistory
强制删除数据库:drop databases db_hive cascade;
建表语句:
create [external] table [if not exists] table_name
(col type comment ‘’)
comment ‘’
partitioned by (col type comment ‘’,…)
[clustered by ()]
[sorted by (col ASC|DESC)] into num buckets
row format row_format
stored as ORC
location ‘’
;
管理表与外部表
内部表hive控制着数据的生命周期,表数据默认存储在hive.metastore.warehouse.dir中
hive认为其并非完全拥有这份数据,删除表只会删除元数据信息
分区表
分区表实际对应的是HDFS上的独立文件夹,该文件夹下是该分区的所有数据文件
增加分区:alter table student add if not exists partition(pt_d=’’,pt_h=’’) partition(pt_d=’’,pt_h=’’)
删除分区:alter table student drop if exists partition(pt_d=’’,pt_h=’’),partition(pt_d=’’,pt_h=’’)
查看分区:show partitions biads.ads
查看表结构:desc formatted biads.ads
清空表:truncate table student;
表的创建方式:
1.普通建表:CT
2.查询建表:CTAS create table if not eixsts student2 as select id ,name from student;
3.根据已存在的表结构建表:CTLIKE create table if not exists student4 like student;
数据导入:
1.load导入 load data [local] inpath [overwrite] into table student partition(pt_d=’’,pt_h=’’);
2.inset导入 inset into student values (),();
inset into student partition(pt_d=’’,pt_h=’’) select * from student2;
3.import导入
数据导出:
1.inset导出 inset overwrite [local] directory ’ ’ select * from student;
2.hdfs导出 hadoop fs -get
3.shell导出 beeline -e ’ select * from student’ >/opt/student.txt
4.export导出 export table student to ’ ’
5.sqoop导出
查询语句:
select [*|distinct] col1,col2…
from student
where pt_d=’ ’
group by col1,col2
order by col1,col2
cluster by col_list
distribute by col_list sort by col_list
limit number
;
Like与Rlike的区别
%代表任意,_代表单个字符 Rlike是Like的扩展
Having与where的区别
where针对表中列,having针对结果,having只用与group by 分组统计语句
表连接:
连接n个表,至少需要n-1个条件
大多数情况下,Hive会对每对join对象启动一个MapReduce任务,Hive总是按照从左到右的顺序执行的
笛卡尔积
省略连接条件,连接条件无效
排序
order by:全局排序一个MapReduce
Sort by:每个MapReduce内部排序
distribute by:分区排序类似partition,结合sort by使用
cluster by:特殊的distribute by
抽样(略)
函数:
查看系统函数:show functions
显示系统函数:desc function extended max;
自定义函数
1.UDF 一进一出
2.UDAF 多进一出
3.UDTF 一进多出
编程步骤:
1.继承 org.apache.hadoop.hive.ql.udf
2.实现evalute方法
3.hive_cli创建函数 添加jar add jar test.jar create [temporary] function biads.test as test;
注意事项:UDF必须要有返回类型,可以返回null,但是返回类型不能是void
压缩(略)
文件存储格式
主要有textfile,sequencefile,orc,parquet
行式存储与列式存储
表优化(略)