hive深入使用
Hive表的创建和数据类型
https://cwiki.apache.org/confluence/display/Hive/Home
管理表和外部的区别
# 管理表
1. 内部表也称之为MANAGED_TABLE;
2. 默认存储在/user/hive/warehouse下,也可以通过location指定;
3. 删除表时,会删除表数据以及元数据;
# 托管表(外部表)
1. 外部表称之为EXTERNAL_TABLE;
2. 在创建表时可以自己指定目录位置(LOCATION);
3. 删除表时,只会删除元数据不会删除表数据;
分区表创建及查询
分区表实际上就是对应一个HDFS文件系统上的独立的文件夹,该文件夹下是该分区所有的数据文件。Hive中的分区就是分目录,把一个大的数据集根据业务需要分割成较小的数据集。
# hdfs中目录结构:
/user/hive/warehouse/bf_log/
/20150911/
20150911.log
/20150912/
20150912.log
# 创建分区表
create EXTERNAL table IF NOT EXISTS default.emp_partition(
empno int,
ename string,
job string,
mgr int,
hiredate string,
sal double,
comm double,
deptno int
)
partitioned by (month string,day string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' ;
# 导入数据到分区表
load data local inpath '/opt/datas/emp.txt' into table default.emp_partition partition (month='201509',day='13') ;
# 查询
select * from emp_partition where month = '201509' and day = '13' ;
分区表需要注意的事项
创建普通表(不是分区表)后,直接把数据put到hdfs表目录
create table IF NOT EXISTS default.dept_nopart(
deptno int,
dname string,
loc string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
# 把数据直接上传到分区目录
dfs -put /opt/datas/dept.txt /user/hive/warehouse/dept_nopart ;
# 可以正常查询
select * from dept_nopart ;
创建分区表后,直接把数据put到hdfs表目录
create table IF NOT EXISTS default.dept_part(
deptno int,
dname string,
loc string
)
partitioned by (day string)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t';
## 第一种方式
dfs -mdir -p /user/hive/warehouse/dept_part/day=20150913 ;
dfs -put /opt/datas/dept.txt /user/hive/warehouse/dept_part/day=20150913 ;
## 修复分区
msck repair table dept_part ;
## 第二种方式
dfs -mkdir -p /user/hive/warehouse/dept_part/day=20150914 ;
dfs -put /opt/datas/dept.txt /user/hive/warehouse/dept_part/day=20150914 ;
## 手动添加分区
alter table dept_part add partition(day='20150914');
show partitions dept_part ;
加载数据到Hive表中常用的方式
1)加载本地文件到hive表
load data local inpath '/opt/datas/emp.txt' into table default.emp ;
2)加载hdfs文件到hive中(overwrite 覆盖掉原有文件)
load data inpath '/user/beifeng/hive/datas/emp.txt' overwrite into table default.emp ;
3)加载数据覆盖表中已有的数据(默认在原文件中追加)
load data inpath '/user/beifeng/hive/datas/emp.txt' into table default.emp ;
4)创建表时通过insert加载
create table default.emp_ci like emp ;
insert into table default.emp_ci select * from default.emp ;
5)创建表的时候通过location指定加载
create EXTERNAL table IF NOT EXISTS default.emp_ext(
empno int,
ename string
)
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
location '/user/beifeng/hive/warehouse/emp_ext';
Hive导出数据的几种方式
1)insert overwrite local directory 导出到本地
# 没有格式化
insert overwrite local directory '/opt/datas/hive_exp_emp'
select * from default.emp ;
# FORMAT格式化
insert overwrite local directory '/opt/datas/hive_exp_emp2'
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' COLLECTION ITEMS TERMINATED BY '\n'
select * from default.emp ;
2)通过管道流输出方式
bin/hive -e "select * from default.emp ;" > /opt/datas/exp_res.txt
3)导出到hdfs,再从hdfs -get
insert overwrite directory '/user/beifeng/hive/hive_exp_emp'
select * from default.emp ;
Hive常用的查询
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Select
Hive中 Export / Import
Export
导出,将Hive表中的数据,导出到外部。
EXPORT TABLE default.emp TO '/user/beifeng/hive/export/emp_exp' ;
export_target_path:指的是HDFS上路径
Import
导入,将外部数据导入Hive表中。
# 创建一个和default.emp结构一样的表
create table db_hive.emp like default.emp ;
import table db_hive.emp from '/user/beifeng/hive/export/emp_exp';
Hive分区相关
1)order by
# 对全局数据的一个排序,仅仅只有个reduce
select * from emp order by empno desc ;
2)sort by
# 对每一个reduce内部数据进行排序的,全局结果集来说不是排序
set mapreduce.job.reduces= 3;
insert overwrite local directory '/opt/datas/sortby-res' select * from emp sort by empno asc ;
3) distribute by
# 分区partition 类似于MapReduce中分区partition,对数据进行分区,结合sort by进行使用
insert overwrite local directory '/opt/datas/distby-res' select * from emp distribute by deptno sort by empno asc ;
# 注意事项:
distribute by 必须要在sort by 前面。
4)cluster by
# 当distribute by和sort by 字段相同时,可以使用cluster by ;
insert overwrite local directory '/opt/datas/cluster-res' select * from emp cluster by empno ;
Hive UDF自定义函数
pom.xml添加hive依赖(当然还需要hadoop相关)
<!-- Hive Client -->
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>${hive.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>${hive.version}</version>
</dependency>
Creating Custom UDFs
官方文档:https://cwiki.apache.org/confluence/display/Hive/HivePlugins
1) First, you need to create a new class that extends UDF, with one or more methods named evaluate.
package com.example.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
public final class Lower extends UDF {
public Text evaluate(final Text s) {
if (s == null) {
return null;
}
return new Text(s.toString().toLowerCase());
}
}
2)After compiling your code to a jar, you need to add this to the Hive classpath. See the section below on deploying jars.
3) 两种使用方式
# 把jar包添加到hive的环境变量中
add jar /opt/datas/hiveudf.jar
# 创建一个function
create temporary function my_lower as "com.beifeng.senior.hive.udf.LowerUDF" ;
# 使用
select ename, my_lower(ename) lowername from emp limit 5 ;
CREATE FUNCTION self_lower AS 'com.beifeng.senior.hive.udf.LowerUDF' USING JAR 'hdfs://hadoop-senior.ibeifeng.com:8020/user/beifeng/hive/jars/hiveudf.jar';
select ename, self_lower(ename) lowername from emp limit 5 ;
hive深入使用的更多相关文章
- 初识Hadoop、Hive
2016.10.13 20:28 很久没有写随笔了,自打小宝出生后就没有写过新的文章.数次来到博客园,想开始新的学习历程,总是被各种琐事中断.一方面确实是最近的项目工作比较忙,各个集群频繁地上线加多版 ...
- Hive安装配置指北(含Hive Metastore详解)
个人主页: http://www.linbingdong.com 本文介绍Hive安装配置的整个过程,包括MySQL.Hive及Metastore的安装配置,并分析了Metastore三种配置方式的区 ...
- Hive on Spark安装配置详解(都是坑啊)
个人主页:http://www.linbingdong.com 简书地址:http://www.jianshu.com/p/a7f75b868568 简介 本文主要记录如何安装配置Hive on Sp ...
- HIVE教程
完整PDF下载:<HIVE简明教程> 前言 Hive是对于数据仓库进行管理和分析的工具.但是不要被“数据仓库”这个词所吓倒,数据仓库是很复杂的东西,但是如果你会SQL,就会发现Hive是那 ...
- 基于Ubuntu Hadoop的群集搭建Hive
Hive是Hadoop生态中的一个重要组成部分,主要用于数据仓库.前面的文章中我们已经搭建好了Hadoop的群集,下面我们在这个群集上再搭建Hive的群集. 1.安装MySQL 1.1安装MySQL ...
- hive
Hive Documentation https://cwiki.apache.org/confluence/display/Hive/Home 2016-12-22 14:52:41 ANTLR ...
- 深入浅出数据仓库中SQL性能优化之Hive篇
转自:http://www.csdn.net/article/2015-01-13/2823530 一个Hive查询生成多个Map Reduce Job,一个Map Reduce Job又有Map,R ...
- Hive读取外表数据时跳过文件行首和行尾
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 有时候用hive读取外表数据时,比如csv这种类型的,需要跳过行首或者行尾一些和数据无关的或者自 ...
- Hive索引功能测试
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 从Hive的官方wiki来看,Hive0.7以后增加了一个对表建立index的功能,想试下性能是 ...
- 轻量级OLAP(二):Hive + Elasticsearch
1. 引言 在做OLAP数据分析时,常常会遇到过滤分析需求,比如:除去只有性别.常驻地标签的用户,计算广告媒体上的覆盖UV.OLAP解决方案Kylin不支持复杂数据类型(array.struct.ma ...
随机推荐
- python开发微信支付学习记录(转)
前言 微信支付是由微信及财付通联合推出的移动支付创新产品.如今,随着微信支付的全面开放,相关需求也越来越多,很多开发人员进行微信支付开发及商家申请微信支付时,面临着诸多疑惑. 要想开发顺利进行,首先要 ...
- 【Hive】Hive 基础
Hive架构: Hive基础 1 概念 1.1 简介 1.1.1 hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表, 并提供简单的sql查询功能,可以将sql语句 ...
- Android——Activity的生命周期
一,Demo測试Activity的生命周期 写两个Activity: package com.example.activity_04; import android.os.Bundle; import ...
- 在MyEclipse上安装GIT插件EGit
MyEclipse2016 CI 下载地址:http://pan.baidu.com/s/1gfBw9Ab 1.“Help”->"Install from Site" 2.在 ...
- NYOJ82 迷宫寻宝(一)【BFS】
迷宫寻宝(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描写叙述 一个叫ACM的寻宝者找到了一个藏宝图.它依据藏宝图找到了一个迷宫,这是一个非常特别的迷宫,迷宫里有N个 ...
- 系统封装 如何打造原生WINPE
1 安装微软的AIK(Windows Automated Installation Kit,Windows自动安装工具包),AIK简体中文版下载地址: http://download.microsof ...
- OSQL.EXE 命令行下脱裤mssql
cd C:\Program Files\Microsoft SQL Server\100\Tools\Binn\ OSQL.EXE -S "localhost" -U " ...
- Oracle基础(二) 创建数据库实例
Oracle中数据库也称为数据库实例,在Oracle在安装过程中可以选择创建数据库,也可以再安装之后再进行创建. 一般创建数据库可以使用图形界面方式和SQL指令的方式,由于SQL指令方式较为复杂,这里 ...
- svn解决与优化帮助
1.问题的出现 解决方案: 最后一行不能子目录. 启动的时候也是要根目录的.svnserve -d -r /home/svn/repos [不能是svnserve -d -r /home/svn/re ...
- 测试代码覆盖率工具学习(Android Emma)
博客分类: 工具分享 eclipseeclemmaemmatestng 关于eclemma的历史和怎么安装,请参考http://www.ibm.com/developerworks/cn/ ...