基本操作
先创建测试文件(/home/bxp/Documents/doc/hivetest/hive.txt
)数据,内容如下(每行的两个字段之间使用”\t”分隔):
11 AA
22 BB
33 CC
使用bin/hive
命令进入hive客户端shell。hive连接成功后和mysql数据库的操作基本相同。他具有一个默认的数据库default,通过show databases
命令能够查看的到。
(1)对数据库,表的基本操作
//创建数据库
create database bhive;
//查看创建的表
show databases;
show databases like "b*"; //模糊查询
//选择将要操作的数据库
use bhive;
//创建表
create table b_test(id string, name string)row format delimited fields terminated by '\t';
//向表中加载数据,/home/bxp/Documents/doc/hivetest/hive.txt' 为本篇开始创建的文件目录
load data local inpath '/home/bxp/Documents/doc/hivetest/hive.txt' into table b_test;
//查询表中的数据
select * from b_test;
//查看表的描述信息
desc bhive;
//查看表的详细描述信息
desc extended b_test;
//查看表的格式化后的详细描述信息
desc formatted b_test;
//查看所有的函数
show functions;
//查看函数如何使用
desc function upper;
//查看函数如何使用,包含详细的信息以及使用案例
desc function extended upper;
//删除数据库,如果数据库存在数据表,可以使用第二条语句删除,
//数据库删除后对应的hdfs文件系统的文件目录也会删除
drop database if exists bhive;
drop database if exists bhive cascade;
(2)其他基本操作
//在hive客户端中操作hdfs文件系统
dfs -s /
hive客户端中操作本地文件系统(命令之前加!)
!ls /
//查看历史命令,查看用户主目录下的.hivehistory 文件
!cat /home/bxp/.hivehistory
(3)bin/hive命令:
//执行一条hive交互式命令,但是终端不进入hive客户端
bin/hive -e "select * from bhive.b_test";
//执行sql脚本
bin/hive -f fileName.sql;
//将执行结果写入指定的文件desFileName
bin/hive -f fileName.sql > desFileName;
//执行sql脚本,和-f的区别是,-i执行的sql脚本是用户初始化使用。
bin/hive -i fileName.sql;
常用的配置
(1)使用hive查询数据时,默认不显示查询的字段的头信息。需要显示,只需要在hive-site.xml中添加如下的配置信息即可
<!--配置使用hive查询数据时,显示所查询字段的头信息-->
<property>
<name>hive.cli.print.header</name>
<value>true</value>
<description>Whether to print the names of the columns in query output.</description>
</property>
<!--配置当前使用的是那个数据库-->
<property>
<name>hive.cli.print.current.db</name>
<value>true</value>
<description>Whether to include the current database in the Hive prompt.</description>
</property>
(2)配置日志,在hive-log4j2.properties文件中进行配置:
hive.log.threshold=ALL
hive.root.logger=INFO,DRFA
hive.log.dir=/home/bxp/Documents/install/hive-2.2.0-bin/logs
hive.log.file=hive.log
(3)查看并设置属性值
//查看属性值信息
set;
设置属性值
set name=value
在启动hive时进行属性值的设置
bin/hive --hiveconf name=value
(4)创建的表在hdfs文件系统中的路径配置,默认路径是/user/hive/warehouse,也可以在hive.site.xml文件中进行配置。
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive/warehouse</value>
</property>
<property>
不管是自己创建的目录还是使用默认的目录,在目录创建完成后都需要为目录添加相应的权限
dfs -chmod g+w /user/hive/warehouse