Hive 部署
鸡汤: 生活中总有诸般不如意,但是这并不足以成为我们的阻碍我们的梦。
1. 介绍¶
1.1 引入原因¶
- 对存在HDFS上的文件或HBase中的表进行查询时,是要手工写一堆MapReduce代码
- 对于统计任务,只能由动MapReduce的程序员才能搞定
- 耗时耗力,更多精力没有有效的释放出来
- Hive基于一个统一的查询分析层,通过SQL语句的方式对HDFS上的数据进行查询、统计和分析
1.2 hive是什么¶
- Hive是一个SQL解析引擎,将SQL语句转译成MR Job,然后再Hadoop平台上运行,达到快速开发的目的。
- Hive中的表是纯逻辑表,就只是表的定义等,即表的元数据。本质就是Hadoop的目录/文件, 达到了元数据与数据存储分离的目的
- Hive本身不存储数据,它完全依赖HDFS和MapReduce。
- Hive的内容是读多写少,不支持对数据的改写和删除
- Hive中没有定义专门的数据格式,由用户指定,需要指定三个属性:
- 列分隔符
- 行分隔符
- 读取文件数据的方法
2. 部署¶
2.1 默认配置¶
默认情况下,Hive的元数据信息存储在内置的Derby数据中。 Hive支持将元数据存储在MySQL中。
元数据存储配置
- 【本地配置1】:默认
- 【本地配置2】:本地搭建mysql,通过localhost:Port方式访问
- 【远程配置】:远程搭建mysql,通过IP:Port方式访问
2.2 安装mysql¶
[root@master ~]# wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm [root@master ~]# yum -y install mysql57-community-release-el7-10.noarch.rpm [root@master ~]# yum -y install mysql-community-server [root@master ~]# systemctl start mysqld [root@master ~]# systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2019-06-24 16:29:27 CST; 8s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 32960 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS ( code=exited, status=0/SUCCESS) Process: 32886 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 32963 (mysqld) CGroup: /system.slice/mysqld.service └─32963 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid Jun 24 16:29:24 master systemd[1]: Starting MySQL Server... Jun 24 16:29:27 master systemd[1]: Started MySQL Server.
找到mysql的默认密码
[root@master ~]# grep "password" /var/log/mysqld.log 2019-06-24T08:29:24.952507Z 1 [Note] A temporary password is generated for root@localhost: JHIDn36Ikb>e 2019-06-24T08:29:54.981672Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO) 2019-06-24T08:29:58.562555Z 3 [Note] Access denied for user 'root'@'localhost' (using password: YES) 2019-06-24T08:30:29.996028Z 4 [Note] Access denied for user 'root'@'localhost' (using password: YES) 2019-06-24T08:30:31.719812Z 5 [Note] Access denied for user 'root'@'localhost' (using password: NO)
修改密码
[root@master ~]# mysql -uroot -p'JHIDn36Ikb>e' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 5.7.26 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> set global validate_password_policy=0; # 关闭弱密码检查 Query OK, 0 rows affected (0.00 sec) mysql> set global validate_password_length=1; # 关闭密码长度检查 Query OK, 0 rows affected (0.00 sec) mysql> SET PASSWORD = PASSWORD('root'); Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) 开放root远程连接 mysql> grant all privileges on *.* to root@'%' identified by "root"; Query OK, 0 rows affected, 1 warning (0.03 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
其他机器安装mysql客户端
[root@slave1 ~]# yum install -y mysql [root@slave2 ~]# yum install -y mysql 测试mysql远程连接 [root@slave1 ~]# mysql -uroot -proot -hmaster Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 15 Server version: 5.7.26 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]>
说明可以远程连接,其他客户端安装的目的,就是让其他客户端,也可以mysql链接到master上的mysql,若是不连接,其他客户端就可以不用安装。
3.3 hive安装¶
Hive其实就是一个客户端,下载、解压、配置、启动就可以直接链接了。 下载地址
http://mirror.bit.edu.cn/apache/hive/
安装
[root@master ~]# cd /usr/local/src/ [root@master src]# wget http://mirror.bit.edu.cn/apache/hive/hive-1.2.2/apache-hive-1.2.2-bin.tar.gz [root@master src]# tar zxvf apache-hive-1.2.2-bin.tar.gz [root@master src]# mv apache-hive-1.2.2-bin /usr/local/ [root@master src]# cd /usr/local/ [root@master local]# ln -sf apache-hive-1.2.2-bin/ hive [root@master local]# cd hive/conf/ [root@master conf]# ls beeline-log4j.properties.template hive-env.sh.template hive-log4j.properties.template hive-default.xml.template hive-exec-log4j.properties.template ivysettings.xml
新建配置文件hive-site.xml
[默认没有]
[root@master conf]# cat hive-site.xml <configuration> <property> <name>javax.jdo.option.ConnectionURL</name> <value>jdbc:mysql://master:3306/hive?createDatabaseIfNotExist=true&useSSL=false</value> </property> <property> <name>javax.jdo.option.ConnectionDriverName</name> <value>com.mysql.jdbc.Driver</value> </property> <property> <name>javax.jdo.option.ConnectionUserName</name> <value>root</value> </property> <property> <name>javax.jdo.option.ConnectionPassword</name> <value>root</value> </property> </configuration>
解释:
1. javax.jdo.option.ConnectionURL 是配置连接mysql,我这是连本地。远程连接就写ip。还有端口号 2. javax.jdo.option.ConnectionDriverName 连接mysql的java的jdbc的驱动 3. javax.jdo.option.ConnectionUserName 连接mysql的用户名 4. javax.jdo.option.ConnectionPassword 连接mysql root用户对应的密码 5. &useSSL=false 要去掉否则会报ssl 警告
下载jdbc驱动,下载地址
http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/Connector-J
操作
[root@master lib]# cd [root@master ~]# cd /usr/local/src/ [root@master src]# wget http://mirrors.ustc.edu.cn/mysql-ftp/Downloads/Connector-J/mysql-connector-java-5.1.46.tar.gz [root@master src]# tar xf mysql-connector-java-5.1.46.tar.gz [root@master src]# mv mysql-connector-java-5.1.46/mysql-connector-java-5.1.46.jar /usr/local/hive/lib/
3.4 配置环境变量¶
[root@master ~]# tail -2 /etc/profile export HIVE_HOME=/usr/local/hive export PATH=$HIVE_HOME/bin:$PATH [root@master ~]# source /etc/profile [root@master ~]# which hive /usr/local/hive/bin/hive
3.5 避坑¶
[root@master ~]# cp /usr/local/hive/lib/jline-2.12.jar /usr/local/hadoop-2.6.5/share/hadoop/yarn/lib/ [root@master conf]# ll /usr/local/hadoop-2.6.5/share/hadoop/yarn/lib/|grep jline -rw-rw-r-- 1 1000 1000 87325 Oct 3 2016 jline-0.9.94.jar -rw-r--r-- 1 root root 213854 Jun 24 17:30 jline-2.12.jar [root@master conf]# cd /usr/local/hadoop-2.6.5/share/hadoop/yarn/lib/ [root@master lib]# mv jline-0.9.94.jar /tmp/
要把之前的老的包去掉,否则影响启动
3.5 包分发和配置环境¶
[root@master ~]# rsync -az /usr/local/{hive,apache-hive-1.2.2-bin} slave1:/usr/local/ [root@master ~]# rsync -az /usr/local/{hive,apache-hive-1.2.2-bin} slave2:/usr/local/ [root@slave1 ~]# tail -2 /etc/profile export HIVE_HOME=/usr/local/hive export PATH=$HIVE_HOME/bin:$PATH [root@slave1 ~]# source /etc/profile [root@slave1 ~]# which hive /usr/local/hive/bin/hive [root@slave2 ~]# tail -2 /etc/profile export HIVE_HOME=/usr/local/hive export PATH=$HIVE_HOME/bin:$PATH [root@slave2 ~]# source /etc/profile [root@slave2 ~]# which hive /usr/local/hive/bin/hive
Hive就是客户端,你想在哪个机器上使用Hive,就在哪个机器上安装hive
3.6 启动¶
[root@master ~]# hive Logging initialized using configuration in jar:file:/usr/local/apache-hive-1.2.2-bin/lib/hive-common-1.2.2.jar!/hive-log4j.properties hive> show tables; OK Time taken: 0.349 seconds
3.7 自检¶
[root@master ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 65 Server version: 5.7.26 MySQL Community Server (GPL) Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. corresponds to your MySQL server version for the right syntax to use near 'databases;' at line 1 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | hive | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.01 sec) mysql> use hive; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +---------------------------+ | Tables_in_hive | +---------------------------+ | BUCKETING_COLS | | CDS | | COLUMNS_V2 | | DATABASE_PARAMS | | DBS | | FUNCS | | FUNC_RU | | GLOBAL_PRIVS | | PARTITIONS | | PARTITION_KEYS | | PARTITION_KEY_VALS | | PARTITION_PARAMS | | PART_COL_STATS | | ROLES | | SDS | | SD_PARAMS | | SEQUENCE_TABLE | | SERDES | | SERDE_PARAMS | | SKEWED_COL_NAMES | | SKEWED_COL_VALUE_LOC_MAP | | SKEWED_STRING_LIST | | SKEWED_STRING_LIST_VALUES | | SKEWED_VALUES | | SORT_COLS | | TABLE_PARAMS | | TAB_COL_STATS | | TBLS | | VERSION | +---------------------------+ 29 rows in set (0.00 sec)
hive启动后会把Hive的元数据存放在MySQL中。
4. 基本使用¶
4.1 创建内部表¶
[root@master hive_test]# cat create_table.sql create TABLE u_info ( usrid STRING, age STRING, sex STRING ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n';
详细操作过程
hive> show tables; OK Time taken: 0.705 seconds hive> create TABLE u_info > ( > usrid STRING, > age STRING, > sex STRING > ) > ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' > LINES TERMINATED BY '\n'; OK Time taken: 0.36 seconds hive> show tables; OK u_info Time taken: 0.081 seconds, Fetched: 1 row(s) hive> show tables; OK u_info Time taken: 0.081 seconds, Fetched: 1 row(s) hive> desc u_info; OK usrid string age string sex string Time taken: 0.181 seconds, Fetched: 3 row(s)
新建表的是,会在hdfs上创建文件/user/hive/warehouse
[root@master ~]# hadoop fs -ls / Found 4 items drwxr-xr-x - root supergroup 0 2019-06-24 11:01 /hbase -rw-r--r-- 3 root supergroup 225 2019-06-23 17:13 /hosts drwx-wx-wx - root supergroup 0 2019-06-24 17:14 /tmp drwxr-xr-x - root supergroup 0 2019-06-24 19:03 /user [root@master ~]# hadoop fs -ls /user/ Found 1 items drwxr-xr-x - root supergroup 0 2019-06-24 19:03 /user/hive [root@master ~]# hadoop fs -ls /user/hive Found 1 items drwxr-xr-x - root supergroup 0 2019-06-24 19:03 /user/hive/warehouse
外部导入数据语法
#hive -e "LOAD DATA LOCAL INPATH '/opt/a.txt' OVERWRITE INTO TABLE table_aname" #hive -e "LOAD DATA LOCAL INPATH '/opt/b.txt' OVERWRITE INTO TABLE table_aname"
详细操作
[root@master hive_test]# cat /root/hive/hive_test/a.txt user1 27 1 user2 28 1 user3 29 0 user4 30 1 user5 31 0 user6 32 1 user7 33 1 user8 34 0 hive> LOAD DATA LOCAL INPATH '/root/hive/hive_test/a.txt' OVERWRITE INTO TABLE u_info; Loading data to table default.u_info Table default.u_info stats: [numFiles=1, numRows=0, totalSize=88, rawDataSize=0] OK Time taken: 0.469 seconds
默认是default库,
文件在本地就是
LOAD DATA LOCAL INPATH '/root/hive/hive_test/a.txt' OVERWRITE INTO TABLE u_info;
文件在hdfs就去掉LOCAL
LOAD DATA INPATH '/root/hive/hive_test/a.txt' OVERWRITE INTO TABLE u_info;
查看
hive> show tables; OK u_info Time taken: 0.02 seconds, Fetched: 1 row(s) hive> select * from u_info; OK user1 27 1 user2 28 1 user3 29 0 user4 30 1 user5 31 0 user6 32 1 user7 33 1 user8 34 0 Time taken: 0.293 seconds, Fetched: 8 row(s)
hive数据会存在hadoop里面
[root@master ~]# hadoop fs -text /user/hive/warehouse/u_info/a.txt user1 27 1 user2 28 1 user3 29 0 user4 30 1 user5 31 0 user6 32 1 user7 33 1 user8 34 0
4.2 删除表¶
hive> show tables; OK u_info Time taken: 0.093 seconds, Fetched: 1 row(s) hive> drop table u_info; OK Time taken: 0.711 seconds hive> show tables; OK Time taken: 0.024 seconds [root@master ~]# hadoop fs -ls /user/hive/warehouse/u_info ls: `/user/hive/warehouse/u_info': No such file or directory
表删除后,hdfs里面数据也就被删除了,
4.3 创建外部表¶
外部表和内部表最大区别就是内部表删除,hdfs里面数据也被删除了,而外部表被删除,hdfs里面数据不会呗删除
create EXTERNAL TABLE cmz_external ( usrid STRING, age STRING, sex STRING ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n';
详细操作
hive> create EXTERNAL TABLE cmz_external > ( > usrid STRING, > age STRING, > sex STRING > ) > ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' > LINES TERMINATED BY '\n'; OK Time taken: 0.076 seconds hive> show tables; OK cmz_external Time taken: 0.024 seconds, Fetched: 1 row(s)
或
hive -f abc.sql
插入数据
hive> LOAD DATA LOCAL INPATH '/root/hive/hive_test/a.txt' OVERWRITE INTO TABLE cmz_external; Loading data to table default.cmz_external Table default.cmz_external stats: [numFiles=1, numRows=0, totalSize=88, rawDataSize=0] OK Time taken: 0.437 seconds hive> select * from cmz_external; OK user1 27 1 user2 28 1 user3 29 0 user4 30 1 user5 31 0 user6 32 1 user7 33 1 user8 34 0 Time taken: 0.055 seconds, Fetched: 8 row(s)
删除表
[root@master ~]# hadoop fs -ls /user/hive/warehouse/cmz_external Found 1 items -rwxr-xr-x 3 root supergroup 88 2019-06-24 20:23 /user/hive/warehouse/cmz_external/a.txt hive> drop table cmz_external; OK Time taken: 0.097 seconds hive> show tables; OK Time taken: 0.019 seconds 再次查看hdfs [root@master ~]# hadoop fs -ls /user/hive/warehouse/cmz_external Found 1 items -rwxr-xr-x 3 root supergroup 88 2019-06-24 20:23 /user/hive/warehouse/cmz_external/a.txt 文件还在。
此时虽然表被删除了,但是hdfs上数据还在,不要怕,重新建那个表,数据自动回来。
hive> create EXTERNAL TABLE cmz_external > ( > usrid STRING, > age STRING, > sex STRING > ) > ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' > LINES TERMINATED BY '\n'; OK Time taken: 0.057 seconds hive> show tables; OK cmz_external Time taken: 0.02 seconds, Fetched: 1 row(s) hive> select * from cmz_external; OK user1 27 1 user2 28 1 user3 29 0 user4 30 1 user5 31 0 user6 32 1 user7 33 1 user8 34 0 Time taken: 0.08 seconds, Fetched: 8 row(s)
数据回来了
5. Hive基本操作¶
5.1 Hive基本操作¶
1. 启动hive [root@master root]# 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;
5.2 HiveJDBC¶
Hive除了自身客户端连接还是支持第三方插件连接,一下使用JDBC方式去连接、
5.2.1 启动hiveserver2服务¶
[root@master root]# hiveserver2 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory]
前台运行,方便查看错误
5.2.2 启动beeline¶
[root@master ~]# beeline SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.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.slf4j.impl.Log4jLoggerFactory] SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.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.slf4j.impl.Log4jLoggerFactory] Beeline version 1.2.2 by Apache Hive beeline> show databases; No current connection
5.2.3 连接hiveserver2¶
beeline> !connect jdbc:hive2://master:10000 Connecting to jdbc:hive2://master:10000 Enter username for jdbc:hive2://master:10000: root Enter password for jdbc:hive2://master:10000: Connected to: Apache Hive (version 1.2.2) Driver: Hive JDBC (version 1.2.2) Transaction isolation: TRANSACTION_REPEATABLE_READ 0: jdbc:hive2://master:10000>
只有有权限操作hive数据即可,我使用root。
0: jdbc:hive2://master:10000> show databases; +----------------+--+ | database_name | +----------------+--+ | cmz | | default | | hivedemo | | loocha | | test | +----------------+--+ 5 rows selected (1.519 seconds) 0: jdbc:hive2://master:10000> use loocha; No rows affected (0.085 seconds) 0: jdbc:hive2://master:10000> show tables; +-----------+--+ | tab_name | +-----------+--+ | stu | | t1 | | test | +-----------+--+ 3 rows selected (0.062 seconds) 0: jdbc:hive2://master:10000> select * from stu; +-----------+------------+------------+--+ | stu.name | stu.xueke | stu.score | +-----------+------------+------------+--+ | zhangsan | 10 | 90 | | lisi | 80 | 66 | | wangwu | 66 | 55 | +-----------+------------+------------+--+ 3 rows selected (1.047 seconds)
5.3 Hive常用交互模式¶
[root@master ~]# hive -help SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] 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)
5.3.1 -e 模式¶
类似mysql -e
,“-e”不进入hive的交互窗口执行sql语句.
hive -e 'show databases';
执行过程
[root@master ~]# hive -e 'show databases'; SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] Logging initialized using configuration in jar:file:/usr/local/apache-hive-1.2.2-bin/lib/hive-common-1.2.2.jar!/hive-log4j.properties OK database_name cmz default hivedemo loocha test Time taken: 2.069 seconds, Fetched: 5 row(s)
5.3.2 -f模式¶
-f
执行脚本中sql语句。
echo 'show databases'> caimengzhi.hql hive -f caimengzhi.hql
执行过程
[root@master ~]# echo 'show databases'> caimengzhi.hql [root@master ~]# hive -f caimengzhi.hql SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] Logging initialized using configuration in jar:file:/usr/local/apache-hive-1.2.2-bin/lib/hive-common-1.2.2.jar!/hive-log4j.properties OK database_name cmz default hivedemo loocha test Time taken: 1.074 seconds, Fetched: 5 row(s)
5.4 其他命令¶
- 退出
hive(default)>exit; hive(default)>quit;
在新版的hive中没区别了,在以前的版本是有的:
-
exit:先隐性提交数据,再退出;
-
quit:不提交数据,退出;
-
HDFS操作
在hive cli命令窗口中如何查看hdfs文件系统.
hive (default)> dfs -ls /user; Found 2 items drwxr-xr-x - root supergroup 0 2019-06-24 19:03 /user/hive drwxr-xr-x - root supergroup 0 2019-07-04 17:10 /user/root
- 产看操作系统本地命令
hive (default)> !ifconfig; ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.186.10 netmask 255.255.255.0 broadcast 192.168.186.255 inet6 fe80::9d58:5651:daa8:880a prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c6:79:90 txqueuelen 1000 (Ethernet) RX packets 649585 bytes 135398513 (129.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1720289 bytes 3534604662 (3.2 GiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1000 (Local Loopback) RX packets 152309 bytes 39464571 (37.6 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 152309 bytes 39464571 (37.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 hive (default)> !date; Thu Aug 1 18:41:01 CST 2019
注意末尾的 ;
- 查看在hive中输入的所有历史命令
1. 进入到当前用户的家目录 2. 查看. hivehistory文件
详细步骤
[root@master ~]# cd [root@master ~]# cat .hivehistory|head create table bucket_table1 (id int) clustered by(id) into 8 buckets; create table bucket_table2 (id int); load data local inpath 'testbucket1.txt' into table bucket_table2; insert into table bucket_table1 select * from table bucket_table2; insert into table bucket_table1 select * from bucket_table2; select * from bucket_table1 tablesample(bucket 3 out of 4 on id); create table docs(line string); load data inpath '/cmz/*.txt' overwrite into table docs; create table word_count as select word,count(1) as count from
5.5 Hive 常见参数配置¶
5.5.1 数据仓库位置¶
- Default数据仓库的最原始位置是在hdfs上的:/user/hive/warehouse路径下。
- 在仓库目录下,没有对默认的数据库default创建文件夹。如果某张表属于default数据库,直接在数据仓库目录下创建一个文件夹。
- 修改default数据仓库原始位置(将hive-default.xml.template如下配置信息拷贝到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>
配置同组用户有执行权限
hdfs dfs -chmod g+w /user/hive/warehouse
5.5.2 查询后信息显示配置¶
- 在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>
- 重新启动hive,对比配置前后差异。
配置前, 显示如下:
hive > show databases; OK database_name cmz default hivedemo loocha test Time taken: 0.136 seconds, Fetched: 5 row(s)
配置之后重启进入
hive (default)> show databases; OK database_name cmz default hivedemo loocha test Time taken: 0.136 seconds, Fetched: 5 row(s) hive (default)> use test; hive (test)> select * from student1; OK student1.name student1.chinese student1.math student1.english zhangsan NULL 90 60 lisi 80 66 77 wangwu 66 55 80 Time taken: 0.067 seconds, Fetched: 3 row(s)
显示了当前库的名字。比如上面的库名default,test,且也显示表字段。
5.5.3 Hive运行日志信息配置¶
- Hive的log默认存放在/tmp/root/hive.log目录下(当前用户名下)
- 修改hive的log存放日志到/usr/local/src/logs/hive/logs
修改/opt/module/hive/conf/hive-log4j.properties.template文件名称为hive-log4j.properties
[root@master conf]# pwd /usr/local/hive/conf [root@master conf]# mv hive-log4j.properties.template hive-log4j.properties
- 在hive-log4j.properties文件中修改log存放位置
hive.log.dir=/usr/local/src/logs/hive/logs
5.5.4 参数配置方式¶
5.5.4.1 查看当前所有的配置信息¶
hive>set;
5.5.4.2 参数的配置三种方式¶
- 配置文件方式
默认配置文件:hive-default.xml
用户自定义配置文件:hive-site.xml
注意:用户自定义配置会覆盖默认配置。另外,Hive也会读入Hadoop的配置,因为Hive是作为Hadoop的客户端启动的,Hive的配置会覆盖Hadoop的配置。配置文件的设定对本机启动的所有Hive进程都有效。
5.5.4.3 命令行参数方式¶
- 启动Hive时,可以在命令行添加-hiveconf param=value来设定参数。
hive -hiveconf hive.cli.print.current.db=true;
详细过程
[root@master conf]# hive -hiveconf hive.cli.print.current.db=true SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] Logging initialized using configuration in jar:file:/usr/local/apache-hive-1.2.2-bin/lib/hive-common-1.2.2.jar!/hive-log4j.properties hive (default)> [root@master ~]# hive -hiveconf hive.cli.print.current.db=false; SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/usr/local/hadoop-2.6.5/share/hadoop/common/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/usr/local/spark-1.6.3-bin-hadoop2.6/lib/spark-assembly-1.6.3-hadoop2.6.0.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.slf4j.impl.Log4jLoggerFactory] Logging initialized using configuration in jar:file:/usr/local/apache-hive-1.2.2-bin/lib/hive-common-1.2.2.jar!/hive-log4j.properties hive>
对比. ==hive (default)> == ==hive> ==
注意:仅对本次hive启动有效
- 参数声明方式
可以在HQL中使用SET关键字设定参数
hive> set hive.cli.print.current.db=true; hive (default)>
注意:仅对本次hive启动有效。
查看之前当前的参数设置
hive (default)> set hive.cli.print.current.db; hive.cli.print.current.db=true
要是查看所有就
hive (default)> set; 省略
上述三种设定方式的优先级依次递增。即配置文件<命令行参数<参数声明。注意某些系统级的参数,例如log4j相关的设定,必须用前两种方式设定,因为那些参数的读取在会话建立以前已经完成了。