修改表的操作
-
增加分区
第一步手动创建新增分区的目录 第二步把该分区目录加载到hive表信息中 ALTER TABLE t_user_p ADD PARTITION (guojia='riben') location '/user/hive/warehouse/itcast.db/t_user_p/guojia=riben';
-
hive中错误的分类
Error while compiling statement 编译期间的错误 sql语法错误 Error while processing statement 执行期间的错误 逻辑思维的错误 sql语法层面没问题
-
日常常见的使用命令
show tables; 显示当前数据库所有表 show databases |schemas; 显示所有数据库 show partitions table_name; 显示表分区信息,不是分区表执行报错 desc formatted table_name; 查看表信息(格式化美观)
DML
-
load加载数据
-
是hive官方推荐的操作 尽量不要使用hadoop fs -put
-
本质是把文件从本地文件系统或者hdfs文件系统 移动到hive表对应的hdfs目录下去。
create table stu_local(Sno int,Sname string,Sex string,Sage int,Sdept string) row format delimited fields terminated by ','; create table stu_no_local(Sno int,Sname string,Sex string,Sage int,Sdept string) row format delimited fields terminated by ','; --load加载数据 load data local inpath '/root/hivedata/students.txt' into table stu_local; --Loading data to table itcast.stu_local from file:/root/hivedata/students.txt --从本地加载的时候 实质是复制操作 相当于hadoop fs -put load data inpath '/stu/students.txt' into table stu_no_local; --Loading data to table itcast.stu_no_local from hdfs://node-1:8020/stu/students.txt --从hdfs加载的时候 实质是移动操作 相当于 hadoop fs -mv
-
-
insert
-
在hive 中 ,insert+select使用 插入表的数据来自于后面的查询语句返回的结果
-
注意:保证查询返回的结果字段顺序 类型 个数和待插入表一致 如果不一致 hive尝试换行 但是不一定成功
如果成功就显示数据 如果不成功 就显示null
-
多重插入:一次扫描 多次插入
from source_table insert overwrite table test_insert1 select id insert overwrite table test_insert2 select name;
-
-
多分区表概念
-
多分区指的是在前一个分区基础上继续分区 是一个递进的关系
-
当下常见的是两个分区
-
建表
create table t_user_double_p(id int,name string, country string) partitioned by (guojia string, sheng string) row format delimited fields terminated by ',';
-
加载数据
load data local inpath '/root/hivedata/china.txt' into table t_user_double_p partition(guojia='zhongguo', sheng='beijing'); load data local inpath '/root/hivedata/china.txt' into table t_user_double_p partition(guojia='zhongguo', sheng='shanghai'); load data local inpath '/root/hivedata/usa.txt' into table t_user_double_p partition(guojia='meiguo', sheng='niuyue'); select * from t_user_double_p where guojia="zhongguo" and sheng="beijing";
-
-
动态分区
-
概念:分区字段的值是如何确定的
- 如果是在加载数据的时候 手动写死指定的 这叫做静态分区
- 如果是在插入的时候通过查询来动态确定分区的值 叫做动态分区
-
动态分区的操作
-
首先开启动态分区的功能
set hive.exec.dynamic.partition=true;
-
指定动态分区的模式
strict 严格模式 nonstrict 非严格模式 set hive.exec.dynamic.partition.mode=nonstrict;
-
动态分区数据的 插入
insert overwrite table d_p_t partition (month,day) select ip,substr(day,1,7) as month,day from dynamic_partition_table;
在上述表的插入过程中,查询返回了3个字段,其中第一个字段作为表的真实字段插入,后面两个查询返回的字段将决定该数据的分区值。分区值不在是我们手动写死指定的 所以叫动态分区。
-
-
-
数据导出操作
-
把select查询语句的结果导出到文件系统中(local hive服务器本地文件系统 否则就是hdfs文件系统)
-
注意:overwrite操作 是一个覆盖操作
insert overwrite local directory '/root/123123' select * from student; insert overwrite directory '/aaa/test' select * from stu_tmp;
-
-
select查询操作
-
cluster by 分桶查询
- 就是把数据按照指定的字段分成若干个部分
- 可以根据指定的字段分开 并且根据该字段默认排序 正序
--没有指定分为几桶 select * from student cluster by(Sno); --根据输入的数据量来自动评估 INFO : Number of reduce tasks not specified. Estimated from input data size: 1 --指定分桶的个数 set mapreduce.job.reduces=2; Number of reduce tasks not specified. Defaulting to jobconf value of: 2 Number of reduce tasks not specified. Defaulting to jobconf value of: 3
-
需求:根据一个字段分 根据另一个字段排序
select * from student cluster by(Sno) sort by(sage desc); --错误 --Cannot have both CLUSTER BY and SORT BY clauses select * from student DISTRIBUTE by(Sno) sort by(sage desc);--正确 --DISTRIBUTE by负责分 sort by负责排序 这两个字段可以不一样 --如果字段一样的话 cluster by = DISTRIBUTE by + sort by
-
order by全局排序
- 不管当前系统中设置多少个reducetask 最终编译决定一个 因为只有一个才能全局排序的需求
- order by和sort by order负责全局排序 sort by负责分桶内排序
-
-
hive中join
- inner join内关联: 只显示关联上的左右两边的数据
- left join 左关联: 以左表为准 显示左表所有数据 右表与之关联 关联上显示结果 关联不上显示null
- right join 右关联: 以右表为准 显示右表所有数据 左表与之关联 关联上显示结果 关联不上显示null
- outer join 外关联:显示所有的结果
- left semi join :内关联只显示左表的数据
- cross join 交叉关联 笛卡尔积 慎用
-
hive shell命令行
-
充当第一代客户端 bin/hive 连接metastore服务 访问hive
-
在启动的时候 添加相关参数 执行hive sql
bin/hive -e "xzxzx" 启动执行后面的sql语句 执行完退出 bin/hive -f xxxx.sql 启动执行后面的sql脚本 执行完结束 上述两种hive的操作方式 是生产环境中常见的调度方式。
-
如何开启hive服务启动的时候的日志
--没有开启日志的启动方式 /export/servers/hive/bin/hive --service metastore --开启日志查看启动过程 /export/servers/hive/bin/hive --service metastore --hiveconf hive.root.logger=DEBUG,console /export/servers/hive/bin/hive --service metastore --hiveconf hive.root.logger=DEBUG,console
-
-
hive 参数配置方式
-
配置方式
conf/hive-site.xml 全局有效 影响该安装包任何启动方式 --hiveconf k1=v1 启动进程有效 谁启动谁设置谁生效 set k1=v1 连接会话session有效 谁连接谁设置谁生效 session结束 失效
-
优先级
set > hiveconf >hive-site.xml >hive-default.xml
-
hive作为基于hadoop的数仓软件 也会把hadoop配置文件加载到自己的工作中
-
hive 函数
-
内置函数
-
自定义函数
-
自定义函数的分类
- UDF 用户定义的普通函数 描述了输入一行输出一行 比如:substring year
- UDAF 用户定义聚合函数 描述了输入多行 输出一行 比如:count sum max mix avg
- UDTF 用户定义表生成函数 描述了输入一行输出多行
-
自定义普通函数的开发流程
-
继承UDF类 重载evaluate方法(自定义函数的逻辑就是该方法的逻辑)
-
打成jar
-
把jar添加到hive的classpath中
add jar /root/hivedata/asdasdasd.jar
-
注册自定义函数 把jar和函数名称进行绑定
create temporary function 函数名 as '类的全路径';
-
-
-
UDTF函数–explode
- T:表生成函数 可以输入一行 输出多行
- explode
- 如果explode(array) :会把array中每一个元素变成一行
- 如果explode(map):会把kv对变成一行 其中k一列 v 一列
-
lateral view 侧视图
-
该语法是使用场景就是为了配合UDTF函数的使用的
-
UDTF函数可以把一行变成多行 相当于产生了一个新的表 但是是虚拟的表 跟原来的表没有关联
-
如果想在一个select语法中 既要查询原来表的数据 又要查询虚拟表的数据 就是需要使用lateral view语法把真实的表和虚拟的表进行关联
真实表名 lateral view UDTFs(xxxx) 侧视图名(虚拟表名) as 字段1,字段2
-
-
hive类型转换函数
ast(列 as string) select cast(12 as double) from dual;
-
行列转换的使用
-
多行转单列
a)concat_ws(参数1,参数2),用于进行字符的拼接 参数1—指定分隔符 参数2—拼接的内容 b)collect_set(col3),它的主要作用是将某字段的值进行去重汇总,产生array类型字段 如果不想去重可用collect_list()
-
单列转多行
explode函数和lateral view侧视图 注意:explode只接受array 或者map类型 如果字段不是该类型 想法设法转换成array类型 select explode(split(col3,",")) from col2row_21;
-
-
reflect反射函数
- 可以调用java中任何一个已经存在的函数