简体   繁体   中英

how to invoke shell script in hive

Could someone please explain me how to invoke a shell script from hive?. I explored on this and found that we have to use source FILE command to invoke a shell script from hive. But I am not sure how exactly I can call my shell script from hive using source File command. So can someone help me on this? Thanks in Advance.

To invoke a shell script through HIVE CLI, please look at the example below.

!sh file.sh; 

or

!./file.sh;

Please go though Hive Interactive Shell Commands section in the link below for more information. https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Cli

using ! <command> - Executes a shell command from the Hive shell. ! <command> - Executes a shell command from the Hive shell.

test_1.sh:

#!/bin/sh
echo "This massage is from $0 file"

hive-test.hql:

! echo showing databases... ; 
show databases;

! echo showing tables...;
show tables;

! echo runing shell script...;
! /home/cloudera/test_1.sh

output:

$ hive -v -f hive-test.hql
showing databases...

    show databases
OK
default
retail_edw
sqoop_import
Time taken: 0.997 seconds, Fetched: 3 row(s)
showing tables...

    show tables
OK
scala_departments
scaladepartments
stack
stackover_hive
Time taken: 0.062 seconds, Fetched: 4 row(s)
runing shell script...
This massage is from /home/cloudera/test_1.sh file

Don't know if it would suit you, but you can inverse your problem by launching the hive commands from the bash shell in combination with the hive queries results. You can even create a single bash script for this to combine your hive queries with bash commands in a single script:

#!/bin/bash
hive -e 'SELECT count(*) from table' > temp.txt
cat temp.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM