The impala-shell command works with any standard Unix toolchain. This page describes a variety of shell commands that can be used interactively or non-interactively.
The -q option allows you to run a single query without having to start the interactive interpreter. You can use this option to run impala-shell from a shell script or from a Python, Perl, or other script using the command invocation syntax.
The below example fetches the information from testdates table which is in dbTest database in Impala.
impala-shell -i quickstart.cloudera:21000 -d dbTest -q ‘select * from testdates’
-i refers to the impala-shell interpreter to specify the connection information for that instance of impalad.
quickstart.cloudera:21000 is the host and the port in which impala daemon is running.
-d refers to the database to connect
-q refers to query
You can use the -f option to process a file that contains several SQL statements, such as a series of reports or DDL statements to build a group of tables and views.
The below execution executes the local file and remains in non-interactive mode. This will usually be implemented for batch processing.
impala-shell -f sample.sql
The —var option allows you to pass replacement variables to statements run by that impala-shell session, such as those in a script file processed with the -f option. On the command line, use the format —var=variable name=value to encode the substitution variable. The syntax $var:variable name is used to substitute the value within a SQL statement. CDH 5.7 / Impala 2.5 and higher support this feature.
You can save query output to a file using the -o option.
The -B switch disables nice printing, allowing you to output comma-separated, tab-separated, or other delimited text files. (To change the delimiter character, use the —output delimiter option; the tab character is the default.)
Non-interactive mode prints query output to stdout or the file given by the -o option, while accidental output is printed to stderr, allowing you to process only the query output as part of a Unix pipeline.
Below is the syntax.
impala-shell -i servername:portname -B -q ‘select * from table’ -o filename ‘–output_delimiter=\delimiter’
Examples:
The below command exports the contents of the table into a file.
impala-shell -i quickstart.cloudera:21000
-d dbtest
-q ‘select * from region3’
-o region3results.csv
-d refers to database
-q refers to query
-o refers to output file name
The below command exports the query results into a file in a specified format
impala-shell -i quickstart.cloudera:21000 -B
-d dbtest
-q ‘select * from region3’
-o region3results.csv
‘–output_delimiter=\t’
Look at the screenshots for a better understanding.
Use the below command based on the Kerberos environment-
impala-shell -k -i quickstart.cloudera:21000 -B
-d dbtest
-q ‘select * from region3’
-o region3results.csv
‘–output_delimiter=\t’
Impala Interface:

Hope you liked this post.
Stay in touch for more interesting updates.
One comment