hadoop(十) - hive安装与自定义函数

来源:互联网 发布:上海cnc编程招聘 编辑:程序博客网 时间:2024/05/05 11:55

一. Hive安装

Hive只在一个节点上安装即可

1. 上传tar包
2. 解压 tar -zxvf hive-0.9.0.tar.gz -C /cloud/
3. 配置mysql metastore(切换到root用户)

    3.1 配置HIVE_HOME环境变量

    3.2 安装mysql

    查询以前安装的mysql相关包: rpm -qa | grep mysql
    暴力删除这个包: rpm -e mysql-libs-5.1.66-2.el6_3.i686 --nodeps
    安装mysql: rpm -ivh MySQL-server-5.1.73-1.glibc23.i386.rpm 
                      rpm -ivh MySQL-client-5.1.73-1.glibc23.i386.rpm 

    执行命令设置mysql: /usr/bin/mysql_secure_installation (注意:删除匿名用户,允许用户远程连接)

    (如果修改失败,运行此命令:/etc/init.d/mysql start)

    登陆mysql: mysql -u root -p

4. 配置hive
cp hive-default.xml.template hive-site.xml 
修改hive-site.xml(删除所有内容,只留一个<property></property>)

添加如下内容:

<property>  <name>javax.jdo.option.ConnectionURL</name>  <value>jdbc:mysql://hadoop00:3306/hive?createDatabaseIfNotExist=true</value>  <description>JDBC connect string for a JDBC metastore</description></property><property>  <name>javax.jdo.option.ConnectionDriverName</name>  <value>com.mysql.jdbc.Driver</value>  <description>Driver class name for a JDBC metastore</description></property><property>  <name>javax.jdo.option.ConnectionUserName</name>  <value>root</value>  <description>username to use against metastore database</description></property><property>  <name>javax.jdo.option.ConnectionPassword</name>  <value>123</value>  <description>password to use against metastore database</description></property>

5. 安装hive和mysq完成后,将mysql的连接jar包拷贝到$HIVE_HOME/lib目录下

如果出现没有权限的问题,在mysql授权(在安装mysql的机器上执行)
mysql -uroot -p
#(执行下面的语句  *.*:所有库下的所有表   %:任何IP地址或主机都可以连接)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123' WITH GRANT OPTION;
FLUSH PRIVILEGES;

6. 建表(默认是内部表)
create table trade_detail(id bigint, account string, income double, expenses double, time string) row format delimited fields terminated by '\t';

建分区表:

create table td_part(id bigint, account string, income double, expenses double, time string) partitioned by (logdate string) row format delimited fields terminated by '\t';

建外部表: (外部表的数据在指定位置,不在Hive的数据仓库中,只是在Hive元数据库中注册)

create external table td_ext(id bigint, account string, income double, expenses double, time string) row format delimited fields terminated by '\t' location '/td_ext';

7. 创建分区表

普通表和分区表区别:有大量数据增加的需要建分区表

分区是表的部分列的集合,可以为频繁使用的数据建立分区,这样查找分区中的数据时就不需要扫描全表,对于提高查找效率很有帮助。

create table book (id bigint, name string) partitioned by (pubdate string) row format delimited fields terminated by '\t'; 

分区表加载数据:

load data local inpath './book.txt' overwrite into table book partition (pubdate='2010-08-22');

load data local inpath '/root/data.am' into table beauty partition (nation="USA");

select nation, avg(size) from beauties group by nation order by avg(size);

二. HiveQL

1. HiveQL的数据类型:

Hive支持基本类型和复杂类型,基本类型主要有数值型、布尔型和字符串;复杂类型有三种:array、map和struct



2. HiveQL常用操作:

2.1 建立三张表:

create table userinfo (id int, name string) row format delimited fields terminated by '\t';create table choice (userid int, classname string) row format delimited fields terminated by '\t';create table classinfo (teacher string, classname string) row format delimited fields terminated by '\t';
“row format delimited fields terminated by” 是HiveQL特有的,用来指定数据的分隔方式。


2.2 导入数据

load data local inpath '/usr/resources/1.txt' overwrite into table userinfo;load data local inpath '/usr/resources/2.txt' overwrite into table choice;load data local inpath '/usr/resources/3.txt' overwrite into table classinfo;
如果导入的数据在HDFS上,则不需要local关键字。内部表导入的数据文件可在数据仓库目录看到:

hadoop fs -ls /user/hive/warehouse


2.3 建立分区表

create table ptest(userid int) partitioned by (name string) row format delimited fields terminated by '\t';


2.4 桶

可以把表或分区组织成桶,桶是按照分开组织特定字段,每个桶对应一个reduce操作,在建立桶之前,需要设置hive.enforce.bucketing属性为true,使hive能够识别桶

set hive.enforce.bucketing = true;
create table btest2 (id int, name string) clustered by (id) into 3 buckets row format delimited fields terminated by '\t';
insert overwrite table btest2 select * from userinfo;

hive使用对分桶所用的值进行hash,并用hash结果除以桶的个数做取余运算来分桶,保证每个桶中都有数据,但每个桶中的数据条数不一定相等。

分桶可以获得比分区更高的查询效率,同时分桶也便于对全部数据进行采样处理。

取样操作:select * from btest2 tablesample(bucket 1 out of 3 on id);


2.5 多表插入

多表插入指的是在同一条语句中,把读取的同一份元数据插入到不同的表中,只需要扫描一遍元数据即可完成所有表的插入操作,效率很高。

create table mutil1 as select id, name from userinfo;
create table mutil2 like mutil1;
from userinfo insert overwrite table mutil1 select id, name insert overwrite table mutil2 select count(distinct id), name group by name;


2.6 修改表

重命名表名、增加数据列的操作:

alter table mutil1 rename to mutil11;
alter table mutil11 add columns (grade string);
describe mutil11;


2.7 删除表

drop table mutil11;

对于内部表,drop操作会把元数据和数据文件删除掉,对于外部表只是删除元数据,如果只是删除表中的数据,保留表明可以在HDFS上删除数据文件。


2.8 连接

内连接:select userinfo.*, choice.* from userinfo join choice on (userinfo.id = choice.userid);

左外连接:select userinfo.*, choice.* from userinfo left outer join choice on (userinfo.id = choice.userid);

右外连接:select userinfo.*, choice.* from userinfo right outer join choice on (userinfo.id = choice.userid);

半连接:select userinfo.* from userinfo left semi join choice on (userinfo.id = choice.userid); 

半连接是hive所特有的,hive不支持in操作,半连接为替代方案,注意连接的表choice不能出现在查询的列中,只能出现在on子句中。


2.9 子查询

查询教课最多的老师 (hive只能在from引导的子句中出现子查询)

select teacher, max(class_num) from (select teacher, count(classname) as class_num from classinfo group by teacher) subq group by teacher;


2.10 创建视图

create view teacher_classnum as select teacher, count(classname) from classinfo group by teacher;

三. UDF

自定义UDF要继承org.apache.hadoop.hive.ql.exec.UDF类实现evaluate 

public class AreaUDF extends UDF{private static Map<Integer, String> areaMap = new HashMap<Integer, String>();static {areaMap.put(1, "北京");areaMap.put(2, "上海");areaMap.put(3, "广州");}public Text evaluate(Text in){String result = areaMap.get(Integer.parseInt(in.toString()));if(result == null){result = "其他";}return new Text(result);}}

自定义函数调用过程:
1. 添加jar包(在hive命令行里面执行)
hive> add jar /root/NUDF.jar;

2. 创建临时函数
hive> create temporary function getNation as 'cn.itcast.hive.udf.NationUDF';

3. 调用
hive> select id, name, getNation(nation) from beauty;

4. 将查询结果保存到HDFS中
hive> create table result row format delimited fields terminated by '\t' as select * from beauty order by id desc;
hive> select id, getAreaName(id) as name from tel_rec;
create table result row format delimited fields terminated by '\t' as select id, getNation(nation) from beauties;

1 0
原创粉丝点击