Hive学习-高级版一(其他客户端使用介绍)

操作hive的方法前面只介绍了hive客户端方式,但是被官方定义为过时(虽然还是最常用的),其他操作hive的方式有beeline、webUI、JavaAPI(官方最推荐的方式是beeline)。这几种客户端方式需要服务hiveserver2的支持,所以首先我们需要先启动该服务。

1、hiveserver2启动

默认启动方式,默认端口10000

$ ./hiveserver2

$ hive --service hiveserver2

修改启动端口

./hiveserver2 --hiveconf hive.server2.thrift.port=14000

2、beeline连接

第一种连接方式,模式如下:

$ bin/beeline

beeline> !connect jdbc:hive2://localhost:10000 username password

默认是没有密码的,默认用户是hdfs默认的超级用户hadoop,hive-site.xml可以设置具体的验证方式和相应参数

$ ./beeline

beeline>!connect jdbc:hive2://localhost:10000 hadoop

登录成功后即可按照正常的HQL操作hive了

3、JavaAPI

添加依赖

groupId:org.apache.hive

artifactId:hive-jdbc

version:对应hive版本

import java.sql.SQLException;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;

import java.sql.DriverManager;

public class HiveJdbcClient {

private static String driverName ="org.apache.hive.jdbc.HiveDriver";

public static void main(String[]args)throws SQLException {

try {

Class.forName(driverName);

}catch (ClassNotFoundException e) {

e.printStackTrace();

System.exit(1);

}

Connection con =DriverManager.getConnection("jdbc:hive2://192.168.205.131:10000/test","hadoop","");

Statement stmt =con.createStatement();

String tableName ="a";

ResultSet res =stmt.executeQuery("select * from  " +tableName);

while (res.next()) {

System.out.println(res.getString(1) +"\t" +res.getString(2));

}

}

}

注意:从官方网站复制代码需要注意两点,默认的是hive1的连接方式,不是hiveserver2的,需要修改驱动和URL

4、WebUI的方式

推荐使用HUE,不推荐使用hive自带的UI

你可能感兴趣的:(Hive学习-高级版一(其他客户端使用介绍))