Hive学习之路 (二)Hive安装

Hive的下载

下载地址http://mirrors.hust.edu.cn/apache/

选择合适的Hive版本进行下载,进到stable-2文件夹可以看到稳定的2.x的版本是2.3.3

Hive的安装

1、本人使用MySQL做为Hive的元数据库,所以先安装MySQL。

MySql安装过程http://www.cnblogs.com/qingyunzong/p/8294876.html

2、上传Hive安装包

3、解压安装包

[[email protected] ~]$ tar -zxvf apache-hive-2.3.3-bin.tar.gz -C apps/

4、修改配置文件

配置文件所在目录apache-hive-2.3.3-bin/conf

[[email protected] apps]$ cd apache-hive-2.3.3-bin/
[[email protected] apache-hive-2.3.3-bin]$ ls
bin  binary-package-licenses  conf  examples  hcatalog  jdbc  lib  LICENSE  NOTICE  RELEASE_NOTES.txt  scripts
[[email protected] apache-hive-2.3.3-bin]$ cd conf/
[[email protected] conf]$ ls
beeline-log4j2.properties.template    ivysettings.xml
hive-default.xml.template             llap-cli-log4j2.properties.template
hive-env.sh.template                  llap-daemon-log4j2.properties.template
hive-exec-log4j2.properties.template  parquet-logging.properties
hive-log4j2.properties.template
[[email protected] conf]$ pwd
/home/hadoop/apps/apache-hive-2.3.3-bin/conf
[[email protected] conf]$ 

新建hive-site.xml并添加以下内容

[[email protected] conf]$ touch hive-site.xml
[[email protected] conf]$ vi hive-site.xml 
<configuration>
        <property>
                <name>javax.jdo.option.ConnectionURL</name>
                <value>jdbc:mysql://hadoop1:3306/hivedb?createDatabaseIfNotExist=true</value>
                <description>JDBC connect string for a JDBC metastore</description>
                <!-- 如果 mysql 和 hive 在同一个服务器节点,那么请更改 hadoop02 为 localhost -->
        </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>

以下可选配置,该配置信息用来指定 Hive 数据仓库的数据存储在 HDFS 上的目录

        <property>
                <name>hive.metastore.warehouse.dir</name>
                <value>/hive/warehouse</value>
                <description>hive default warehouse, if nessecory, change it</description>
        </property>    

5、 一定要记得加入 MySQL 驱动包(mysql-connector-java-5.1.40-bin.jar)该 jar 包放置在 hive 的根路径下的 lib 目录

6、 安装完成,配置环境变量

[[email protected] lib]$ vi ~/.bashrc 
#Hive
export HIVE_HOME=/home/hadoop/apps/apache-hive-2.3.3-bin
export PATH=$PATH:$HIVE_HOME/bin

使修改的配置文件立即生效

[[email protected] lib]$ source ~/.bashrc 

7、 验证 Hive 安装

[[email protected] ~]$ hive --help
Usage ./hive <parameters> --service serviceName <service parameters>
Service List: beeline cleardanglingscratchdir cli hbaseimport hbaseschematool help hiveburninclient hiveserver2 hplsql jar lineage llapdump llap llapstatus metastore metatool orcfiledump rcfilecat schemaTool version
Parameters parsed:
  --auxpath : Auxiliary jars
  --config : Hive configuration directory
  --service : Starts specific service/component. cli is default
Parameters used:
  HADOOP_HOME or HADOOP_PREFIX : Hadoop install directory
  HIVE_OPT : Hive options
For help on a particular service:
  ./hive --service serviceName --help
Debug help:  ./hive --debug --help
[[email protected] ~]$ 

8、 初始化元数据库

  注意:当使用的 hive 是 2.x 之前的版本,不做初始化也是 OK 的,当 hive 第一次启动的 时候会自动进行初始化,只不过会不会生成足够多的元数据库中的表。在使用过程中会 慢慢生成。但最后进行初始化。如果使用的 2.x 版本的 Hive,那么就必须手动初始化元 数据库。使用命令:

[[email protected] ~]$ schematool -dbType mysql -initSchema
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/hadoop/apps/apache-hive-2.3.3-bin/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/hadoop/apps/hadoop-2.7.5/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]
Metastore connection URL:     jdbc:mysql://hadoop1:3306/hivedb?createDatabaseIfNotExist=true
Metastore Connection Driver :     com.mysql.jdbc.Driver
Metastore connection User:     root
Starting metastore schema initialization to 2.3.0
Initialization script hive-schema-2.3.0.mysql.sql
Initialization script completed
schemaTool completed
[[email protected] ~]$ 

9、 启动 Hive 客户端

hive --service cli和hive效果一样

[[email protected] ~]$ hive --service cli
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/hadoop/apps/apache-hive-2.3.3-bin/lib/log4j-slf4j-impl-2.6.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/hadoop/apps/hadoop-2.7.5/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:/home/hadoop/apps/apache-hive-2.3.3-bin/lib/hive-common-2.3.3.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. spark, tez) or using Hive 1.X releases.
hive> 

基本使用

现有一个文件student.txt,将其存入hive中,student.txt数据格式如下:

95002,刘晨,女,19,IS
95017,王风娟,女,18,IS
95018,王一,女,19,IS
95013,冯伟,男,21,CS
95014,王小丽,女,19,CS
95019,邢小丽,女,19,IS
95020,赵钱,男,21,IS
95003,王敏,女,22,MA
95004,张立,男,19,IS
95012,孙花,女,20,CS
95010,孔小涛,男,19,CS
95005,刘刚,男,18,MA
95006,孙庆,男,23,CS
95007,易思玲,女,19,MA
95008,李娜,女,18,CS
95021,周二,男,17,MA
95022,郑明,男,20,MA
95001,李勇,男,20,CS
95011,包小柏,男,18,MA
95009,梦圆圆,女,18,MA
95015,王君,男,18,MA

1、创建一个数据库myhive

hive> create database myhive;
OK
Time taken: 7.847 seconds
hive> 

2、使用新的数据库myhive

hive> use myhive;
OK
Time taken: 0.047 seconds
hive> 

3、查看当前正在使用的数据库

hive> select current_database();
OK
myhive
Time taken: 0.728 seconds, Fetched: 1 row(s)
hive> 

4、在数据库myhive创建一张student表

hive> create table student(id int, name string, sex string, age int, department string) row format delimited fields terminated by ",";
OK
Time taken: 0.718 seconds
hive> 

5、往表中加载数据

hive> load data local inpath "/home/hadoop/student.txt" into table student;
Loading data to table myhive.student
OK
Time taken: 1.854 seconds
hive> 

6、查询数据

hive> select * from student;
OK
95002    刘晨    女    19    IS
95017    王风娟    女    18    IS
95018    王一    女    19    IS
95013    冯伟    男    21    CS
95014    王小丽    女    19    CS
95019    邢小丽    女    19    IS
95020    赵钱    男    21    IS
95003    王敏    女    22    MA
95004    张立    男    19    IS
95012    孙花    女    20    CS
95010    孔小涛    男    19    CS
95005    刘刚    男    18    MA
95006    孙庆    男    23    CS
95007    易思玲    女    19    MA
95008    李娜    女    18    CS
95021    周二    男    17    MA
95022    郑明    男    20    MA
95001    李勇    男    20    CS
95011    包小柏    男    18    MA
95009    梦圆圆    女    18    MA
95015    王君    男    18    MA
Time taken: 2.455 seconds, Fetched: 21 row(s)
hive> 

7、查看表结构

hive> desc student;
OK
id                      int
name                    string
sex                     string
age                     int
department              string
Time taken: 0.102 seconds, Fetched: 5 row(s)
hive> 
hive> desc extended student;
OK
id                      int
name                    string
sex                     string
age                     int
department              string                                      

Detailed Table Information    Table(tableName:student, dbName:myhive, owner:hadoop, createTime:1522750487, lastAccessTime:0, retention:0, sd:StorageDescriptor(cols:[FieldSchema(name:id, type:int, comment:null), FieldSchema(name:name, type:string, comment:null), FieldSchema(name:sex, type:string, comment:null), FieldSchema(name:age, type:int, comment:null), FieldSchema(name:department, type:string, comment:null)], location:hdfs://myha01/user/hive/warehouse/myhive.db/student, inputFormat:org.apache.hadoop.mapred.TextInputFormat, outputFormat:org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat, compressed:false, numBuckets:-1, serdeInfo:SerDeInfo(name:null, serializationLib:org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, parameters:{serialization.format=,, field.delim=,}), bucketCols:[], sortCols:[], parameters:{}, skewedInfo:SkewedInfo(skewedColNames:[], skewedColValues:[], skewedColValueLocationMaps:{}), storedAsSubDirectories:false), partitionKeys:[], parameters:{transient_lastDdlTime=1522750695, totalSize=523, numRows=0, rawDataSize=0, numFiles=1}, viewOriginalText:null, viewExpandedText:null, tableType:MANAGED_TABLE, rewriteEnabled:false)
Time taken: 0.127 seconds, Fetched: 7 row(s)
hive> 
hive> desc formatted student;
OK
# col_name                data_type               comment             

id                      int
name                    string
sex                     string
age                     int
department              string                                      

# Detailed Table Information
Database:               myhive
Owner:                  hadoop
CreateTime:             Tue Apr 03 18:14:47 CST 2018
LastAccessTime:         UNKNOWN
Retention:              0
Location:               hdfs://myha01/user/hive/warehouse/myhive.db/student
Table Type:             MANAGED_TABLE
Table Parameters:
    numFiles                1
    numRows                 0
    rawDataSize             0
    totalSize               523
    transient_lastDdlTime    1522750695          

# Storage Information
SerDe Library:          org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe
InputFormat:            org.apache.hadoop.mapred.TextInputFormat
OutputFormat:           org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat
Compressed:             No
Num Buckets:            -1
Bucket Columns:         []
Sort Columns:           []
Storage Desc Params:
    field.delim             ,
    serialization.format    ,
Time taken: 0.13 seconds, Fetched: 34 row(s)
hive> 

原文地址:https://www.cnblogs.com/qingyunzong/p/8708057.html

时间: 2024-07-29 05:28:00

Hive学习之路 (二)Hive安装的相关文章

java痛苦学习之路[二] ---JSONObject使用

一.Strut2必须引入的包 要使程序可以运行必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包: 1.commons-lang.jar 2.commons-beanutils.jar 3.commons-collections.jar 4.commons-logging.jar 5.ezmorph.jar 6.json-lib-2.2.2-jdk15.jar 当然除了这些包,strut2基础包也得引入 struts2-core-2.1.6.jar freemarker-2.

Hive学习之路 (二十)Hive 执行过程实例分析

一.Hive 执行过程概述 1.概述 (1) Hive 将 HQL 转换成一组操作符(Operator),比如 GroupByOperator, JoinOperator 等 (2)操作符 Operator 是 Hive 的最小处理单元 (3)每个操作符代表一个 HDFS 操作或者 MapReduce 作业 (4)Hive 通过 ExecMapper 和 ExecReducer 执行 MapReduce 程序,执行模式有本地模式和分 布式两种模式 2.Hive 操作符列表 3.Hive 编译器的

Hive学习笔记(二)

类型转换 hive原子数据类型可以进行隐式数据类型转换,如果某个表达式使用INT,那么TINYINT会转化为INT,hive不会进行反向转换,除非使用cast才操作. 数据类型转化规则: 任何整数类型都会转换为范围更广的类型. 所有整数类型.FLOAT.STRING类型都能隐式转换为double类型. TINYINT.SMALLINY.INT都可以转换为FLOAT BLOOEAN 不能转换为任何其他数据类型 cast进行显式数据类型转换: eg:CAST('1',AS INT) 将字符串'1'转

Hive学习之路 (一)Hive初识

Hive 简介 什么是Hive 1.Hive 由 Facebook 实现并开源 2.是基于 Hadoop 的一个数据仓库工具 3.可以将结构化的数据映射为一张数据库表 4.并提供 HQL(Hive SQL)查询功能 5.底层数据是存储在 HDFS 上 6.Hive的本质是将 SQL 语句转换为 MapReduce 任务运行 7.使不熟悉 MapReduce 的用户很方便地利用 HQL 处理和计算 HDFS 上的结构化的数据,适用于离线的批量数据计算. 数据仓库之父比尔·恩门(Bill Inmon

Hive学习之路 (十八)Hive的Shell操作

一.Hive的命令行 1.Hive支持的一些命令 Command Description quit Use quit or exit to leave the interactive shell. set key=value Use this to set value of particular configuration variable. One thing to note here is that if you misspell the variable name, cli will no

[转帖]Hive学习之路 (一)Hive初识

https://www.cnblogs.com/qingyunzong/p/8707885.html 讨论QQ:1586558083 目录 Hive 简介 什么是Hive 为什么使用 Hive Hive 特点 Hive 和 RDBMS 的对比 Hive的架构 1.用户接口: shell/CLI, jdbc/odbc, webui Command Line Interface 2.跨语言服务 : thrift server 提供了一种能力,让用户可以使用多种不同的语言来操纵hive 3.底层的Dr

hive学习之路

#变量 #引入变量 given_dayno="'20190601'" …… dayno=${given_dayno} #退出 exit ${v_job_stat} #参数 #map过大 跑不动number of mappers: 230691; number of reducers: 1099 set mapred.max.split.size=1000000000; #并行执行(union all 多的情况) set hive.exec.parallel=true; set hive

Hive学习(八)Hive数据导出三种方式

今天我们再谈谈Hive中的三种不同的数据导出方式. 根据导出的地方不一样,将这些方式分为三种: (1).导出到本地文件系统: (2).导出到HDFS中: (3).导出到Hive的另一个表中. 为了避免单纯的文字,我将一步一步地用命令进行说明. 一.导出到本地文件系统 hive> insert overwrite local directory '/home/wyp/wyp' > select * from wyp; 复制代码 这条HQL的执行需要启用Mapreduce完成,运行完这条语句之后,

Hive学习(九)Hive体系结构

1.Hive架构与基本组成 下面是Hive的架构图. 图1.1 Hive体系结构 Hive的体系结构可以分为以下几部分: (1)用户接口主要有三个:CLI,Client 和 WUI.其中最常用的是CLI,Cli启动的时候,会同时启动一个Hive副本.Client是Hive的客户端,用户连接至Hive Server.在启动 Client模式的时候,需要指出Hive Server所在节点,并且在该节点启动Hive Server. WUI是通过浏览器访问Hive. (2)Hive将元数据存储在数据库中