Hive 从 0 到 1 学习 —— 第二章 Hive 安装

本文详细介绍了在Hadoop集群上安装和配置Hive的步骤,包括下载与解压Hive,配置Hadoop环境,创建HDFS目录,启动Hive,以及导入数据到Hive表中。此外,还讲解了遇到的多客户端连接问题,解决方案是将Hive元数据存储到MySQL,并展示了如何通过JDBC访问Hive以及使用DBeaver连接Hive。最后,文章提到了Hive的一些常用交互命令和配置选项。
摘要由CSDN通过智能技术生成

1. Hive 安装地址

  1. Hive官网地址

    http://hive.apache.org/

  2. 文档查看地址

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

  3. 下载地址

    http://archive.apache.org/dist/hive/

  4. github地址

    https://github.com/apache/hive

2. Hive 安装部署

2.1 Hive 安装及配置

  1. 把 apache-hive-1.2.1-bin.tar.gz上传到 linux 的 /opt/software目录下

  2. 解压 apache-hive-1.2.1-bin.tar.gz 到 /opt/module/目录下面

    [atguigu@hadoop102 software]$ tar -zxvf apache-hive-1.2.1-bin.tar.gz -C /opt/module/
    
  3. 修改 apache-hive-1.2.1-bin.tar.gz 的名称为 hive

    [atguigu@hadoop102 module]$ mv apache-hive-1.2.1-bin/ hive
    
  4. 修改 /opt/module/hive/conf目录下的 hive-env.sh.template名称为 hive-env.sh

  5. 配置 hive-env.sh文件

    配置 HADOOP_HOME路径

    export HADOOP_HOME=/opt/module/hadoop-2.7.2
    

    配置 HIVE_CONF_DIR路径

    export HIVE_CONF_DIR=/opt/module/hive/conf
    

2.2 Hadoop 集群配置

  1. 必须启动 hdfs 和 yarn

    [atguigu@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh
    [atguigu@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh
    
  2. 在 HDFS 上创建 /tmp/user/hive/warehouse两个目录并修改他们的同组权限可写

    [atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -mkdir /tmp
    [atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -mkdir -p /user/hive/warehouse
    
    [atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /tmp
    [atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs -chmod g+w /user/hive/warehouse
    

2.3 Hive基本操作

  1. 启动 hive

    [atguigu@hadoop102 hive]$ bin/hive
    
  2. 查看数据库

    hive> show databases;
    
  3. 打开默认数据库

    hive> use default;
    
  4. 显示 default 数据库中的表

    hive> show tables;
    
  5. 创建一张表

    hive> create table student(id int, name string);
    
  6. 显示数据库中有几张表

    hive> show tables;
    
  7. 查看表的结构

    hive> desc student;
    
  8. 向表中插入数据

    hive> insert into student values(1000,"ss");
    
  9. 查询表中数据

    hive> select * from student;
    
  10. 退出 hive

    hive> quit;
    

3. 将本地文件导入 Hive 案例

需求

将本地 /opt/module/datas/student.txt这个目录下的数据导入到 hive 的 student(id int, name string)表中。

3.1 数据准备

/opt/module/datas这个目录下准备数据

[atguigu@hadoop102 module]$ mkdir datas
[atguigu@hadoop102 module]$ cd datas
[atguigu@hadoop102 module]$ vim student.txt
1001	大明
1002	二明
1003	三明

注意以 tab 键间隔。

3.2 Hive 实际操作

  1. 启动 hive

    [atguigu@hadoop102 hive]$ bin/hive
    
  2. 显示数据库

    hive> show databases;
    
  3. 使用 default 数据库

    hive> use default;
    
  4. 显示 default 数据库中的表

    hive> show tables;
    
  5. 删除已创建的 student 表

    hive> drop table if exists student;
    
  6. 创建 student 表, 并声明文件分隔符 ’\t’

    hive> create table student(id int, name string) ROW FORMAT DELIMITED FIELDS TERMINATED
     BY '\t';
    
  7. 加载 /opt/module/datas/student.txt文件到 student 数据库表中。

    hive> load data local inpath '/opt/module/datas/student.txt' into table student;
    
  8. Hive 查询结果

    hive> select * from student;
    OK
    1001	大明
    1002	二明
    1003	三明
    Time taken: 0.289 seconds, Fetched: 3 row(s)
    

3.3 遇到的问题

再打开一个客户端窗口启动 hive,会产生 java.sql.SQLException 异常。

Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException:
 Unable to instantiate
 org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
        at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:522)
        at org.apache.hadoop.hive.cli.CliDriver.run(CliDriver.java:677)
        at org.apache.hadoop.hive.cli.CliDriver.main(CliDriver.java:621)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
        at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.ql.metadata.SessionHiveMetaStoreClient
        at org.apache.hadoop.hive.metastore.MetaStoreUtils.newInstance(MetaStoreUtils.java:1523)
        at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.<init>(RetryingMetaStoreClient.java:86)
        at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:132)
        at org.apache.hadoop.hive.metastore.RetryingMetaStoreClient.getProxy(RetryingMetaStoreClient.java:104)
        at org.apache.hadoop.hive.ql.metadata.Hive.createMetaStoreClient(Hive.java:3005)
        at org.apache.hadoop.hive.ql.metadata.Hive.getMSC(Hive.java:3024)
        at org.apache.hadoop.hive.ql.session.SessionState.start(SessionState.java:503)
... 8 more

原因是,Metastore 默认存储在自带的 derby 数据库中,推荐使用 MySQL 存储 Metastore。

4. Hive 元数据配置到 MySql

4.1 驱动拷贝

拷贝 mysql 的驱动到 /opt/moudule/hive/lib

[root@hadoop102 software]# cp mysql-libs/mysql-connector-java-5.1.44.jar
 /opt/module/hive/lib/

4.2 配置 Metastore 到 MySql

  1. /opt/module/hive/conf目录下创建一个 hive-site.xml

  2. 根据官方文档配置参数,拷贝数据到 hive-site.xml文件中

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
    <configuration>
        <property>
            <name>javax.jdo.option.ConnectionURL</name>
            <value>jdbc:mysql://hadoop102:3306/metastore?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=UTF-8</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>123456</value>
            <description>password to use against metastore database</description>
        </property>
        <property>
            <name>hive.metastore.schema.verification</name>
            <value>false</value>
            <description>关闭元数据检查</description>
        </property>
    </configuration>
    
  3. 配置完毕后,如果启动 hive 异常,可以重新启动虚拟机。(重启后,别忘了启动 hadoop 集群)

4.3 多窗口启动 Hive 测试

  1. 先启动 MySQL

    [dwjf321@hadoop102 hive]$ mysql -uroot -p123456
    

    查看有几个数据库

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql             |
    | performance_schema |
    | test               |
    +--------------------+
    
  2. 再次打开多个窗口,分别启动 hive

    [dwjf321@hadoop102 hive]$ bin/hive
    
  3. 启动 hive 后,回到 MySQL 窗口查看数据库,显示增加了 metastore 数据库

    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | metastore          |
    | mysql             |
    | performance_schema |
    | test               |
    +--------------------+
    

5. HiveJDBC 访问

5.1 启动 hiveserver2 服务

nohup /opt/module/hive/bin/hiveserver2 >/dev/null 2>&1 &

5.2 连接 hiveserver2

beeline> !connect jdbc:hive2://hadoop102:10000(回车)
Connecting to jdbc:hive2://hadoop102:10000
Enter username for jdbc:hive2://hadoop102:10000: dwjf321(回车)
Enter password for jdbc:hive2://hadoop102:10000: (直接回车)
Connected to: Apache Hive (version 1.2.1)
Driver: Hive JDBC (version 1.2.1)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://hadoop102:10000> show databases;
+----------------+--+
| database_name  |
+----------------+--+
| default        |
+----------------+--+

5.3 DBeaver 连接 hive

  1. core-site.xml添加配置

    vim /opt/module/hadoop-2.7.2/etc/hadoop/core-site.xml
    

    内容如下:

    <property>
            <name>hadoop.proxyuser.root.hosts</name>
            <value>*</value>
     </property>
    <property>
            <name>hadoop.proxyuser.root.groups</name>
        <value>*</value>
    </property>
    <property>
            <name>hadoop.proxyuser.zhaoshb.hosts</name>
            <value>*</value>
    </property>
    <property>
            <name>hadoop.proxyuser.zhaoshb.groups</name>
            <value>*</value>
    </property>
    
  2. 连接 hive
    在这里插入图片描述

6. Hive 常用交互命令

[dwjf321@hadoop102 hive]$ bin/hive -help
usage: hive
 -d,--define <key=value>          Variable subsitution to apply to hive
                                  commands. e.g. -d A=B or --define A=B
    --database <databasename>     Specify the database to use
 -e <quoted-query-string>         SQL from command line
 -f <filename>                    SQL from files
 -H,--help                        Print help information
    --hiveconf <property=value>   Use value for given property
    --hivevar <key=value>         Variable subsitution to apply to hive
                                  commands. e.g. --hivevar A=B
 -i <filename>                    Initialization SQL file
 -S,--silent                      Silent mode in interactive shell
 -v,--verbose                     Verbose mode (echo executed SQL to the console)
  1. -e不进入 hive 的交互窗口执行 sql 语句

    [dwjf321@hadoop102 hive]$ bin/hive -e "select id from student;"
    
  2. -f执行脚本中sql语句

    [dwjf321@hadoop102 datas]$ touch hivef.sql
    

    文件中写入正确的sql语句

    select *from student;
    

    执行文件中的 sql 语句

    [dwjf321@hadoop102 hive]$ bin/hive -f /opt/module/datas/hivef.sql  > /opt/module/datas/hive_result.txt
    

7. Hive 其他命令操作

  1. 退出hive窗口:

    hive(default)>exit;
    

    hive(default)>quit;
    

    在新版的hive中没区别了,在以前的版本是有的:

    • exit:先隐性提交数据,再退出;

    • quit:不提交数据,退出;

  2. hive cli命令窗口中如何查看hdfs文件系统

    hive(default)>dfs -ls /;
    
  3. hive cli命令窗口中如何查看本地文件系统

    hive(default)>! ls /opt/module/datas;
    
  4. 查看在 hive 中输入的所有历史命令

    1. 进入到当前用户的根目录 /root/home/dwjf321

    2. 查看 . hivehistory文件

      [dwjf312@hadoop102 ~]$ cat .hivehistory
      

8. Hive 常见属性配置

8.1 Hive 数据仓库位置配置

  1. Default数据仓库的最原始位置是在hdfs上的:/user/hive/warehouse路径下。

  2. 在仓库目录下,没有对默认的数据库 default 创建文件夹。如果某张表属于default数据库,直接在数据仓库目录下创建一个文件夹。

  3. 修改default数据仓库原始位置

    vim hive-site.xml
    

    添加如下内容

    <property>
        <name>hive.metastore.warehouse.dir</name>
        <value>/user/hive/warehouse</value>
        <description>location of default database for the warehouse</description></property>
    

    配置同组用户有执行权限

    bin/hdfs dfs -chmod g+w /user/hive/warehouse
    

8.2 查询后信息显示配置

  1. hive-site.xml文件中添加如下配置信息,就可以实现显示当前数据库,以及查询表的头信息配置。

    <property>
    	<name>hive.cli.print.header</name>
    	<value>true</value>
    </property>
    <property>
    	<name>hive.cli.print.current.db</name>
    	<value>true</value>
    </property>
    

    配置后,如图所示:

    在这里插入图片描述

8.3 Hive 运行日志信息配置

  1. Hive 的 log 默认存放在 /tmp/dwjf321/hive.log目录下(当前用户名下)

  2. 修改hive 的 log 存放日志到 /opt/module/hive/logs

    1. 修改 /opt/module/hive/conf/hive-log4j.properties.template文件名称为 hive-log4j.properties

      [dwjf321@hadoop102 conf]$ mv hive-log4j.properties.template hive-log4j.properties
      
    2. hive-log4j.properties文件中修改log存放位置

      hive.log.dir=/opt/module/hive/logs
      

8.4 参数配置方式

  1. 查看当前所有的配置信息

    hive>set;
    
8.4.1 参数的配置三种方式
8.4.1.1 配置文件方式

默认配置文件:hive-default.xml

用户自定义配置文件:hive-site.xml

注意:用户自定义配置会覆盖默认配置。另外,Hive 也会读入Hadoop 的配置,因为 Hive 是作为 Hadoop 的客户端启动的,Hive 的配置会覆盖 Hadoop 的配置。配置文件的设定对本机启动的所有 Hive进程都有效。

8.4.1.2 命令行参数方式

启动Hive时,可以在命令行添加 -hiveconf param=value来设定参数。

例如:

[dwjf31@hadoop103 hive]$ bin/hive -hiveconf mapred.reduce.tasks=10;

注意:仅对本次hive启动有效

查看参数设置:

hive (default)> set mapred.reduce.tasks;
8.4.1.3 参数声明方式

可以在 HQL 中使用 SET关键字设定参数

例如:

hive (default)> set mapred.reduce.tasks=100;

注意:仅对本次 hive 启动有效。

查看参数设置

hive (default)> set mapred.reduce.tasks;

上述三种设定方式的优先级依次递增。即 配置文件 < 命令行参数 < 参数声明。注意某些系统级的参数,例如log4j相关的设定,必须用前两种方式设定,因为那些参数的读取在会话建立以前已经完成了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值