3

I have a hive query that has an echo command to print something. Below is a sample HQL file

!echo 'Total records';
use testDB;
select count(*) from tempTable;

After executing the query from hive using the command hive -f sample.hql > op.txt I would get the below output in the op.txt file.

Total records
231

What I want to know is how to achieve the same from beeline. If I run the same sample.hql file from beeline using the command beeline -u jdbc:hive2://localhost:10000/ --silent=true -f hive.hql > op.txt it fails. Below is the error I have received

Unknown command: echo 'Total records';

Do anyone know how to fix this problem?

Alex Raj Kaliamoorthy
  • 2,035
  • 3
  • 29
  • 46

1 Answers1

3

It is very simple
Replace your !echo 'Total records'; with !sh echo "Total records"; in your hive.hql file. It will work.
Enjoy!

aiman
  • 1,049
  • 19
  • 57
  • You are right but it will work only with Hive version 0.14 or later but what I had was 0.13. Anyhow thanks for your answer. – Alex Raj Kaliamoorthy May 16 '16 at 16:10
  • this post was regarding the beeline however you are asking a question from Hive. Nevertheless, I executed a similar query like you mentioned and I can get that executed successfully. But i did not understand `\"seperator\"` in your query. See my sample query that has worked for me `hive -database alexraj_testing -e 'desc sample; !echo ============; desc sample;'` – Alex Raj Kaliamoorthy Mar 07 '18 at 00:18
  • 1
    For me, `!sh echo "blah";` failed but `'!echo "blah";` works. The behavior I observe is that the ! works like in `vim`, what comes next gets interpreted by the shell. Since /bin/echo isn't a shell script (on my system, it's a compiled executable), sh can't run /bin/echo as a script. – JawguyChooser Oct 26 '18 at 15:35