通过远程jdbc方式连接到hive数据仓库

来源:互联网 发布:创始于淘宝的女装品牌 编辑:程序博客网 时间:2024/06/10 19:57

1.启动hiveserver2服务器,监听端口10000

$>hive --service hiveserver2 &

netstat -anop | grep 10000

 

2.通过beeline命令行连接到hiveserver2

$>beeline //进入beeline命令行(hive --service beeline)

$beeline>!help //查看帮助

$beeline>!quit //退出

$beeline>!connect jdbc:hive2://localhost:10000/db2//连接到hibve数据

此时,会遇到以下问题:


解决方案:在hadoop>etc>hadoop>core-site.xml 中添加如下部分,重启服务即可:

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

遇到以下问题,请参考下面的解决方法


解决方法:

hadoop dfs -chmod -R777 /tmp

 

$beeline>show databases ;

$beeline>use mydb2 ;

$beeline>show tables; //显式表

3.、使用Hive-jdbc驱动程序采用jdbc方式访问远程数据仓库

1.创建java模块
2.引入maven
3.添加hive-jdbc依赖
<dependencies>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>

4.App

import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;/** * 使用jdbc方式连接到hive数据仓库,数据仓库需要开启hiveserver2服务。 */public class App {public static void main(String[] args) throws  Exception {Class.forName("org.apache.hive.jdbc.HiveDriver");Connection conn = DriverManager.getConnection("jdbc:hive2://192.168.52.201:10000/db2");Statement st = conn.createStatement();ResultSet rs = st.executeQuery("select id , name ,age from t");while(rs.next()){System.out.println(rs.getInt(1) + "," + rs.getString(2)) ;}rs.close();st.close();conn.close();}}