服务端HiveServer2和客户端beeline进行Hive SQL操作
一、启动服务端HiveServer2
配置好hive环境变量后:
cd $HIVE_HOME/bin
./hiveserver2
启动成功后:
[ruoze@zhangfei ~]$ hiveserver2 --hiveconf hive.server2.thrift.port=10086
which: no hbase in (/home/ruoze/app/hive/bin:/home/ruoze/app/hadoop/bin:/home/ruoze/app/hadoop/sbin:/usr/java/jdk1.8.0_45/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ruoze/.local/bin:/home/ruoze/bin)
19/12/21 16:54:38 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
OK
OK
OK
OK
生产上会将该启动命令放到后台执行:
nohup ./hiveserver2 &
后台执行命令成功后不会出现上图客户端访问服务端的输出结果。
二、启动客户端beeline:
jdbc连接hive客户端连接默认端口是10000号:
cd $HIVE_HOME/bin
./beeline -u jdbc:hive2://zhangfei:10000/zfdata_hive -n hadoop
端口号可修改,方法如下:
./hiveserver2 --hiveconf hive.server2.thrift.port=10086
再次启动客户端:
./beeline -u jdbc:hive2://zhangfei:10086/zfdata_hive -n hadoop
启动在命令行可看到当前hive版本,驱动器,beeline版本,接下来就可以对hive中的表进行各种操作啦。
[ruoze@zhangfei bin]$ ./beeline -u jdbc:hive2://zhangfei:10086/zfdata_hive -n hadoop
which: no hbase in (/home/ruoze/app/hive/bin:/home/ruoze/app/hadoop/bin:/home/ruoze/app/hadoop/sbin:/usr/java/jdk1.8.0_45/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ruoze/.local/bin:/home/ruoze/bin)
scan complete in 1ms
Connecting to jdbc:hive2://zhangfei:10086/zfdata_hive
Connected to: Apache Hive (version 1.1.0-cdh5.16.2)
Driver: Hive JDBC (version 1.1.0-cdh5.16.2)
Transaction isolation: TRANSACTION_REPEATABLE_READ
Beeline version 1.1.0-cdh5.16.2 by Apache Hive
0: jdbc:hive2://zhangfei:10086/zfdata_hive> show databases;
INFO : Compiling command(queryId=ruoze_20191221165555_677b50d6-cb8a-4e24-a8fd-245b21fa731d): show databases
INFO : Semantic Analysis Completed
INFO : Returning Hive schema: Schema(fieldSchemas:[FieldSchema(name:database_name, type:string, comment:from deserializer)], properties:null)
INFO : Completed compiling command(queryId=ruoze_20191221165555_677b50d6-cb8a-4e24-a8fd-245b21fa731d); Time taken: 0.612 seconds
INFO : Concurrency mode is disabled, not creating a lock manager
INFO : Executing command(queryId=ruoze_20191221165555_677b50d6-cb8a-4e24-a8fd-245b21fa731d): show databases
INFO : Starting task [Stage-0:DDL] in serial mode
INFO : Completed executing command(queryId=ruoze_20191221165555_677b50d6-cb8a-4e24-a8fd-245b21fa731d); Time taken: 0.041 seconds
INFO : OK
+----------------+--+
| database_name |
+----------------+--+
| default |
| zfdata |
+----------------+--+
0: jdbc:hive2://zhangfei:10086/zfdata_hive> select * from emp ;
INFO : Compiling command(queryId=ruoze_20191221165656_eed7410e-6c33-4162-9853-3081ca31f46a): select * from emp
INFO : Semantic Analysis Completed
INFO : Returning Hive schema: Schema(fieldSchemas:[FieldSchema(name:emp.empno, type:int, comment:null), FieldSchema(name:emp.ename, type:string, comment:null), FieldSchema(name:emp.job, type:string, comment:null), FieldSchema(name:emp.mgr, type:int, comment:null), FieldSchema(name:emp.hiredate, type:string, comment:null), FieldSchema(name:emp.sal, type:double, comment:null), FieldSchema(name:emp.comm, type:double, comment:null), FieldSchema(name:emp.deptno, type:int, comment:null)], properties:null)
INFO : Completed compiling command(queryId=ruoze_20191221165656_eed7410e-6c33-4162-9853-3081ca31f46a); Time taken: 0.328 seconds
INFO : Concurrency mode is disabled, not creating a lock manager
INFO : Executing command(queryId=ruoze_20191221165656_eed7410e-6c33-4162-9853-3081ca31f46a): select * from emp
INFO : Completed executing command(queryId=ruoze_20191221165656_eed7410e-6c33-4162-9853-3081ca31f46a); Time taken: 0.0 seconds
INFO : OK
+------------+------------+------------+----------+---------------+----------+-----------+-------------+--+
| emp.empno | emp.ename | emp.job | emp.mgr | emp.hiredate | emp.sal | emp.comm | emp.deptno |
+------------+------------+------------+----------+---------------+----------+-----------+-------------+--+
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.0 | NULL | 20 |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-2-20 | 1600.0 | 300.0 | 30 |
| 7521 | WARD | SALESMAN | 7698 | 1981-2-22 | 1250.0 | 500.0 | 30 |
| 7566 | JONES | MANAGER | 7839 | 1981-4-2 | 2975.0 | NULL | 20 |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-9-28 | 1250.0 | 1400.0 | 30 |
| 7698 | BLAKE | MANAGER | 7839 | 1981-5-1 | 2850.0 | NULL | 30 |
| 7782 | CLARK | MANAGER | 7839 | 1981-6-9 | 2450.0 | NULL | 10 |
| 7788 | SCOTT | ANALYST | 7566 | 1987-4-19 | 3000.0 | NULL | 20 |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.0 | NULL | 10 |
| 7844 | TURNER | SALESMAN | 7698 | 1981-9-8 | 1500.0 | 0.0 | 30 |
| 7876 | ADAMS | CLERK | 7788 | 1987-5-23 | 1100.0 | NULL | 20 |
| 7900 | JAMES | CLERK | 7698 | 1981-12-3 | 950.0 | NULL | 30 |
| 7902 | FORD | ANALYST | 7566 | 1981-12-3 | 3000.0 | NULL | 20 |
| 7934 | MILLER | CLERK | 7782 | 1982-1-23 | 1300.0 | NULL | 10 |
| 8888 | HIVE | PROGRAM | 7839 | 1988-1-23 | 10300.0 | NULL | NULL |
+------------+------------+------------+----------+---------------+----------+-----------+-------------+--+
15 rows selected (0.445 seconds)
0: jdbc:hive2://zhangfei:10086/zfdata_hive>