hive深入使用

摘要:
配置单元表的创建和数据类型https://cwiki.apache.org/confluence/display/Hive/Home管理表和外部表之间的区别#管理表1.内部表也称为MANAGED_TABLE2.默认存储在/user/hhive/warehouse中,也可以通过位置指定;3.删除表时,表数据和元数据将被删除#托管表(外部表)1.外部表称为external_table二

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 '	' ;

# 导入数据到分区表
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 '	';

# 把数据直接上传到分区目录
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 '	';

## 第一种方式
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 '	'
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 '	' COLLECTION ITEMS TERMINATED BY '
'
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深入使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇项目中eCharts中的tree配置Mac下Pycharm中升级pip失败,通过终端升级pip下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

spring读取外部配置文件

在开发中我们经常有些值是在不确定的,需要运行时才赋确定的值(比如数据库连接ip地址,开发时可能用本地数据库测试,上线时就改用服务器数据库 ),还有如果在每个地方都硬编码写死了,之后要改就很麻烦。所以我们希望可以将这些值写到外部配置文件,如果要进行修改就直接修改配置文件而不用动源码。 使用@PropertySource注解和Environment @Prop...

jni中关于dll的装载问题[转]

   通常我们在写大型项目时,也就是集成的项目时,单独用JAVA语言去完成所有的事情往往效果不佳,也不能很好的完成,例如:我们要去调硬件,获取电脑的运行状况等等(如果用JAVA语言实现时,往往耗时),基于此,我们就要寻求一种很好的解决方案,那就是利用别的语言的长处了,如:C++(它在对底层的调用和硬件方面确实够强悍)。如果我们能用C++实现对硬件的所有操作...

eclipse+maven+ssm框架搭建

eclipse+maven+ssm框架 0、系统环境 1)Windows 10 企业版 2)JDK 1.8.0_131 3)Eclipse Java EE IDE for Web Developers  Version: Neon.3 Release (4.6.3) 4)Tomcat 8.5 1、maven下载及配置 maven的下载地址:http:...

判断SQLSERVER数据库表字段为空的问题

今天在用遍历dataset所有记录时(把dataset中的字段值读出并赋给各TextBox的Text属性), 读取方法:TextBox3.Text=Convert.ToString(dataSet11.msg_info[a].msg_receiver),a是一变量,利用button_click触发其自增, 用以上方法当读取到一条类型为datatime的空字...

unity 在移动平台中,文件操作路径详解

今天,这篇文章其实是个老生常谈的问题咯,在网上类似的文章也比比皆是,在此我只是做个详细总结方便大家能够更好、更快的掌握,当然,如有不足的地方 欢迎指正!!!相信大家在开发过程中,难免会保存一些文件在客户端进行本地化操作。如:配置文件,状态文件,Assetbundle文件等等...最近总有人问我:1.保存了一个xml在客户端,能读取里面的数据,可是不能修改,...

Log4net源码View之Logger解析

  1.简介 Logger是Log4net的三大核心载体之一,搞清楚它的意义很重大。另外两个分别是Appender和Layout。其对应关系为一个Logger对应多个Appender,一个Appender对应一个Layout。 对于Log4net的内部实现,ILogger才是接口(所有日志需要实现的——Interface that all logger...