hive脚本的执行方式大致有三种:
参考hive的用法:
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> SQL from command line
-f <filename> SQL from files
-H,--help Print help information
-h <hostname> connecting to Hive Server on remote host
--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
-p <port> connecting to Hive Server on port number
-S,--silent Silent mode in interactive shell
-v,--verbose Verbose mode (echo executed SQL to the
console)
- hive控制台执行;
hive控制台以后,执行sql语句
- hive -e “SQL”执行;
hive -e " select page_name, tpa_name
from pms.pms_exps_prepro
limit 2;"
- hive -f SQL文件执行;
将sql保存到一个文件中demo.sql
demo.sql的内容:
select * from table_name where ...
执行hive命令
hive -f demo.sql
ps:如果sql中需要有参数,而且和Linux shell脚本结合跑批
例子:
demo2.sql的内容:
insert overwrite table ods_yqg.fdm_yqg_user_asset_full partition(dt = "${p_date}")
select t.id,t.user_id,t.product_id,t.principal,t.asset,
t.return_accumulated,t.return_yesterday,t.last_update_date,t.time_updated
from tmp_export.user_asset_full as t where t.dt = "${p_date}";
通过hive -hivevar 加参数
test.sh的内容:
#!/usr/bin/env bash
start_date="$1"
hive -hivevar p_date=${start_date} -f /data/apps/yqg_datatask_cheetah/task/tempdata/load_tmp_export_asset_full_to_ods_yqg/hivescript.sql