Hive basic operation 2: hive connection mode

Operation 1. The hiveserver2 command is directly executed, and the runjar process exists, and it is stuck normally.
Command: -Use $>hiveserver2
jdbc to write an external program to operate hive, you need to open the hiveserver2 service
hive server2 itself is a service RunJar
hive provides external connection port
10000: external connection Port
10002: webUI port
http://192.168.110.3:10002/hiveserver2.jsp to connect to the webUI of hive server2

Operation 2. beeline (optimization of cli, choose one of hive command cli and beeline 2)
before using beeline, you need to open the hiveserver2 service
and type the following command on another machine

$>beeline
$beeline>! connect jdbc:hive2://192.168.235.136:10000/db1

After entering the beeline window, the operation is the same as the cli, and the data display format is better.
Exit the connected beeline command

!quit 

Prerequisite:
Modify Hadoop's core-site.xml
------------------------------

<property>
  <name>hadoop.proxyuser.hiveyd.hosts</name>
  <value>*</value>
</property>
<property>
  <name>hadoop.proxyuser.hiveyd.groups</name>
  <value>*</value>
</property>
	----------------------------------------

Go to hive-site.xml to find and modify

  <property>
    <name>hive.server2.thrift.client.user</name>
    <value>root</value>
    <description>Username to use against thrift client</description>
  </property>
  <property>
    <name>hive.server2.thrift.client.password</name>
    <value>root</value>
    <description>Password to use against thrift client</description>
  </property>

Operation 3. hive cli

$>hive
------------

Before hive2.0, the most commonly used command can only be run on the machine.

$> hive --service cli  等同于 $>hive
	-e 在Linux中直接使用hive命令执行某条Hql语句
$>hive -e "select * from db1.employee"

Exit hive directly after execution, and return to the Linux command line window
-f Execute a hql file directly in Linux

  $>cd ~
			$>mkdir hql
			$>touch hive1.hql
			$>vi hive1.hql
		    $>cat hive1.hql
		  --------------
 		 use db1;
		  select * from db1.employee;
		$>hive -f ~/hql/hive1.hql	直接在命令行打印
hive -f ~/hql/hive1.hq > ~/file/hive/rs1.txt(提前创建,输出到该文件)
		执行完后直接退出hive,回到Linux命令行窗口

Operation 4: hive cli exit operation

$hive>quit;:直接退出
$hive>exit; 在hive1先提交数据再退出
在hive2两者无区别

Operation 5: Execute hdfs directly under the hive cli command line

$hive>dfs -lsr /
$hive>dfs -chmod 777/

Execute centos directly under the hive cli command line

$hive>!cat /home/centos/file/hive/rs1.txt
$hive>!clear  

Operation 6: Set hive so that the cli client displays the header information of the query result and current database information

hive-site.xml
hive.cli.print.header = true (默认为false)
hive.cli.print.current.db=true((默认为false)

Operation 7: Parameter configuration mode
7.1 Configuration file mode (permanent effect)
7.2 Configuration via hive command line (cli) (valid temporarily, only valid for the current hive connected client)

$>set  hive.cli.print.header = true;
$>set  hive.cli.print.current.db=true;

Operation 8: Use jdbc to write external programs to operate hive, you need to open the hiveserver2 service

$>hiveserver2

   1.导入hive相关依赖
    <dependencies>
      <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>2.1.1</version>
      </dependency>
    </dependencies>
  2.编写hive的jdbc程序
    public static void main(String[] args) throws Exception{
    
    
	    Class.forName("org.apache.hive.jdbc.HiverDriver");
		Connection conn=DriverManger.getConnection
("jdbc:hive2://192.168.235.136:10000/db1","root","root");
		Statement stmt=conn.createStatement();
		ResultSet rs = stmt.executeQuery("select * from employee");
		while(rs.next()){
    
    
		    System.out.println(rs.getInt("id"),rs.getString("name"),rs.getInt("age"));
		}
    
  rs.close();
	    stmt.close();
	    conn.close();
}

Guess you like

Origin blog.csdn.net/weixin_44703894/article/details/114904796