How to run one or more hive queries in a file using hive -f command

Contents

Hive -f command

The Hive -f command is used to execute one or more hive queries from a file in batch mode.Instead of enter into the Hive CLI and execute the queries one by one ,We can directly execute the set of queries using Hive -f option from the command line itself.

Syntax of Hive -f command

hive -f 

Example for Hive -f option

hive -f "/home/Revisitclass/test.sql"

The test.sql file contains around 3 queries and the queries include CREATE TABLE,INSERT INTO and SELECT statement at the end of the file. The file is stored under the directory /home/Revisitclass. Here the path of the file is given in the hive -f command.

The file test.sql contains the queries as below.

CREATE TABLE test_db.customer(
  id int,
  name string);

INSERT INTO test_db.customer
VALUES
(121,'Person1');

SELECT * FROM test_db.customer;

Once we hit the enter after this command, it will run all the hive queries in batch mode and returns the result set in the command line itself.

Output

log4j:WARN No such property [maxFileSize] in org.apache.log4j.DailyRollingFileAppender.

Logging initialized using configuration in file:/etc/hive/2.6.5.0-292/0/hive-log4j.properties
OK
Time taken: 1.167 seconds
Query ID = revisit_batch_20190609062152_ffc1e3fe-d918-4f2f-81d6-6f9e850ed6a6
Total jobs = 1
Launching Job 1 out of 1
Status: Running (Executing on YARN cluster with App id application_1554473216483_1056486)

--------------------------------------------------------------------------------
        VERTICES      STATUS  TOTAL  COMPLETED  RUNNING  PENDING  FAILED  KILLED
--------------------------------------------------------------------------------
Map 1 ..........   SUCCEEDED      1          1        0        0       0       0
--------------------------------------------------------------------------------
VERTICES: 01/01  [==========================>>] 100%  ELAPSED TIME: 6.66 s
--------------------------------------------------------------------------------
Loading data to table test_db.customer
Table test_db.customer stats: [numFiles=1, numRows=1, totalSize=20, rawDataSize=11]
OK
Time taken: 9.167 seconds
OK
121     Person1
Time taken: 0.199 seconds, Fetched: 1 row(s)

Related Articles : Hive -e command to run the query in Command line