Using the Hive command

Hive first started with hiveserver1. However, this version of Hive server was not very stable. It sometimes suspended or blocked the client’s connection quietly. Since v0.11.0, Hive has included a new thrift server called hivesever2 to replace hiveserver1hiveserver2 has an enhanced server designed for multiple client concurrency and improved authentication. It also recommends using beeline as the major Hive command-line interface instead of the hive command. The primary difference between the two versions of servers is how the clients connect to them. hive is an Apache-Thrift-based client, and beeline is a JDBC client. The hive command directly connects to the Hive drivers, so we need to install the Hive library on the client. However, beeline connects to hiveserver2 through JDBC connections without installing Hive libraries on the client. That means we can run beeline remotely from outside the cluster. For more usage of hiveserver2 and its API access, refer to https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients.

The following two tables list the commonly-used commands in different command modes considering different user preferences:

Purposehiveserver2 - beelinehiveserver1 - hive
Connect serverbeeline –u <jdbc_url>hive -h <hostname> -p <port>
Helpbeeline -hhive -H
Run querybeeline -e "hql query"
beeline -f hql_query_file.hql
beeline -i hql_init_file.hql
hive -e "hql query"
hive -f hql_query_file.hql
hive -i hql_init_file.hql
Setvariablebeeline --hivevar var_name=var_valuehive --hivevar var_name=var_value
Purposehiveserver2 - beelinehiveserver1 - hive
Enter modebeelinehive
Connect server!connect <jdbc_url>N/A
List tables!table
show tables; --also support
show tables;
List columns!column table_name
desc table_name;
desc table_name;
Run queryselect * from table_name;select * from table_name;
Save result!record result_file.dat
!record
N/A
Run shell cmd!sh ls!ls;
Run dfs cmddfs -ls;dfs -ls;
Run hql file!run hql_query_file.hqlsource hql_query_file.hql;
Quit mode!quitquit;

In addition, Hive configuration settings and properties can be accessed and overwritten by theSET keyword in the interactive mode. For more details, refer to the Apache Hive wiki at https://cwiki.apache.org/confluence/display/Hive/Configuration+Properties.
For beeline, ; is not needed after the command that starts with !. Both commands do not support running a pasted query with <tab> inside, because <tab> is used for auto-complete by default in the environment. Alternatively, running the query from files has no such issues. In interactive mode, we can use the keyboard’s up and down arrow keys to retrieve the previous commands. The !history command can be used in beeline to show the command’s history.  In addition, the dfs command may be disabled in beeline for permissions control in some Hadoop distributions. Both commands support variable substitution, which refers to https://cwiki.apache.org/confluence/display/Hive/LanguageManual+VariableSubstitution.