如何向Hive/Beeline传递多个HQL文件并在单会话中执行?
Great question! When you run hive -f with multiple files, Hive indeed only processes the first one because the -f flag expects a single script file. Here are a few reliable ways to execute multiple HQL files in the same Hive session:
Method 1: Use Shell Concatenation with Pipe
You can concatenate the content of all your HQL files and pipe it directly to the Hive CLI. This ensures all commands run in the same session:
cat file_1.hql file_2.hql | hive
This works because the Hive CLI reads commands from standard input when no -f or -e flag is specified. Just make sure each HQL statement in your files ends with a semicolon (;) so Hive knows where one command ends and the next begins.
Method 2: Create a Master HQL Script with source Commands
Another clean approach is to create a master script that references your individual HQL files using Hive's built-in source command. This lets you control the execution order and even add extra logic if needed.
- Create a file named
master_script.hqlwith the following content:
-- Execute file_1 first source /path/to/file_1.hql; -- Then execute file_2 source /path/to/file_2.hql;
(Replace /path/to/ with the actual path to your HQL files, or use relative paths if they're in the same directory.)
- Run the master script with:
hive -f master_script.hql
The source command tells Hive to load and execute the content of the specified file within the current session, so all tables, variables, or session configurations from file_1.hql will persist when running file_2.hql.
Method 3: Use hive -e with Concatenated Content
If you prefer a one-liner without creating a master file, you can pass the concatenated content directly via the -e flag (which accepts a string of HQL commands):
hive -e "$(cat file_1.hql file_2.hql)"
Note that this might not be ideal for very large HQL files, as it can hit shell command-length limits, but it works well for smaller scripts.
内容的提问来源于stack exchange,提问作者Sano babu

