hive sql——hplsql命令行

12 篇文章 2 订阅

PL/HQL主要是通过命令行工具来执行一个SQL查询语句或者是一个SQL脚本。

进入$HIVE_HOME/bin/,执行./ hplsql即可显示命令行的用法:

./hplsql 
usage: hplsql
 -d,--define <key=value>          变量替换,例如-d A=B或——define A=B
 -e                               HPL/SQL命令行参数
 -f                               HPL/SQL文件
 -H,--help                        Print help information
    --hiveconf <property=value>   Value for given property
    --hivevar <key=value>         Variable subsitution e.g. --hivevar A=B
 -main                            Entry point (procedure or function name)
 -offline,--offline               Offline mode - skip SQL execution
 -trace,--trace                   Print debug information
 -version,--version               Print HPL/SQL version

1.1 -d,–define

-d,–define <key=value>选项可以指定变量名及变量值,它和–hiveconf <property=value>以及–hivevar <key=value>的效果等价。

 ./hplsql -e "PRINT a || ',' || b || c" -d a=Hello -d b=Bigdata -d c=!
Hello,Bigdata!
 ./hplsql -e "PRINT a || ',' || b || c" --hiveconf a=Hello --hiveconf b=Bigdata --hiveconf c=!
 Hello, Bigdata!
./hplsql -e "PRINT a || ', ' || b || c" --hivevar a=Hello --hivevar b=Bigdata --hivevar c=!
Hello, Bigdata!

1.2 -e <quoted-query-string>

hive –e,用于执行HQL语句;

$ hive -e "DESC test.school_class_info"
 
Logging initialized using configuration in file:/home/hadoop/hive/apache-hive-2.3.7-bin/conf/hive-log4j2.properties Async: true
OK
class               	string
student             	array<string>
user_info           	map<string,int>
position            	struct<province:string,city:string,district:string>
user_id             	bigint              	??ID
name                	string              	????
city                	string              	??
sex                 	string              	????
age                 	string              	????
phone               	string              	????
email               	string              	????
Time taken: 1.359 seconds, Fetched: 11 row(s)

1.3 –f <filename>

hive –f,执行一个文件中保存的HQL语句。

$ cat test.sql
desc test.school_class_info;
$ hive -f test.sql
Logging initialized using configuration in file:/home/hadoop/hive/apache-hive-2.3.7-bin/conf/hive-log4j2.properties Async: true
OK
class               	string
student             	array<string>
user_info           	map<string,int>
position            	struct<province:string,city:string,district:string>
user_id             	bigint              	??ID
name                	string              	????
city                	string              	??
sex                 	string              	????
age                 	string              	????
phone               	string              	????
email               	string              	????
Time taken: 1.326 seconds, Fetched: 11 row(s)

1.4 -trace,–trace

打印debug信息。

$ hplsql -e "PRINT 'Hello,Bigdata'" -trace
Configuration file: jar:file:/home/hadoop/hive/apache-hive-2.3.7-bin/lib/hive-hplsql-2.3.7.jar!/hplsql-site.xml
Parser tree: (program (block (stmt (print_stmt PRINT (expr (expr_atom (string 'Hello,Bigdata')))))))
Ln:1 PRINT
Hello,Bigdata

1.5 –main <procname>

-main <procname>只执行指定函数或存储过程内的语句。

比如:test.sql文件内容如下:

$ cat test.sql 
$ cat /home/hadoop/wangtongbo/test.sql
CREATE PROCEDURE set_message(IN arg STRING)
BEGIN
 SET result = 'Hello,SaoDiseng!';
 PRINT result || ' ' || arg;
END;

PRINT 'Hello,Bigdata! at beginning .. ';
CALL set_message(' by CALL ..');
select * from test.school_class_test;

1.创建存储过程set_message;
2.打印’Hello,Bigdata! at beginning … ';
3.调用存储过程,并传入参数;
4.查询Hive表test.school_class_test;

看看使用-main选项和不使用的执行结果:

$ ./hplsql -f test.sql
Hello,SaoDiseng! at beginning .. 
Hello,Bigdata!  by CALL ..
Open connection: jdbc:hive2://172.16.0.30:10000 (125 ms)
Starting query
Query executed successfully (166 ms)
X
$ ./hplsql -f test.sql -main set_message
Hello,SaoDiseng!

如果不指定-main选项,那么则将test.sql中的语句从头到尾执行完。
如果指定了-main选项,则会执行指定存储过程内的语句。

1.6 其他

-H,–help为打印帮助信息。
-version,–version 打印HPL/SQL版本。
-offline,–offline选项的功能offline参数意思是只解析sql,不执行

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扫地增

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值