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
,不执行