Hive调研

Hive是什么

Hive是基于Hadoop构建的一套数据仓库分析系统,它提供了丰富的SQL查询方式来分析存储在Hadoop 分布式文件系统中的数据。Hive可以将结构化的数据存储在数据仓库中,通过自己的SQL去查询分析需要的内容,这套SQL简称Hive SQL。它与关系型数据库的SQL略有不同,但支持了绝大多数的语句如DDL、DML 以及常见的聚合函数、连接查询、条件查询。

Hive在Hadoop的架构体系中承担了一个SQL解析的过程,它提供了对外的入口来获取用户的指令然后对指令进行分析,解析出一个MapReduce程序组成可执行计划,并按照该计划生成对应的MapReduce任务提交给Hadoop集群处理,获取最终的结果。

Hive架构

1、  操作界面:CLI,Web,Thrift

2、  driver:hive系统将用户操作转化为mapreduce计算的模块(重点)

3、  hadoop:hdfs+mapreduce

4、  metastore:存储元数据

Hive安装和配置

安装环境:

Hive的安装非常简单,只需去官网下载hive的bin包,解压到自己的目录即可,剩下的工作就是配置自己的hive环境了。

Hive安装在sy-se-da02.sy.leho.com,ip为10.10.60.31

Mysql安装在sy-se-da01.sy.leho.com,ip为10.10.60.20,端口为3306

Mysql端配置:

首先解释一下为什么要用mysql。Hive将元数据存储在在 RDBMS 中,一般常用的有MYSQL和DERBY,默认存储在hive自带的DERBY中。但是将元数据存储在DERBY中,无法提供多用户同时访问hive,只能用于简单的测试。而采用mydql存储元数据,允许多用于同时访问hive。

1.       创建数据库hive,用于存储hivemetadata

Createdatabase hive;

2. 在mysql专门为hive添加用户,并授权

create user 'hive'@'10.10.60.31' identified by 'MiraCle';

grant all privileges on hive to 'hive'@'10.10.60.31' with grantoption;

flush privileges;

hive端配置:

修改hive/conf文件hive-site.xml(红色部分为修改地方)

可能你在conf目录下找不到hive-site.xml,此时可用hive-default.xml拷贝一份hive-site.xml,hive-default.xml文件作为默认配置文件备份。

 

<property>

 <name>javax.jdo.option.ConnectionURL</name>

  <value>jdbc:mysql://10.10.60.20:3306/hive?characterEncoding=UTF-8</value>

  <description>JDBCconnect string for a JDBC metastore</description>

</property>

 

<property>

 <name>javax.jdo.option.ConnectionDriverName</name>

  <value>com.mysql.jdbc.Driver</value>

  <description>Driverclass name for a JDBC metastore</description>

</property>

 

<property>

 <name>javax.jdo.option.ConnectionUserName</name>

  <value>hive</value>

  <description>usernameto use against metastore database</description>

</property>

 

<property>

 <name>javax.jdo.option.ConnectionPassword</name>

  <value>MiraCle</value>

  <description>passwordto use against metastore database</description>

</property>

添加jdbc的jar包

wget http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.11.tar.gz/from/http://mysql.he.net/
tar -xvzf mysql-connector-java-5.1.11.tar.gz
cp mysql-connector-java-5.1.11/*.jar /data/soft/hive/lib

注意:此处一定要将jar包拷贝到hive下的lib目录中

hive的元数据存储在mysql,报错的解决方法!

当在hive中drop table 时如果报以下错时
FAILED: Error in metadata: javax.jdo.JDODataStoreException: Error(s) were foundwhile auto-creating/validating the datastore for classes. The errors areprinted in the log, and are attached to this exception.

NestedThrowables:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:Specified key was too long; max key length is 767 bytes

FAILED: Execution Error, return code 1from org.apache.hadoop.hive.ql.exec.DDLTask

解决的方法是到mysql中的hive数据库里执行alter database hive character set latin1;改变hive元数据库的字符集,问题就可以解决!

 

Hive基本命令

创建表:

     hive> create tablerefer2url(time_stamp int, refer string, url string, baiduid int)

    > partitioned by (timeint)                                                   

    > row formatdelimited                                                        

> fields terminated by '\t'

> stored as textfile ;

 

创建一个新表,结构与其他一样

hive> createtable new_table like refer2url;

 

加载分区表数据:

hive> loaddata local inpath '/home/grass/hive/partitions/file1' into table refer2url partition(time='20120901');

 

展示表中有多少分区:

hive> showpartitions refer2url;

 

展示所有表:

hive> SHOWTABLES;

 

显示表的结构信息:

hive>DESCRIBE refer2url;

hive>DESCrefer2url;

 

更新表的名称:

hive> ALTERTABLE source RENAME TO target;

 

添加新一列

hive> ALTERTABLE invites ADD COLUMNS (new_col2 INT COMMENT 'a comment');

 

删除表:

hive> DROPTABLE refer2url;

删除表中数据,但要保持表的结构定义

hive> dfs-rmr /user/hive/warehouse/ refer2url;

 

显示所有函数:

hive> showfunctions;

查看函数用法:

hive>describe function substr;

 

查看hive为某个查询使用多少个MapReduce作业

hive> Explain SELECTsales.*, things.* FROM sales JOIN things ON (sales.id = things.id);

比较有用,可以验证sql语句的好坏

 

数据中心hive应用示例

此示例用于计算每天各个refer带来的pv、uv、ip_num

1.      在hive中创建表

hive> create table refer2url(time_stamp int, refer string, urlstring, baiduid int)

    > partitioned by (timeint)                                                   

    > row formatdelimited                                                         

> fields terminated by ','

> stored as textfile ;

         refer2url表采用按天分区。

2.      将日志中对应的数据load进refer2url表

load data inpath '格式化日志后的路径' overwriteinto table refer2url partition (time='20120801')'

应用此命令依次将数据导入hive中。

3.      应用hive查询20120801一天中各个refer带来的pv、uv、ip_num

insert overwrite directory ‘query_result_directory’

select refer, count(*) as pv, count(distinct baiduid),count(distinct ip)

from refer2url

where time=’20120801’;

执行上面的命令,hive将该查询语句解析为hadoop的mapreduce任务进行计算,最后将结果输出到命令中指定的目录‘query_result_directory’。

 

可见,hive的方便之处,不用再去写繁琐的map、reduce脚本,hive的一个查询操作就搞定了,大大减少了工作量。

 

 

Hive wiki:

https://cwiki.apache.org/confluence/display/Hive/Home

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值