hive的详细介绍,安装,部署

简介:

介绍:

一、什么是hive???

1,hive是基于Hadoop的一个数据仓库工具、

2,可以将结构化的数据文件映射为一张数据库表,并提供类sql的查询功能、

3,可以将sql语句转换为mapreduce任务进行运行、

4,可以用来进行数据提取转换加载(ETL)

5,hive是sql解析引擎,它将sql 语句转换成M/R job然后在Hadoop中运行。

hive的表其实就是HDFS的目录/文件夹。 

hive表中的数据 就是hdfs目录中的文件。按表名把文件夹分开。如果是分区表,则分区值是子文件夹,可以直接在M/R job里使用这些数据.


6,hive优点与缺点:

可以提供类SQL语句快速实现简单的mapreduce统计,不需要开发专门的mapreduce应用

不支持实时查询 


7,hive数据分为真实存储的数据和元数据

真实数据存储在hdfs中,元数据存储在mysql中 

metastore 元数据存储数据库       

Hive将元数据存储在数据库中,如MySQL、derby。

Hive中的元数据包括表的名字,表的列和分区及其属性,表的属性(是否为外部表等),表的数据所在目录等。


二、hive的体系架构:


wKiom1hBPF6Qp69oAAFG4S0rL6Q799.png

用户接口,包括 CLI(shell),JDBC/ODBC,WebUI(通过浏览器)

元数据存储,通常是存储在关系数据库如 mysql, derby 中

解释器、编译器、优化器、执行器完成HQL查询语句从语法分析,编译,优化以及查询计划的生成,生成的查询计划存储在HDFS中,并随后被mapreduce调用执行

Hadoop:用 HDFS 进行存储,利用 MapReduce 进行计算(带*的查询select * from teacher不会生成mapreduce任务,只是进行全表扫描)


   

在此强调:

Hadoop,zookpeer,spark,kafka,mysql已经正常启动

三、开始安装部署hive

基础依赖环境:

1
2
3
4
1,jdk   1.6+
2, hadoop 2.x
3,hive 0.13-0.19
4,mysql   (mysql-connector-jar)

安装详细如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
#java 
export  JAVA_HOME= /soft/jdk1 .7.0_79/
export  CLASSPATH=.:$JAVA_HOME /lib/dt .jar:$JAVA_HOME /lib/tools .jar
#bin
export  PATH=$PATH:/$JAVA_HOME /bin :$HADOOP_HOME /bin :$SCALA_HOME /bin :$SPARK_HOME /bin : /usr/local/hadoop/hive/bin
#hadoop
export  HADOOP_HOME= /usr/local/hadoop/hadoop
#scala
export  SCALA_HOME= /usr/local/hadoop/scala
#spark
export  SPARK_HOME= /usr/local/hadoop/spark
#hive
export  HIVE_HOME= /usr/local/hadoop/hive

                              


一、开始安装:

1,下载:

1
https: //hive .apache.org /downloads .html

解压:

1
2
3
tar   xvf   apache-hive-2.1.0-bin. tar .gz  -C   /usr/local/hadoop/
cd   /usr/local/hadoop/
mv    apache-hive-2.1.0   hive

2,修改配置

1
2
3
4
5
6
7
8
9
修改启动环境
cd    /usr/local/hadoop/hive
vim bin /hive-config .sh
#java 
export  JAVA_HOME= /soft/jdk1 .7.0_79/
#hadoop
export  HADOOP_HOME= /usr/local/hadoop/hadoop
#hive
export  HIVE_HOME= /usr/local/hadoop/hive

修改默认配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cd    /usr/local/hadoop/hive
vim conf /hive-site .xml
<configuration>
     <property>
         <name>javax.jdo.option.ConnectionURL< /name >
         <value>jdbc:mysql: //master :3306 /hive ?createDatabaseInfoNotExist= 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>hive< /value >
             <description>Username to use against metastore database< /description >
       < /property >
     <property>
             <name>javax.jdo.option.ConnectionPassword< /name >
             <value>xujun< /value >
             <description>password to use against metastore database< /description >
       < /property >
< /configuration >

3,修改tmp dir

修改将含有"system:java.io.tmpdir"的配置项的值修改为如上地址

/tmp/hive

4,安装mysql driver

去mysql官网下载驱动mysql-connector-java-5.1.40.zip

unzip  mysql-connector-java-5.1.40.zip

cp mysql-connector-java-5.1.40-bin.jar /user/lcoal/hadoop/hive/lib/

 

二、安装好mysql,并且启动

1.创建数据库

1
2
3
create  database  hive 
grant  all  on  *.*  to   hive@ '%'   identified  by  'hive' ;
flush   privileges ;

三,初始化hive(初始化metadata)

1
2
3
4
5
6
7
8
9
10
11
cd    /usr/local/hadoop/hive
bin /schematool  -initSchema -dbType mysql 
SLF4J: See http: //www .slf4j.org /codes .html #multiple_bindings for an explanation.
SLF4J: Actual binding is of  type  [org.apache.logging.slf4j.Log4jLoggerFactory]
Metastore connection URL: jdbc:mysql: //hadoop3 :3306 /hive ?createDatabaseInfoNotExist= true
Metastore Connection Driver : com.mysql.jdbc.Driver
Metastore connection User: hive
Starting metastore schema initialization to 2.1.0
Initialization script hive-schema-2.1.0.mysql.sql
Initialization script completed
schemaTool completed



四、启动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
[hadoop@hadoop1 hadoop]$ hive /bin/hive
which : no hbase  in  ( /usr/lib64/qt-3 .3 /bin : /usr/local/bin : /bin : /usr/bin : /usr/local/sbin : /usr/sbin : /sbin : //soft/jdk1 .7.0_79 //bin : /bin : /bin : /bin : /usr/local/hadoop/hive/bin : /home/hadoop/bin )
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding  in  [jar: file : /usr/local/hadoop/hive/lib/log4j-slf4j-impl-2 .4.1.jar! /org/slf4j/impl/StaticLoggerBinder .class]
SLF4J: Found binding  in  [jar: file : /usr/local/hadoop/hadoop/share/hadoop/common/lib/slf4j-log4j12-1 .7.10.jar! /org/slf4j/impl/StaticLoggerBinder .class]
SLF4J: See http: //www .slf4j.org /codes .html #multiple_bindings for an explanation.
SLF4J: Actual binding is of  type  [org.apache.logging.slf4j.Log4jLoggerFactory]
Logging initialized using configuration  in  jar: file : /usr/local/hadoop/hive/lib/hive-common-2 .1.0.jar! /hive-log4j2 .properties Async:  true
Hive-on-MR is deprecated  in  Hive 2 and may not be available  in  the future versions. Consider using a different execution engine (i.e. tez, spark) or using Hive 1.X releases.
hive> show databases;
OK
default
Time taken: 1.184 seconds, Fetched: 1 row(s)
hive>
 
 
 
五,实践操作
 
 
使用hive创建表
以下两个操作只是针对当前session终端
1,hive>  set   hive.cli.print.current.db= true ;   设置显示当前数据库名
hive (default)> 
 
2,hive (default)>  set   hive.cli.print.header= true ;    当使用 select  查询数据时候,显示的结果会带有表的字段名称
 
3,创建表,并导入数据
hive> create table teacherq( id  bigint,name string) row  format  delimited fields terminated by  '\t' ;
OK
hive> create table people ( id  int ,name string);
OK
Time taken: 3.363 seconds
hive> SHOW TABLES;
OK
people
teacherq
student
Time taken: 0.283 seconds, Fetched: 1 row(s)
 
导入数据:
hive>load  data   local   inpath  '/root/stdent.txt'   into table teacherq;
 
注意:如果你是普通用户启动hive,则使用相对路径来导入本地数据
 
mv  stdent.txt  /usr/local/hadoop/hive/
cd    /usr/local/hadoop/hive
  > load  data   local   inpath   'stdent.txt'   into table teacherq;
Loading data to table default.teacherq
OK
Time taken: 2.631 seconds
        
hive>  select  * from teacherq;
OK
1   zhangsan
2   lisi
3   wangwu
4   libai
Time taken: 1.219 seconds, Fetched: 4 row(s)
hive>


4.建表(默认是内部表)

适用于先创建表,后load加载数据、

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

默认普通表load数据:

1
load  data   local  inpath  '/root/student.txt'   into   table  student;


建外部表

适用于,hdfs先有数据,后创建表,进行数据查询,分析管理

 

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

 

外部表load数据:

   

1
  load  data   local  inpath  '/root/student.txt'   into   table  student;


建分区表

方法一:先创建分区表,然后load数据

    partition就是辅助查询,缩小查询范围,加快数据的检索速度和对数据按照一定的规格和条件进行管理。

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

分区表中load数据

 

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

hive (itcast)> select * from beat;

OK

beat.idbeat.namebeat.sizebeat.nation

1glm22.0china

2slsl21.0china

3sdsd20.0china

NULLwww19.0china

Time taken: 0.22 seconds, Fetched: 4 row(s)



方法二:先在hdfs 创建目录,倒入数据,最后,更改hive元数据的信息

1, 创建分区目录

   hive (itcast)> dfs -mkdir /beat/nation=japan

dfs -ls /beat;

Found 2 items

drwxr-xr-x   - hadoop supergroup          0 2016-12-05 16:07 /beat/nation=china

drwxr-xr-x   - hadoop supergroup          0 2016-12-05 16:16 /beat/nation=japan

2, 为分区目录加载数据

hive (itcast)> dfs -put d.c  /beat/nation=japan

    此时查询数据:数据还未加载进来。

hive (itcast)> dfs -ls /beat/nation=japan;

Found 1 items

-rw-r--r--   3 hadoop supergroup         20 2016-12-05 16:16 /beat/nation=japan/d.c

hive (itcast)> select * from beat;

OK

beat.idbeat.namebeat.sizebeat.nation

1glm22.0china

2slsl21.0china

3sdsd20.0china

NULLwww19.0china

Time taken: 0.198 seconds, Fetched: 4 row(s)


3,手动修改hive表结构,添加分区表信息

hive (itcast)>  alter table beat add partition (nation='japan') location "/beat/nation=japan";

OK

Time taken: 0.089 seconds

hive (itcast)> select * from beat;

OK

beat.idbeat.namebeat.sizebeat.nation

1glm22.0china

2slsl21.0china

3sdsd20.0china

NULLwww19.0china

7ab111.0japan

8rb23234.0japan

Time taken: 0.228 seconds, Fetched: 6 row(s)


此时数据加载完成。




删除分区

用户可以用 ALTER TABLE DROP PARTITION 来删除分区。分区的元数据和数据将被一并删除。

例:

ALTER TABLE beat DROP PARTITION (nation='japan');



特殊情况案例:

1,表中的某个字段需要作为分区的分区名,默认不允许创建,解决方法:

hive (itcast)> create table sms(id bigint ,content string,area string) partitioned by (area string) row format delimited fields terminated by '\t' ;

FAILED: SemanticException [Error 10035]: Column repeated in partitioning columns

解决方法:

建立冗余字段,即使用 area_pat来区分,

或者修改源码

 hive (itcast)> create table sms(id bigint ,content string,area string) partitioned by (area_pat string) row format delimited fields terminated by '\t' ;

 


      本文转自crazy_charles 51CTO博客,原文链接:http://blog.51cto.com/douya/1878779,如需转载请自行联系原作者





相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
12月前
|
NoSQL 前端开发 BI
MongoDB 分页、排序与统计查询技巧
MongoDB 提供 `count()`、`sort()`、`skip()`、`limit()` 等链式方法,高效实现数据统计、分页与排序。支持多字段排序、条件分页,执行顺序固定为:先排序 → 再跳过 → 后限制数量,适用于各类业务场景的数据查询处理。(239字符)
|
人工智能 安全 机器人
OpenClaw(原 Clawdbot)钉钉对接保姆级教程 手把手教你打造自己的 AI 助手
OpenClaw(原Clawdbot)是一款开源本地AI助手,支持钉钉、飞书等多平台接入。本教程手把手指导Linux下部署与钉钉机器人对接,涵盖环境配置、模型选择(如Qwen)、权限设置及调试,助你快速打造私有、安全、高权限的专属AI助理。(239字)
41278 184
|
10月前
|
人工智能 监控 安全
AI 智能体的上线
国内AI智能体上线已成高合规工程:须完成算法备案、安全评估与AIGC标识;部署沙箱+影子模式验证逻辑;设置“人在回路”审核闸门;通过对抗测试与内容过滤;建立负反馈闭环持续优化。安全稳定,方能落地关键业务闭环。(239字)
|
9月前
|
人工智能 自然语言处理 API
阿里云计算巢部署OpenClaw|iMessage一键接入+千问/Coding Plan API+避坑指南
2026年,AI自动化框架OpenClaw(原Clawdbot)凭借阿里云计算巢一键部署、云端稳定运行与iMessage深度集成能力,成为连接苹果生态与云端AI的核心工具。计算巢提供官方认证的OpenClaw部署模板,无需手动配置环境,5分钟即可完成云端部署;搭配阿里云千问大模型、免费Coding Plan API,再通过iMessage渠道实现iPhone、Mac、iPad与云端AI的无缝交互,满足个人效率管理、移动AI助手、轻量业务开发等场景需求。
677 2
|
10月前
|
机器人 API 数据安全/隐私保护
保姆级教程:阿里云上及本地部署OpenClaw/Clawdbot+集成飞书等Skills步骤流程
在2026年企业协同办公数字化转型的浪潮中,OpenClaw(原Clawdbot、曾用名Moltbot)作为开源优先的AI代理与自动化平台,凭借灵活的部署方式、丰富的技能生态和零技术门槛的操作优势,成为企业提升办公效率的核心工具。无需专业编程基础,即可轻松实现文档生成、代码开发、任务自动化、多模态解析等多元功能,同时支持与飞书无缝集成,构建24小时响应的企业级AI机器人,打破办公工具割裂的痛点,实现指令直达、任务秒级执行。
1741 8
|
Windows 定位技术 弹性计算
|
人工智能 运维 开发工具
Coze开源版本地安装指南
字节跳动开源AI智能体开发平台Coze,具备极低的硬件门槛——2核CPU加4GB内存即可运行,并提供详细的本地部署指南。通过Docker环境准备、源码获取、模型服务配置及一键启动服务等步骤,即使是零基础用户也能在10分钟内完成部署。Coze支持多种AI模型集成,采用Apache 2.0协议,实现零成本商用,覆盖从开发到部署的全链路需求,旨在让每个人都能轻松成为AI创造者。
|
存储 Linux Go
如何在Github上Pull Request的教程
关于如何在GitHub上发起Pull Request(合并请求)的详细教程,包括Fork(分支)、Clone(克隆)、创建新分支、修改代码、提交更改、推送到远程仓库等步骤,并提供了解决权限问题的方法,如创建个人访问令牌(Personal Access Token)。
1206 6
|
NoSQL 数据挖掘 定位技术
如何让geopandas支持写出gdb文件
如何让geopandas支持写出gdb文件
1021 4
|
安全 物联网 数据建模
IP地址能否申请HTTPS证书?
IP地址可申请HTTPS证书,但需满足特定条件。首先,该IP须为公网IP,具备唯一性和可控性。证书类型限于DV或OV级别,不支持EV。申请过程包括所有权验证及端口开放。适用于服务器间通信及IoT设备等场景。申请时需注意成本、浏览器兼容性和安全性问题。