Skip to content
Advertisement

Is it possible to choose to select from which database in Hive

I have one table but in two different databases. How can I query to based on some flag to select only from one of them?

For example:

   insert into db2.table2
   select * from db1.table1 if flag is true else select * from db2.table

What’s the corresponding query (that works) to this sudo query?

Advertisement

Answer

You can pass database parameter using shell

#!/bin/bash

#calculate database as you want here
db=db1

#substitute variable and execute
hive -e "insert into db2.table2
   select * from $db.table1"

You can do similar in Hive using UNION ALL, though it is not efficient way:

INSERT OVERWRITE TABLE OUTPUT_TABLE                                                                                                                                      
 SELECT * FROM DB1.TABLE1 WHERE (${FLAG_one} = TRUE) --any boolean condition
 UNION ALL
 SELECT * FROM DB2.TABLE1 WHERE  (${FLAG_one} = FALSE) --any boolean condition
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement