hive基本操作2:hive的连接方式

操作1. hiveserver2命令直接执行,以runjar进程存在,卡住不动正常
命令:$>hiveserver2
-使用jdbc编写外部程序操作hive,需要打开hiveserver2服务
hive server2 本身是一个服务 RunJar
hive 对外提供的连接端口
10000:外部连接端口
10002:webUI端口
http://192.168.110.3:10002/hiveserver2.jsp 连接hive server2 的webUI

操作2. beeline(对cli的优化, hive 命令 cli 和 beeline 2选一)
在使用beeline之前要开启hiveserver2服务
在另一台机器上敲入以下命令

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

进入到beeline窗口之后,操作与cli相同,数据展示格式更好看
退出连接好的beeline命令

!quit 

前提:
修改Hadoop的core-site.xml
------------------------------

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

去hive-site.xml找到并进行修改

  <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>

操作3. hive cli

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

在hive2.0之前,最常用的一种命令,只能在本机运行。

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

执行完后直接退出hive,回到Linux命令行窗口
-f 在Linux中直接执行某个hql文件

  $>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命令行窗口

操作4:hive cli退出操作

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

操作5:直接在hive cli 命令行下执行hdfs

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

直接在hive cli 命令行下执行centos

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

操作6:设置hive,使得cli客户端展示查询结果的头信息和当前数据库信息

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

操作7:参数配置方式
7.1配置文件的方式(永久生效)
7.2通过hive命令行(cli)进行配置(临时生效,仅对当前hive连接客户端生效)

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

操作8:使用jdbc编写外部程序操作hive,需要打开hiveserver2服务

$>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();
}

猜你喜欢

转载自blog.csdn.net/weixin_44703894/article/details/114904796
今日推荐