Hive专题三--Hive的三种使用方式

版权声明:本文为博主原创文章,转载请注明出处。

欢迎访问:https://blog.csdn.net/qq_21439395

交流QQ: 824203453

欢迎访问博主个人主页:http://www.oldsheep.cn 

hive使用方式

最基本使用方式

启动一个hive交互shell

bin/hive

hive>

设置一些基本参数,让hive使用起来更便捷,比如:

1、让提示符显示当前库:

hive>set hive.cli.print.current.db=true;
  1. 显示查询结果时显示字段名称:
hive>set hive.cli.print.header=true;

3,让mapreduce程序以local模式运行

hive>set hive.exec.mode.local.auto=true;

mapreduce任务默认以yarn模式启动:

学习阶段,可以让mapreduce以local模式运行

但是这样设置只对当前会话有效,重启hive会话后就失效,解决办法:

在linux的当前用户目录中,编辑一个.hiverc文件,将参数写入其中:

vi .hiverc

set hive.cli.print.header=true;
set hive.cli.print.current.db=true;
set hive.exec.mode.local.auto=true;

或者直接修改配置文件:

在hive-site.xml文件中添加如下配置信息,即可实现上述需求:


<property>
       <name>hive.cli.print.header</name>
       <value>true</value>
</property>
<property>
       <name>hive.cli.print.current.db</name>
       <value>true</value>
</property>
<property>
       <name>hive.exec.mode.local.auto</name>
       <value>true</value>
</property>

 

启动hive服务使用

启动hive的服务:

[root@hdp-02 hive-1.2.1]# bin/hiveserver2 -hiveconf hive.root.logger=DEBUG,console

上述启动,会将这个服务启动在前台,如果要启动在后台,则命令如下:

nohup bin/hiveserver2 1>/dev/null 2>&1 &

启动成功后,可以在别的节点上用beeline去连接

  1. 方式(1)

[root@hdp-02 hive-1.2.1]# bin/beeline  回车,进入beeline的命令界面

输入命令连接hiveserver2

beeline> !connect jdbc:hive2://hdp-02:10000

(hadoop01是hiveserver2所启动的那台主机名,端口默认是10000)

  1. 方式(2)

启动时直接连接:

bin/beeline -u jdbc:hive2://hdp-02:10000 -n root

接下来就可以做正常sql查询了

 

脚本化运行

大量的hive查询任务,如果用交互式shell来进行输入的话,显然效率及其低下,因此,生产中更多的是使用脚本化运行机制:

该机制的核心点是:hive可以用一次性命令的方式来执行给定的hql语句

[root@hdp-02 ~]#  hive -e "insert into table t_dest select * from t_src;"

然后,进一步,可以将上述命令写入shell脚本中,以便于脚本化运行hive任务,并控制、调度众多hive任务,示例如下:

vi t_order_etl.sh

#!/bin/bash
hive -e "select * from db_order.t_order"
hive -e "select * from default.t_user"
hql="create table  default.t_bash as select * from db_order.t_order"
hive -e "$hql"

如果要执行的hql语句特别复杂,那么,可以把hql语句写入一个文件:

vi x.hql

select * from db_order.t_order;
select count(1) from db_order.t_user;

然后,用hive -f /root/x.hql 来执行

还可以为hive -e 和 hive -f 配置定时任务:

hive -e “sql语句”

hive -f  etl.sql


[root@hdp-03 ~]# hive -e "select * from t_order;show tables;"
[root@hdp-03 ~]# vim etl.sql
select * from t_order;
show tables;
show databases;
[root@hdp-03 ~]# hive -f etl.sql

分钟 小时 天 月 周 具体的命令

[root@hdp-03 ~]# cat etl.sh
#!/bin/bash
/root/apps/apache-hive-1.2.2-bin/bin/hive -f "/root/etl.sql"
/root/apps/apache-hive-1.2.2-bin/bin/hive -e "show tables;"

配置定时任务:

[root@hdp-03 ~]# crontab -e
* * * * * /root/apps/apache-hive-1.2.2-bin/bin/hive -e "show tables;"
* * * * * /bin/bash /root/etl.sh

交流QQ: 824203453

欢迎访问博主个人主页:http://www.oldsheep.cn 

猜你喜欢

转载自blog.csdn.net/qq_21439395/article/details/88816653