How to use variables in Hive while run the queries?
Contents
hivevar option in Hive
The hivevar option is used to create the custom variables and pass the values to the variable while run the queries in Hive.
Syntax of hivevar option:
1 |
--hivevar <variable=value> |
The value will be substituted to the variable that used in the hive query.
Example:
1 2 3 4 5 |
USE ${database}; create table customer_Txn (account_id varchar(40), acct_balance decimal(8,4), acct_txn_amount decimal(8,4)); |
We have created the custom variable as ${database} in the above hive query. The query is saved in the file create.hql in the following directory /x/home/revisit_user1. The value for the variable ${database} will be passed using the hivevar option.
Hive command to pass the value to the variable and execute the queries
1 |
hive --hivevar database=Banking_Db -f /x/home/revisit_user1/test_create.hql |
Here we are passing the value “Banking_Db” to the variable database using the –hivevar option and -f is used the execute the hive queries that are present inside the file.
Once we hit the enter after this command, the value will substituted and queries will be executed in the Hive.
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
hive> show create table Banking_Db.customer_txn; OK CREATE TABLE `customer_txn`( `account_id` varchar(40), `acct_balance` decimal(8,4), `acct_txn_amount` decimal(8,4)) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe' STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 'hdfs://revisit/hive/warehouse/Banking_Db.db/customer_txn' TBLPROPERTIES ( 'COLUMN_STATS_ACCURATE'='{\"BASIC_STATS\":\"true\"}', 'numFiles'='0', 'numRows'='0', 'rawDataSize'='0', 'totalSize'='0', 'transient_lastDdlTime'='1560532281') Time taken: 0.321 seconds, Fetched: 19 row(s) |
Recommended Articles
- How to execute Hive Queries using Shell script?
- How to run one or more hive queries in a file using hive -f command?