2.6 Hive使用技巧
2.6.1 Hive常用交互命令
[atguigu@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)在Hive命令行里创建一个表student,并插入1条数据
hive (default)> create table student(id int,name string);
OK
Time taken: 1.291 seconds
hive (default)> insert into table student values(1,"zhangsan");
hive (default)> select * from student;
OK
student.id student.name
1 zhangsan
Time taken: 0.144 seconds, Fetched: 1 row(s)
2)“-e”不进入hive的交互窗口执行hql语句
[atguigu@hadoop102 hive]$ bin/hive -e "select id from student;"
3)“-f”执行脚本中的hql语句
(1)在/opt/module/hive/下创建datas目录并在datas目录下创建hivef.sql文件
[atguigu@hadoop102 hive]$ mkdir datas
[atguigu@hadoop102 datas]$ vim hivef.sql
(2)文件中写入正确的hql语句
select * from student;
(3)执行文件中的hql语句
[atguigu@hadoop102 hive]$ bin/hive -f /opt/module/hive/datas/hivef.sql
(4)执行文件中的hql语句并将结果写入文件中
[atguigu@hadoop102 hive]$ bin/hive -f /opt/module/hive/datas/hivef.sql > /opt/module/hive/datas/hive_result.txt
2.6.2 Hive参数配置方式
1)查看当前所有的配置信息
hive>set;
2)参数的配置三种方式
(1)配置文件方式
- 默认配置文件:hive-default.xml
- 用户自定义配置文件:hive-site.xml
注意:用户自定义配置会覆盖默认配置。另外,Hive也会读入Hadoop的配置,因为Hive是作为Hadoop的客户端启动的,Hive的配置会覆盖Hadoop的配置。配置文件的设定对本机启动的所有Hive进程都有效。
(2)命令行参数方式
①启动Hive时,可以在命令行添加-hiveconf param=value来设定参数。例如:
[atguigu@hadoop103 hive]$ bin/hive -hiveconf mapreduce.job.reduces=10;
注意:仅对本次Hive启动有效。
②查看参数设置
hive (default)> set mapreduce.job.reduces;
(3)参数声明方式
可以在HQL中使用SET关键字设定参数,例如:
hive(default)> set mapreduce.job.reduces=10;
注意:仅对本次Hive启动有效。
查看参数设置:
hive(default)> set mapreduce.job.reduces;
上述三种设定方式的优先级依次递增。即配置文件 < 命令行参数 < 参数声明。注意某些系统级的参数,例如log4j相关的设定,必须用前两种方式设定,因为那些参数的读取在会话建立以前已经完成了。
2.6.3 Hive常见属性配置
1)Hive客户端显示当前库和表头
(1)在hive-site.xml中加入如下两个配置:
[atguigu@hadoop102 conf]$ vim hive-site.xml
<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客户端在运行时可以显示当前使用的库和表头信息
[atguigu@hadoop102 conf]$ hive
hive (default)> select * from stu;
OK
stu.id stu.name
1 ss
Time taken: 1.874 seconds, Fetched: 1 row(s)
hive (default)>
2)Hive运行日志路径配置
(1)Hive的log默认存放在/tmp/atguigu/hive.log目录下(当前用户名下)
[atguigu@hadoop102 atguigu]$ pwd
/tmp/atguigu
[atguigu@hadoop102 atguigu]$ ls
hive.log
hive.log.2022-06-27
(2)修改Hive的log存放日志到/opt/module/hive/logs
1修改$HIVE_HOME/conf/hive-log4j2.properties.template文件名称为
hive-log4j2.properties
[atguigu@hadoop102 conf]$ pwd
/opt/module/hive/conf
[atguigu@hadoop102 conf]$ mv hive-log4j2.properties.template hive-log4j2.properties
2在hive-log4j2.properties文件中修改log存放位置
[atguigu@hadoop102 conf]$ vim hive-log4j2.properties
修改配置如下
property.hive.log.dir=/opt/module/hive/logs
3)Hive的JVM堆内存设置
新版本的Hive启动的时候,默认申请的JVM堆内存大小为256M,JVM堆内存申请的太小,导致后期开启本地模式,执行复杂的SQL时经常会报错:java.lang.OutOfMemoryError: Java heap space,因此最好提前调整一下HADOOP_HEAPSIZE这个参数。
(1)修改$HIVE_HOME/conf下的hive-env.sh.template为hive-env.sh
[atguigu@hadoop102 conf]$ pwd
/opt/module/hive/conf
[atguigu@hadoop102 conf]$ mv hive-env.sh.template hive-env.sh
(2)将hive-env.sh其中的参数 export HADOOP_HEAPSIZE修改为2048,重启Hive。
修改前
# The heap size of the jvm stared by hive shell script can be controlled via:
# export HADOOP_HEAPSIZE=1024
修改后
# The heap size of the jvm stared by hive shell script can be controlled via:
export HADOOP_HEAPSIZE=2048
4)关闭Hadoop虚拟内存检查
在yarn-site.xml中关闭虚拟内存检查(虚拟内存校验,如果已经关闭了,就不需要配了)。
(1)修改前记得先停Hadoop
[atguigu@hadoop102 hadoop]$ pwd
/opt/module/hadoop-3.1.3/etc/hadoop
[atguigu@hadoop102 hadoop]$ vim yarn-site.xml
(2)添加如下配置
<property>
<name>yarn.nodemanager.vmem-check-enabled</name>
<value>false</value>
</property>
(3)修改完后记得分发yarn-site.xml,并重启yarn。
2761

被折叠的 条评论
为什么被折叠?



