Hive使用之命令行方式

2023-10-14 大数据Hive

第一个是使用bin目录下的hive命令,这个是从hive一开始就支持的使用方式;后来又出现一个beeline命令,它是通过HiveServer2服务连接hive,它是一个轻量级的客户端工具,所以后来官方开始推荐使用这个。

但其实使用哪一种无所谓,去执行结果都是一样的,没有任何区别。

# hive命令方式

输入hive就直接进入客户端了,然后就可以直接输入SQL使用了。

[root@flume apache-hive-3.1.3-bin]# bin/hive
which: no hbase in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.382.b05-
hive> 

首先我们来查看表,发现没有表。然后创建一张表,再次查看表:

 



 



 




hive> show tables;
OK
Time taken: 0.534 seconds

hive> create table t1(id int,name string);
OK
Time taken: 1.054 seconds

hive> show tables;
OK
t1
Time taken: 0.06 seconds, Fetched: 1 row(s)

然后尝试插入输入,此时就开始执行MapRedurce任务了

 





















hive> insert into t1(id,name) values(1,"zs");
Query ID = root_20231013222833_5d9cd15e-a331-4175-be1b-baedc20a2571
Total jobs = 3
Launching Job 1 out of 3
Number of reduce tasks determined at compile time: 1
In order to change the average load for a reducer (in bytes):
  set hive.exec.reducers.bytes.per.reducer=<number>
In order to limit the maximum number of reducers:
  set hive.exec.reducers.max=<number>
In order to set a constant number of reducers:
  set mapreduce.job.reduces=<number>
Starting Job = job_1697203680404_0001, Tracking URL = http://bigdata03:8088/proxy/application_1697203680404_0001/
Kill Command = /usr/local/soft/hadoop/hadoop-3.2.4/bin/mapred job  -kill job_1697203680404_0001
Hadoop job information for Stage-1: number of mappers: 0; number of reducers: 0
2023-10-13 22:28:49,767 Stage-1 map = 0%,  reduce = 0%
Ended Job = job_1697203680404_0001 with errors
Error during job, obtaining debugging information...
FAILED: Execution Error, return code 2 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask
MapReduce Jobs Launched: 
Stage-Stage-1:  HDFS Read: 0 HDFS Write: 0 FAIL
Total MapReduce CPU Time Spent: 0 msec

删除表使用的SQL也是一样;如果想要退出客户端,使用quit命令即可。

hive> drop table t1;
OK
Time taken: 0.487 seconds
hive> quit;

# beeline命令方式

注意了,启动hiveserver2服务之后,最下面会输出几行Hive Session ID的信息,一定要等到输出 4行以后再使用beeline去连接,否则会提示连接拒绝

# 创建两个远程连接,连接一启动hiveserver2服务,默认端口是10000
[root@flume apache-hive-3.1.3-bin]# bin/hiveserver2 

# 连接2,连接到这个服务,-n表示指定用户身份区连接,不然可能会导致文件读写权限不足
[root@flume apache-hive-3.1.3-bin]# bin/beeline -u jdbc:hive2://localhost:10000 -n root

退出使用ctrl+c即可

# 常见命令 (后文有详细说明)

  • 查看数据库列表: show databases;
  • 选择数据库:use 数据库名称; 默认在default数据库操作
  • 创建数据库:create database 名称;
  • 创建数据库并指定存储路径:create database 名称 location '/user/hive/mydb2';
  • 创建表:create table 表名(字段 类型, 字段 类型);
  • 显示当前数据库中所有的表名:show tables;
  • 查询表结构:desc 表名;
上次更新: 4 个月前