Monday 26 December 2022

hive: How to run the commands from a file in beeline?

Approach 1: In non-interactive mode

Syntax

beeline -u jdbc:hive2:// -f {FILE_LOCATION}

 

Example

beeline -u jdbc:hive2:// -f script.hql

 

Let’s create a file and add some hql commands to it.

 

script.hql
SHOW DATABASES;
SHOW TABLES;
SELECT * FROM emp;

  We can execute the commands in script.hql file by executing below command.

 

beeline -u jdbc:hive2:// -f script.hql

$ beeline -u jdbc:hive2:// -f script.hql 
scan complete in 1ms
Connecting to jdbc:hive2://
Connected to: Apache Hive (version 1.1.0-cdh5.13.0)
Driver: Hive JDBC (version 1.1.0-cdh5.13.0)
Transaction isolation: TRANSACTION_REPEATABLE_READ
0: jdbc:hive2://> SHOW DATABASES;
OK
+----------------+--+
| database_name  |
+----------------+--+
| default        |
+----------------+--+
1 row selected (0.573 seconds)
0: jdbc:hive2://> SHOW TABLES;
OK
+-----------+--+
| tab_name  |
+-----------+--+
| emp       |
+-----------+--+
1 row selected (0.038 seconds)
0: jdbc:hive2://> SELECT * FROM emp;
OK
+---------+-----------+---------------------------------+---------------------------------+-------------------------------+-------------+-------------+--+
| emp.id  | emp.name  |           emp.hobbies           |    emp.technology_experience    |        emp.gender_age         | emp.rating  | emp.salary  |
+---------+-----------+---------------------------------+---------------------------------+-------------------------------+-------------+-------------+--+
| 1       | Hari      | ["Football","Cricket"]          | {"Java":"3.4Yrs","C":"4.5Yrs"}  | {"gender":"Male","age":30}    | -1.5        | 1000000.0   |
| 2       | Chamu     | ["Trekking","Watching movies"]  | {"Selenium":"5.6Yrs"}           | {"gender":"Female","age":38}  | 3.0         | 2500000.0   |
| 3       | Sailu     | ["Chess","Listening to music"]  | {"EmbeddedC":"9Yrs"}            | {"gender":"Female","age":32}  | 2.5         | 1300000.0   |
| 4       | Gopi      | ["Cricket"]                     | {"Datastage":"11Yrs"}           | {"gender":"Male","age":32}    | -0.7        | 8.1E7       |
| 5       | Rahim     | []                              | {}                              | {"gender":"Male","age":null}  | -0.7        | 500000.0    |
+---------+-----------+---------------------------------+---------------------------------+-------------------------------+-------------+-------------+--+
5 rows selected (0.446 seconds)
0: jdbc:hive2://> 
0: jdbc:hive2://> 
Closing: 0: jdbc:hive2://
[cloudera@quickstart hive]$

 

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment