• Hive3.1.2安装指南


    Hive3.1.2安装指南

    链接:https://pan.baidu.com/s/1FvQiiKhT6-KeibuNOOdzgQ
    提取码:q25b
    --来自百度网盘超级会员V6的分享

    1、安装Hive3.1.2

    首先需要下载Hive安装包文件, Hive官网下载地址

    #解压安装包
    hadoop@hadoop-master:~$ sudo tar xf apache-hive-3.1.2-bin.tar.gz -C /usr/local/
    hadoop@hadoop-master:~$ cd /usr/local/
    hadoop@hadoop-master:/usr/local$ sudo mv apache-hive-3.1.2-bin hive
    
    #把hbase目录权限赋予给hadoop用户:
    hadoop@hadoop-master:/usr/local$ sudo chown -R hadoop:hadoop hive
    

    为了方便使用,我们把hive命令加入到环境变量中去,命令如下:

    #配置环境变量
    hadoop@hadoop-master:/usr/local$ vim ~/.bashrc 
    hadoop@hadoop-master:/usr/local$ tail -3 ~/.bashrc 
    export HIVE_HOME=/usr/local/hive
    export PATH=$PATH:/usr/local/hadoop/bin:/usr/local/hadoop/sbin:/usr/local/hbase/bin:$HIVE_HOME/bin
    export HADOOP_HOME=/usr/local/hadoop
    
    #使环境变量立即生效
    hadoop@hadoop-master:/usr/local$ source ~/.bashrc
    

    将hive-default.xml.template重命名为hive-default.xml

    hadoop@hadoop-master:/usr/local$ cd /usr/local/hive/conf/
    hadoop@hadoop-master:/usr/local/hive/conf$ cp -a hive-default.xml.template hive-default.xml
    

    然后新建一个配置文件hive-site.xml,添加如下配置信息:

    hadoop@hadoop-master:/usr/local/hive/conf$ nano hive-site.xml
    hadoop@hadoop-master:/usr/local/hive/conf$ cat hive-site.xml
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    <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>hive</value>
        <description>username to use against metastore database</description>
      </property>
      <property>
        <name>javax.jdo.option.ConnectionPassword</name>
        <value>hive</value>
        <description>password to use against metastore database</description>
      </property>
    </configuration>
    

    2、安装并配置mysql

    这里我们采用MySQL数据库保存Hive的元数据,而不是采用Hive自带的derby来存储元数据。

    首先需要安装MySQL

    #更新软件源
    hadoop@hadoop-master:~$ sudo apt-get update
    
    #安装mysql
    hadoop@hadoop-master:~$ sudo apt-get -y install mysql-server
    
    #修改MySQL的编码
    hadoop@hadoop-master:~$ egrep -v "^#|^$" /etc/mysql/mysql.conf.d/mysqld.cnf
    ......
    [mysqld]
    user		= mysql
    pid-file	= /var/run/mysqld/mysqld.pid
    socket		= /var/run/mysqld/mysqld.sock
    port		= 3306
    basedir		= /usr
    datadir		= /var/lib/mysql
    tmpdir		= /tmp
    character_set_server=utf8   #添加此行
    lc-messages-dir	= /usr/share/mysql
    ......
    

    将下载后的mysql jdbc包解压

    #解压安装包
    hadoop@hadoop-master:~$ tar xf mysql-connector-java-5.1.40.tar.gz
    
    #将mysql-connector-java-5.1.40-bin.jar拷贝到/usr/local/hive/lib目录下
    hadoop@hadoop-master:~$ cp -a mysql-connector-java-5.1.40/mysql-connector-java-5.1.40-bin.jar /usr/local/hive/lib/
    

    新建hive数据库

    #这个hive数据库与hive-site.xml中localhost:3306/hive的hive对应,用来保存hive元数据
    mysql> create database hive;    
    
    #配置mysql允许hive接入:
    #将所有数据库的所有表的所有权限赋给hive用户,后面的hive是配置hive-site.xml中配置的连接密码
    mysql> grant all on *.* to hive@localhost identified by 'hive'; 
    
    #刷新mysql系统权限关系表
    mysql> flush privileges;  
    

    替换guava.jar包

    #查看hadoop安装目录下share/hadoop/common/lib内guava.jar版本
    hadoop@hadoop-master:~$ ll -d /usr/local/hadoop/share/hadoop/common/lib/guava-27.0-jre.jar 
    -rw-r--r-- 1 hadoop hadoop 2747878 9月  12  2019 /usr/local/hadoop/share/hadoop/common/lib/guava-27.0-jre.jar
    
    #查看hive安装目录下lib内guava.jar的版本
    hadoop@hadoop-master:~$ ll -d /usr/local/hive/lib/guava-19.0.jar 
    -rw-r--r-- 1 hadoop hadoop 2308517 9月  27  2018 /usr/local/hive/lib/guava-19.0.jar
    
    #如果两者不一致,删除版本低的,并拷贝高版本的
    hadoop@hadoop-master:~$ rm -rf  /usr/local/hive/lib/guava-19.0.jar 
    hadoop@hadoop-master:~$ cp -a /usr/local/hadoop/share/hadoop/common/lib/guava-27.0-jre.jar /usr/local/hive/lib/
    
    #重启数据库
    hadoop@hadoop-master:~$ sudo systemctl restart mysql
    

    使用schematool工具

    #Hive现在包含一个用于 Hive Metastore 架构操控的脱机工具,名为 schematool.此工具可用于初始化当前 Hive 版本的 Metastore 架构。此外,其还可处理从较旧版本到新版本的架构升级。
    
    hadoop@hadoop-master:~$ cd /usr/local/hive
    hadoop@hadoop-master:/usr/local/hive$ ./bin/schematool -dbType mysql -initSchema
    
    

    启动hive之前,请确保hadoop集群已启动

    #启动hive
    hadoop@hadoop-master:/usr/local/hive$ hive
    

    使用mysql作为元数据库时登陆使用mysql作为元数据库时登陆启动Hive过程中,可能出现的错误和解决方案如下:

    【错误1】

    【错误】
    java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument
    
    【原因】
    com.google.common.base.Preconditions.checkArgument 这是因为hive内依赖的guava.jar和hadoop内的版本不一致造成的。
    
    【解决方法】
    1.查看hadoop安装目录下share/hadoop/common/lib内guava.jar版本
    2.查看hive安装目录下lib内guava.jar的版本 如果两者不一致,删除版本低的,并拷贝高版本的 问题解决!
    

    【错误2】

    【错误】
    org.datanucleus.store.rdbms.exceptions.MissingTableException: Required table missing : “VERSION” in Catalog “” Schema “”. DataNucleus requires this table to perform its persistence operations.
    
    【解决方案】
    进入hive安装目录(比如/usr/local/hive),执行如下命令:./bin/schematool -dbType mysql -initSchema
    

    【错误3】

    【错误】
    在启动Hive时,有可能会出现Hive metastore database is not initialized的错误,这里给出解决方案。
    
    【原因】
    以前曾经安装了Hive或MySQL,重新安装Hive和MySQL以后,导致版本、配置不一致。
    
    【解决方案】
    使用schematool工具。Hive现在包含一个用于 Hive Metastore 架构操控的脱机工具,名为 schematool.此工具可用于初始化当前 Hive 版本的 Metastore 架构。此外,其还可处理从较旧版本到新版本的架构升级。所以,解决上述错误,你可以在终端执行如下命令:
    
    cd /usr/local/hive
    ./bin/schematool -dbType mysql -initSchema
    

    执行后,再启动Hive,应该就正常了。

    启动进入Hive的交互式执行环境以后,会出现如下命令提示符:

    hive>
    

    可以在里面输入SQL语句,如果要退出Hive交互式执行环境,可以输入如下命令:

    hive>exit;
    

    3、Hive的常用HiveQL操作

    3.1 Hive基本数据类型

    首先,我们简单叙述一下HiveQL的基本数据类型。

    Hive支持基本数据类型和复杂类型, 基本数据类型主要有数值类型(INT、FLOAT、DOUBLE ) 、布尔型和字符串, 复杂类型有三种:ARRAY、MAP 和 STRUCT。

    • 基本数据类型
      • TINYINT: 1个字节
      • SMALLINT: 2个字节
      • INT: 4个字节
      • BIGINT: 8个字节
      • BOOLEAN: TRUE/FALSE
      • FLOAT: 4个字节,单精度浮点型
      • DOUBLE: 8个字节,双精度浮点型STRING 字符串
    • 复杂数据类型
      • ARRAY: 有序字段
      • MAP: 无序字段
      • STRUCT: 一组命名的字段

    3.2 常用的HiveQL操作命令

    Hive常用的HiveQL操作命令主要包括:数据定义、数据操作。接下来详细介绍一下这些命令即用法。

    数据定义:主要用于创建修改和删除数据库、表、视图、函数和索引。

    创建、修改和删除数据库

    create database if not exists hive;       #创建数据库
    show databases;                           #查看Hive中包含数据库
    show databases like 'h.*';                #查看Hive中以h开头数据库
    describe databases;                       #查看hive数据库位置等信息
    alter database hive set dbproperties;     #为hive设置键值对属性
    use hive;                                 #切换到hive数据库下
    drop database if exists hive;             #删除不含表的数据库
    drop database if exists hive cascade;     #删除数据库和它中的表
    

    注意,除 dbproperties属性外,数据库的元数据信息都是不可更改的,包括数据库名和数据库所在的目录位置,没有办法删除或重置数据库属性。

    创建、修改和删除表

    #创建内部表(管理表)
    create table if not exists hive.usr(
          name string comment 'username',
          pwd string comment 'password',
          address struct<street:string,city:string,state:string,zip:int>,
          comment  'home address',
          identify map<int,tinyint> comment 'number,sex') 
          comment 'description of the table'  
         tblproperties('creator'='me','time'='2016.1.1'); 
    #创建外部表
    create external table if not exists usr2(
          name string,
          pwd string,
      address struct<street:string,city:string,state:string,zip:int>,
          identify map<int,tinyint>) 
          row format delimited fields terminated by ','
         location '/usr/local/hive/warehouse/hive.db/usr'; 
    #创建分区表
    create table if not exists usr3(
          name string,
          pwd string,
          address struct<street:string,city:string,state:string,zip:int>,
          identify map<int,tinyint>) 
          partitioned by(city string,state string);    
    #复制usr表的表模式  
    create table if not exists hive.usr1 like hive.usr;
     
    show tables in hive;  
    show tables 'u.*';        #查看hive中以u开头的表
    describe hive.usr;        #查看usr表相关信息
    alter table usr rename to custom;      #重命名表
     
    #为表增加一个分区
    alter table usr2 add if not exists 
         partition(city=”beijing”,state=”China”) 
         location '/usr/local/hive/warehouse/usr2/China/beijing'; 
    #修改分区路径
    alter table usr2 partition(city=”beijing”,state=”China”)
         set location '/usr/local/hive/warehouse/usr2/CH/beijing';
    #删除分区
    alter table usr2 drop if exists  partition(city=”beijing”,state=”China”)
    #修改列信息
    alter table usr change column pwd password string after address;
     
    alter table usr add columns(hobby string);                  #增加列
    alter table usr replace columns(uname string);              #删除替换列
    alter table usr set tblproperties('creator'='liming');      #修改表属性
    alter table usr2 partition(city=”beijing”,state=”China”)    #修改存储属性
    set fileformat sequencefile;             
    use hive;                                                   #切换到hive数据库下
    drop table if exists usr1;                                  #删除表
    drop database if exists hive cascade;                       #删除数据库和它中的表
    

    视图和索引的创建、修改和删除

    主要语法如下,用户可自行实现。

    create view view_name as....;                #创建视图
    alter view view_name set tblproperties(…);   #修改视图
    

    因为视图是只读的,所以 对于视图只允许改变元数据中的 tblproperties属性。

    #删除视图
    drop view if exists view_name;
    
    #创建索引
    create index index_name on table table_name(partition_name/column_name)  
    as 'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler' with deferred rebuild....; 
    

    这里org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler是一个索引处理器,即一个实现了索引接口的Java类,另外Hive还有其他的索引实现。

    alter index index_name on table table_name partition(...)  rebulid;   #重建索引
    

    如果使用 deferred rebuild,那么新索引成空白状态,任何时候可以进行第一次索引创建或重建。

    show formatted index on table_name;                       #显示索引
    drop index if exists index_name on table table_name;      #删除索引
    

    用户自定义函数

    在新建用户自定义函数(UDF)方法前,先了解一下Hive自带的那些函数。show functions; 命令会显示Hive中所有的函数名称:

    hive> show functions;
    OK
    !
    !=
    $sum0
    %
    &
    *
    ......
    

    若想要查看具体函数使用方法可使用describe function函数名:

    hive> describe function abs;
    OK
    abs(x) - returns the absolute value of x
    Time taken: 0.027 seconds, Fetched: 1 row(s)
    

    首先编写自己的UDF前需要继承UDF类并实现evaluate()函数,或是继承GenericUDF类实现initialize()函数、evaluate()函数和getDisplayString()函数,还有其他的实现方法,感兴趣的用户可以自行学习。

    另外,如果用户想在Hive中使用该UDF需要将我们编写的Java代码进行编译,然后将编译后的UDF二进制类文件(.class文件)打包成一个JAR文件,然后在Hive会话中将这个JAR文件加入到类路径下,在通过create function语句定义好使用这个Java类的函数。

    add jar <jar文件的绝对路径>;                         #创建函数
    create temporary function function_name;
    drop temporary function if exists function_name;    #删除函数
    

    3.3 数据操作

    主要实现的是将数据装载到表中(或是从表中导出),并进行相应查询操作,对熟悉SQL语言的用户应该不会陌生。

    向表中装载数据

    这里我们以只有两个属性的简单表为例来介绍。首先创建表stu和course,stu有两个属性id与name,course有两个属性cid与sid。

    #创建hive库
    hive> create database if not exists hive;
    OK
    Time taken: 0.186 seconds
    
    #创建表stu
    hive> create table stu(id int,name string) row format delimited fields terminated by ' ';
    OK
    Time taken: 0.082 seconds
    
    #创建表course
    hive> create table course(cid int,sid int) row format delimited fields terminated by ' ';
    OK
    Time taken: 0.044 seconds
    

    向表中装载数据有两种方法:从文件中导入和通过查询语句插入。

    1、从文件中导入
    假如这个表中的记录存储于文件stu.txt中,内容如下。

    hadoop@hadoop-master:~$ mkdir -p /usr/local/hadoop/examples
    hadoop@hadoop-master:~$ vim /usr/local/hadoop/examples/stu.txt
    hadoop@hadoop-master:~$ cat /usr/local/hadoop/examples/stu.txt
    1 xiapi 
    2 xiaoxue 
    3 qingqing
    

    下面我们把这个文件中的数据装载到表stu中,操作如下:

    hive> use hive;
    OK
    Time taken: 0.027 seconds
    
    hive> load data local inpath '/usr/local/hadoop/examples/stu.txt' overwrite into table stu;
    Loading data to table hive.stu
    OK
    Time taken: 0.173 seconds
    
    hive> select * from stu;
    OK
    1	xiapi
    2	xiaoxue
    3	qingqing
    Time taken: 0.096 seconds, Fetched: 3 row(s)
    

    如果stu.txt文件存储在HDFS 上,则不需要 local 关键字。

    2、通过查询语句插入

    使用如下命令,创建stu1表,它和stu表属性相同,我们要把从stu表中查询得到的数据插入到stu1中:

    hive> create table stu1 as select id,name from stu;
    

    上面是创建表,并直接向新表插入数据;若表已经存在,向表中插入数据需执行以下命令:

    insert overwrite table stu1 select id,name from stu where(条件);
    

    这里关键字overwrite的作用是替换掉表(或分区)中原有数据,换成into关键字,直接追加到原有内容后。

    从表中导出数据

    1、可以简单拷贝文件或文件夹

    命令如下:

    hadoop  fs -cp source_path target_path;
    

    2、写入临时文件

    命令如下:

    hive> insert overwrite local directory '/usr/local/hadoop/tmp/stu' select id,name from stu;
    

    查询操作

    和SQL的查询完全一样,这里不再赘述。主要使用select…from…where…等语句,再结合关键字group by、having、like、rlike等操作。这里我们简单介绍一下SQL中没有的case…when…then…句式、join操作和子查询操作。

    case…when…then…句式和if条件语句类似,用于处理单个列的查询结果,语句如下:

    select id,name,
      case 
      when id=1 then 'first' 
      when id=2 then 'second'
      else 'third'
    

    结果如下:

    hive> select id,name,case when id=1 then 'first' when id=2 then 'second' else 'third' end from stu;
    OK
    1	xiapi	first
    2	xiaoxue	second
    3	qingqing	third
    Time taken: 0.108 seconds, Fetched: 3 row(s)
    

    连接
    连接(join)是将两个表中在共同数据项上相互匹配的那些行合并起来, HiveQL 的连接分为内连接、左向外连接、右向外连接、全外连接和半连接 5 种。

    1、内连接(等值连接)
    内连接使用比较运算符根据每个表共有的列的值匹配两个表中的行。

    首先,我们先把以下内容插入到course表中(自行完成)

    hive> select * from course;
    OK
    1	3
    2	1
    3	1
    Time taken: 0.098 seconds, Fetched: 3 row(s)
    

    下面查询stu和course表中学号相同的所有行,命令如下:

    hive> select stu.*, course.* from stu join course on(stu .id=course .sid);
    ......
    OK
    1	xiapi	2	1
    1	xiapi	3	1
    3	qingqing	1	3
    Time taken: 19.167 seconds, Fetched: 3 row(s)
    

    2、左连接
    左连接的结果集包括“LEFT OUTER”子句中指定的左表的所有行, 而不仅仅是连接列所匹配的行。如果左表的某行在右表中没有匹配行, 则在相关联的结果集中右表的所有选择列均为空值,命令如下:

    hive> select stu.*, course.* from stu left outer join course on(stu .id=course .sid);
    ....
    OK
    1	xiapi	2	1
    1	xiapi	3	1
    2	xiaoxue	NULL	NULL
    3	qingqing	1	3
    Time taken: 18.285 seconds, Fetched: 4 row(s)
    

    3、右连接
    右连接是左向外连接的反向连接,将返回右表的所有行。如果右表的某行在左表中没有匹配行,则将为左表返回空值。命令如下:

    hive> select stu.*, course.* from stu right outer join course on(stu .id=course .sid);
    ....
    OK
    3	qingqing	1	3
    1	xiapi	2	1
    1	xiapi	3	1
    Time taken: 17.139 seconds, Fetched: 3 row(s)
    

    4、全连接
    全连接返回左表和右表中的所有行。当某行在另一表中没有匹配行时,则另一个表的选择列表包含空值。如果表之间有匹配行,则整个结果集包含基表的数据值。命令如下:

    hive> select stu.*, course.* from stu full outer join course on(stu .id=course .sid);
    ....
    OK
    1	xiapi	3	1
    1	xiapi	2	1
    2	xiaoxue	NULL	NULL
    3	qingqing	1	3
    Time taken: 16.741 seconds, Fetched: 4 row(s)
    

    5、半连接
    半连接是 Hive 所特有的, Hive 不支持 in 操作,但是拥有替代的方案; left semi join, 称为半连接, 需要注意的是连接的表不能在查询的列中,只能出现在 on 子句中。命令如下:

    hive> select stu.* from stu left semi join course on(stu .id=course .sid);
    ....
    OK
    1	xiapi
    3	qingqing
    Time taken: 17.892 seconds, Fetched: 2 row(s)
    

    子查询
    标准 SQL 的子查询支持嵌套的 select 子句,HiveQL 对子查询的支持很有限,只能在from 引导的子句中出现子查询。

    注意,在定义或是操作表时,不要忘记指定所需数据库。

    4、Hive简单编程实践

    下面我们以词频统计算法为例,来介绍怎么在具体应用中使用Hive。词频统计算法又是最能体现MapReduce思想的算法之一,这里我们可以对比它在MapReduce中的实现,来说明使用Hive后的优势。

    MapReduce实现词频统计的代码可以通过下载Hadoop源码后,在 $HADOOP_HOME/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar 包中找到(wordcount类),wordcount类由63行Java代码编写而成。下面首先简单介绍一下怎么使用MapReduce中wordcount类来统计单词出现的次数,具体步骤如下:

    1、创建input目录,output目录会自动生成。其中input为输入目录,output目录为输出目录。命令如下:

    hadoop@hadoop-master:~$ hdfs dfs -mkdir input
    

    2、然后,在input文件夹中创建两个测试文件file1.txt和file2.txt,命令如下:

    hadoop@hadoop-master:~$ echo "hello world" > file1.txt
    hadoop@hadoop-master:~$ echo "hello hadoop" > file2.txt
    

    3、上传

    hadoop@hadoop-master:~$ hdfs dfs -put /home/hadoop/file* input/
    
    hadoop@hadoop-master:~$ hdfs dfs -ls input/
    Found 2 items
    -rw-r--r--   1 hadoop supergroup         12 2022-04-25 16:02 input/file1.txt
    -rw-r--r--   1 hadoop supergroup         13 2022-04-25 16:02 input/file2.txt
    

    4、执行如下hadoop命令:

    hadoop@hadoop-master:~$ cd /usr/local/hadoop/
    
    hadoop@hadoop-master:/usr/local/hadoop$ hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar wordcount input output
    

    5、我们可以到output文件夹中查看结果,结果如下:

    hadoop@hadoop-master:/usr/local/hadoop$ hdfs dfs -cat output/*
    hadoop	1
    hello	2
    world	1
    

    下面我们通过HiveQL实现词频统计功能,此时只要编写下面7行代码,而且不需要进行编译生成jar来执行。HiveQL实现命令如下:

    hive> create table docs(line string);
    OK
    
    hive> load data inpath 'file:///usr/local/hadoop/input' overwrite into table docs;
    Loading data to table hive.docs
    OK
    Time taken: 0.65 seconds
    
    hive> select * from docs;
    OK
    hello world
    hello hadoop
    Time taken: 1.06 seconds, Fetched: 2 row(s)
    
    hive> create table word_count as 
        > select word, count(1) as count from
        > (select explode(split(line,' '))as word from docs) w
        > group by word
        > order by word;
        > order by word;
        
    hive> select * from word_count;
    OK
    hadoop	1
    hello	2
    world	1
    Time taken: 0.117 seconds, Fetched: 3 row(s)
    

    由上可知,采用Hive实现最大的优势是,对于非程序员,不用学习编写Java MapReduce代码了,只需要用户学习使用HiveQL就可以了,而这对于有SQL基础的用户而言是非常容易的。

    本文参考:http://dblab.xmu.edu.cn/blog/2440-2/

  • 相关阅读:
    Tomcat的SessionID引起的Session Fixation和Session Hijacking问题
    别把项目成功当目标!——项目经理的误区(1)(转)
    技术,项目经理的命?——项目经理的误区(3)(转)
    项目管理中,最难管的是什么?(转)
    项目经理的超越(三)人际优先,做事上的超越(转)
    大丈夫不可一日无权啊!——项目经理的误区(2)(转)
    舌尖上的职场(二)一起去吃饭吧!(转)
    舌尖上的职场(一)你最近还好吗?(转)
    【项目经理之修炼(10)】《初级篇》人际关系问题也是可以避免的(转)
    gcc与g++区别
  • 原文地址:https://www.cnblogs.com/ywb123/p/16190812.html
Copyright © 2020-2023  润新知