对于熟悉Scala开发的人来说,对于spark-sql的使用,直接jar包中写入代码处理就能轻松实现动态语句的执行。
但是对于我,不打算学习Scala和Java语言,但是又想定时执行时间推延的周期、定时任务,该肿么办?
spark-sql CLI几个参数,完全满足我等非专业人员。
CLI options:
-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)
###########################
-e 可以在命令中带sql语句,但是明显不适合大量的sql。小量语句可以考虑。
-f sql文件,只要把sql写入文件,直接批量提交,全部语句一次性任务,如果周期变化了,怎么办?
--hiveconf 主角登场,可以把参数带入sql语句中替换,在shell中赋值,实现动态sql。
测试前提:预先pcp2表是存在的,这里不错复杂判断表格是否存在后再查询。
###########################################################
测试一:
#简单替换SQL中的参数 -hiveconf 把2个参数替代sql文件里面的动态参数
spark-sql -S -hiveconf table_name='pcp2' -hiveconf row_limit='10' -f /opt/hadoop/sql/spark-test.sql
#########################
--这个SQL文件中,查询的表名字,返回的条目数,由外部输入。
spark-test.sql 内容如下:
show tables;
select * from ${hiveconf:table_name} limit ${hiveconf:row_limit};
插图1
#########################
[root@snn sql]# spark-sql -S -hiveconf table_name=
但是对于我,不打算学习Scala和Java语言,但是又想定时执行时间推延的周期、定时任务,该肿么办?
spark-sql CLI几个参数,完全满足我等非专业人员。
CLI options:
-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)
###########################
-e 可以在命令中带sql语句,但是明显不适合大量的sql。小量语句可以考虑。
-f sql文件,只要把sql写入文件,直接批量提交,全部语句一次性任务,如果周期变化了,怎么办?
--hiveconf 主角登场,可以把参数带入sql语句中替换,在shell中赋值,实现动态sql。
测试前提:预先pcp2表是存在的,这里不错复杂判断表格是否存在后再查询。
###########################################################
测试一:
#简单替换SQL中的参数 -hiveconf 把2个参数替代sql文件里面的动态参数
spark-sql -S -hiveconf table_name='pcp2' -hiveconf row_limit='10' -f /opt/hadoop/sql/spark-test.sql
#########################
--这个SQL文件中,查询的表名字,返回的条目数,由外部输入。
spark-test.sql 内容如下:
show tables;
select * from ${hiveconf:table_name} limit ${hiveconf:row_limit};
插图1
#########################
[root@snn sql]# spark-sql -S -hiveconf table_name=