美文网首页
HIVE简介与安装

HIVE简介与安装

作者: pamperxg | 来源:发表于2017-08-08 11:40 被阅读0次

基于hadoop的数据仓库工具,封装了mapreduce,大大简化了开发过程(简单来说,就是把sql变成了mapreduce程序)。!!!这里说的是hive-1.2.1,在2.几的版本时不支持mapreduce了,支持spark。利用hdfs存储数据,利用mapreduce查询数据。
与传统关系型数据库的对比:

与RDBMS对比

数据库与数据仓库

目标目的组织方式都不同。
数据库,一般是关系型,支持在线联机业务。进行实时事务控制。数据库中有不止一张不同的业务表。而数据仓库存储的是历史的业务数据(即数据库中导出的历史数据),主要用于数据分析。一般是非关系型,是宽表,不在乎数据是否冗余,按照历史和主题来组织。分析的结果支持公司的运营,可以按照指标+维度(粒度)来产生报表,组织的模型有:星形模型,雪花模型。

hive的工作机制

如下图,当我们使用hive时写sql建表和数据库时,其实就是创建了/usr/hive/warehouse/下的目录,这些目录位置信息都由hive存到了另外的数据库(mysql等,hive自带的derby)中。当我们执行查找等操作时,hive中的compiler先解析sql语句(最难的部分),然后转换成mapreduce程序(hive里有mapreduce模板),由hive的jobrunner提交任务给yarn执行。基于mapreduce执行会比较慢,纯粹用来做离线数据分析。

hive的工作机制 架构图

hive安装

hive只在一个节点上安装即可。
1 上传解压hive的tar包
2 安装mysql

rpm -qa | mysql
rpm -e --nodeps mysql
yum list | grep mysql
yum install -y mysql-server mysql mysql-dev_
rpm -qi mysql -server(查看版本信息)
service mysqld start
chkconfig mysqld on(设置开机自启)
mysqladmin -u root password 'root'
cat /etc/my.cnf(查看主配置文件)
netstat -nltp(linux系统是否监听3306端口)

3 配置hive
不用提供的模板,直接来一个新的hive-site.xml

vi hive-site.xml
<configuration>
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://localhost: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>root</value>
<description>password to use against metastore database</description>
</property>
</configuration>

4 启动hive
安装完成后,先将mysql连接jar包(没有可以到maven中央仓库下载)拷贝到$HIVE_HOME/lib目录下(ll my.* 查看是否成功)。
若果出现没有权限问题,在mysql授权:

mysql -u root -p
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
FLUSH PRIVILEGES;

启动:
hive 交互式shell:
bin/hive
hive启动时会找到hadoop的配置文件,加载hadoop所有的jar包到它的classpath,此时Jline包版本不同会报错。解决办法:
拷贝hive/lib中的jline.2.12.jar包替换/home/hadoop/apps/hadoop-2.6.4/share/hadoop/yarn/lib/jline-0.9.94.jar,即可正常启动。
hive命令
hive -e 'sql',从命令行执行指定hql
hive -f执行hql脚本
hive thrift服务:
启动thrift服务后,只要是符合thrift协议的客户端都可以连接。hive自带了一个客户端beeline

[hadoop@mini1 hive]$ cd bin/
[hadoop@mini1 bin]$ ./beeline 
Beeline version 1.2.1 by Apache Hive
beeline> !connect jdbc:hive2://localhost:10000
Connecting to jdbc:hive2://localhost:10000
Enter username for jdbc:hive2://localhost:10000: hadoop(username默认hadoop,无密码)
Enter password for jdbc:hive2://localhost:10000: 
Connected to: Apache Hive (version 1.2.1)
Driver: Hive JDBC (version 1.2.1)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://localhost:10000> showdatabases;

启动服务后转换为后台:nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err &
beeline也可以启动就连接:bin/beeline -u jdbc:hive2://mini1:10000 -n hadoop
5 简单操作:

hive>
hive>show databases;
hive>create database haha;
hive>use haha;
hive>create table t_1(id int,name string);##可以在mini:50070查看
vi t.dat并写入数据
hadoop fs -put t.dat /user/hive/warehouse/haha.db/t_1/
hive>select * from t_1;
hive> truncate table t_1;(清空表里的数据)
hive> drop table t_1;(删除表)
hive> use haha;
OK
Time taken: 0.101 seconds
hive> create table t_sz01(id int,name string)
    > row format delimited
    > fields terminated by ',';
OK
Time taken: 0.435 seconds
hive> show tables;
OK
t_sz01
Time taken: 0.096 seconds, Fetched: 1 row(s)
hadoop fs -put t.dat /user/hive/warehouse/haha.db/t_sz01
hive> select * from t_sz01;
OK
1       h
2       r
3       rg
4       qgqa
5       fdf
6       aff
Time taken: 0.322 seconds, Fetched: 6 row(s)
hive> select count(1) from t_sz01;
Query ID = hadoop_20170808175951_074dd63d-dc84-4125-9b15-2efb1462ca26
Total jobs = 1
Launching Job 1 out of 1
Number of reduce tasks determined at compile time: 1
In order to change the average load for a reducer (in bytes):
  set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
  set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
  set mapreduce.job.reduces=<number>
Job running in-process (local Hadoop)
2017-08-08 17:59:53,976 Stage-1 map = 0%,  reduce = 0%
2017-08-08 17:59:55,995 Stage-1 map = 100%,  reduce = 100%
Ended Job = job_local817039614_0001
MapReduce Jobs Launched: 
Stage-Stage-1:  HDFS Read: 128 HDFS Write: 0 SUCCESS
Total MapReduce CPU Time Spent: 0 msec
OK
6
Time taken: 4.726 seconds, Fetched: 1 row(s) #mapreduce可以在mini1:8088看见
hive> select id,name from t_sz01 where id>3;
hive> select id,name from t_sz01 where id>3 limit 2;#这两句没有转化为mapreduce,hive一个也做了很多优化
hive> select id,name from t_sz01 where id>3 order by name;#避免了写mapreduce的过程
hive的数据存储

Hive中所有的数据都存储在 HDFS 中,没有专门的数据存储格式(可支持Text,SequenceFile(前面两种是hadoop原生自带的),ParquetFile(面向列的,有表头),RCFILE等)只需要在创建表的时候告诉 Hive 数据中的列分隔符和行分隔符,Hive 就可以解析数据。Hive 中包含以下数据模型:DB、Table,External Table,Partition,Bucket。

  • db:在hdfs中表现为${hive.metastore.warehouse.dir}目录下一个文件夹
  • table:在hdfs中表现所属db目录下一个文件夹
  • external table:外部表, 与table类似,不过其数据存放位置可以在任意指定路径
    普通表: 删除表后, hdfs上的文件都删了
    External外部表删除后, hdfs上的文件没有删除, 只是把文件删除了
  • partition:在hdfs中表现为table目录下的子目录
  • bucket:桶, 在hdfs中表现为同一个表目录下根据hash散列(使得join操作效率变高)之后的多个文件, 会根据不同的文件把数据放到不同的文件中

相关文章

  • HIVE简介与安装

    基于hadoop的数据仓库工具,封装了mapreduce,大大简化了开发过程(简单来说,就是把sql变成了mapr...

  • Hive安装与配置

    Hive安装与配置 Hive安装配置 Hive官网:http://hive.apache.org[http://h...

  • 搭建Hive

    简介 hive是构建在hadoop hdfs上的一个数据仓库。 安装 安装hive之前需要先安装jdk和Hadoo...

  • Hive的安装与配置

    一、安装与配置hive 安装hive之前需要到官网查询hive与Hadoop版本的兼容性。这里我所选的hive和H...

  • Spark on Hive配置踩坑全纪录

    Spark与Hive的分别安装与配置 可以参考Spark On Hive 部署和配置和Hive安装配置指南(含Hi...

  • hive环境搭建

    Hive2.1.1安装部署 一、Hive 运行模式 与Hadoop类似,Hive也有 3 种运行模式: 1. 内嵌...

  • Hive安装

    Hive 安装与配置 hive安装 下载压缩包 下载地址 解压压缩包 配置环境变量 /etc/profile 检查...

  • hive安装与配置

    hive安装与配置 准备工作 下载jdk8 jdk安装配置略过 下载hadoop 略过 下载hive 下载对应的发...

  • Hive安装教程

    先安装好hadoop,再安装Hive 下载地址Index of /dist/hive/hive-3.1.2 (ap...

  • 大数据知识 | hive初识

    hive简介 hive架构 hive是什么 官网这样说:https://hive.apache.org/ hive...

网友评论

      本文标题:HIVE简介与安装

      本文链接:https://www.haomeiwen.com/subject/lqjnlxtx.html