进入到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> SQLfrom 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 ininteractive shell
-v,--verbose Verbose mode (echoexecuted SQL to the console)
非交互式:
(1)“-e”不进入hive的交互窗口执行hql语句(传用引号引起来的查询字段,任务执行完之后就会自动退出,适用于每天都需要重复执行的任务)
bin/hive -e "select id from student;"
(2)“-f”执行脚本中的hql语句(传入一个文件,文件里由sql语句)
先在/opt/module/hive/下创建datas目录并在datas目录下创建hivef.sql文件,文件中写入:
select * from stu;
执行文件中的hql语句,并写入到文件中:
bin/hive -f /opt/module/hive/datas/hivef.sql > /opt/module/hive/datas/hive_result.txt
默认配置文件:hive-default.xml
用户自定义配置文件:hive-site.xml
用户自定义配置会覆盖默认配置。因为Hive是作为Hadoop的客户端启动的,所以Hive也会读入Hadoop的配置,Hive的配置会覆盖Hadoop的配置。
启动Hive时,可以在命令行添加-hiveconf param=value来设定参数,且仅对本次Hive启动有效。如:
bin/hive -hiveconf mapreduce.job.reduces=10;
查看参数设置:
hive > set mapreduce.job.reduces;
可以在HQL中使用SET关键字设定参数,且仅对本次Hive启动有效,如:
hive > set mapreduce.job.reduces=10;
参数配置方式优先级:配置文件 < 命令行参数 < 参数声明
但某些系统级的参数,例如log4j相关的设定,必须用前两种方式设定,因为那些参数的读取在会话建立以前已经完成了。
hive-site.xml中加入如下:
<!—控制是否展示查询结果的字段名 -->
<property>
<name>hive.cli.print.headername>
<value>truevalue>
<description>Whether to print the names of the columns in queryoutput.description>
property>
<!—控制是否显示当前的数据库 -->
<property>
<name>hive.cli.print.current.dbname>
<value>truevalue>
<description>Whether to include the current database in the Hiveprompt.description>
property>
Hive的log默认存放在/tmp/用户名/hive.log目录下(当前用户名下),修改Hive的log存放日志到/opt/module/hive/logs。
(1)修改$HIVE_HOME/conf/hive-log4j2.properties.template文件名称为hive-log4j2.properties:
cd /opt/module/hive/conf
mv hive-log4j2.properties.template hive-log4j2.properties
(2)在hive-log4j2.properties文件中修改log存放位置,修改如下:
property.hive.log.dir=/opt/module/hive/logs
新版本的Hive启动的时候,默认申请的JVM堆内存大小为256M,JVM堆内存申请的太小,导致后期开启本地模式,执行复杂的SQL时经常会报错:java.lang.OutOfMemoryError: Java heap space,需要修改HADOOP_HEAPSIZE参数。
原因:
对于数据量比较小的任务就不提交给Yarn进行分布式运算,而是在本地运行(如果使用Hive CLI命令行客户端则在客户端的本地运行,如果使用JDBC客户端则在Hiveserver2上运行),可以节省申请资源的开销,速度更快,所以对Hive堆内存要求要高些。
(1)修改$HIVE_HOME/conf下的hive-env.sh.template为hive-env.sh:
cd /opt/module/hive/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 hiveshell script can be controlled via:
# export HADOOP_HEAPSIZE=1024
#改为:
# The heap size of the jvm stared by hiveshell script can be controlled via:
export HADOOP_HEAPSIZE=2048
在yarn-site.xml中关闭虚拟内存检查(虚拟内存校验,如果已经关闭了,就不需要配了)。
yarn-site.xml中添加:
<!—虚拟内存检查是否启用,与yarn中容器的内存有关 -->
<property>
<name>yarn.nodemanager.vmem-check-enabled</name>
<value>false</value>
</property>
分发并重启yarn:
xsync yarn-site.xml
myhadoop.sh stop
myhadoop.sh start