通过JDBC连接HiveServer2
如果通过JDBC连接HiveServer2时提示:User: hive is not allowed to impersonate hive,需要在core-site.xml中新增如下配置:
hadoop.proxyuser.hive.hosts=*
hadoop.proxyuser.hive.groups=*
红色部分表示可通过代理用户hive操作的主机和用户组,蓝色部分表示所有的hadoop主机及该主机上的hadoop用户组。 连接代码如下:
package com.mengyao.bigdata.utils; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; /**
* HiveJdbcUtil
* core-site.xml配置hive连接用户通过代理操作任意hadoop的用户组及主机
* hadoop.proxyuser.hive.hosts=*
* hadoop.proxyuser.hive.groups=*
* @author mengyao
*
*/
@Component
public class HiveJdbcUtil { private static HiveJdbcUtil hiveJdbcUtil;
@Value("${hive.driverClassName}")
private String driverClassName = "org.apache.hive.jdbc.HiveDriver";
@Value("${hive.url}")
private String url = "jdbc:hive2://h3:10000/default?mapred.job.queue.name=default;hive.mapred.mode=nonstrict";
@Value("${hive.username}")
private String username = "hive";
@Value("${hive.password}")
private String password = "hive";
private Connection connection; /**
* 私有构造函数声明
*/
private HiveJdbcUtil() {
try {
Class.forName(driverClassName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} /**
* 获取HiveJdbcUtil单例对象
* @return
*/
public static HiveJdbcUtil getInstance() {
if (null == hiveJdbcUtil) {
hiveJdbcUtil = new HiveJdbcUtil();
}
return hiveJdbcUtil;
} /**
* 获取HiveJdbc连接
* @return
*/
public Connection getConnection() {
try {
connection = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
} /**
* 关闭连接
* @param rs
* @param ps
* @param st
* @param connection
*/
public void closeAll(ResultSet rs, PreparedStatement ps, Statement st, Connection connection) {
try {
if (null != rs) {
rs.close();
}
if (null != ps) {
ps.close();
}
if (null != st) {
st.close();
}
if (null != connection) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
} public static void main(String[] args) {
HiveJdbcUtil instance = HiveJdbcUtil.getInstance();
Connection connection_ = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection_ = instance.getConnection();
ps = connection_.prepareStatement("show databases");
rs = ps.executeQuery();
while(rs.next()) {
System.out.println("==== "+rs.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
instance.closeAll(rs, ps, null, connection_);
}
} }
User: hive is not allowed to impersonate hive
通过JDBC连接HiveServer2的更多相关文章
- 用Java代码通过JDBC连接Hiveserver2
1.在终端启动hiveserver2#hiveserver2 2.使用beeline连接hive另外打开一个终端,输入如下命令(xavierdb必须是已经存在的数据库)#beeline -u jdbc ...
- (转)CDH中启动的hive,使用jdbc连接hiveServer2时候权限不足解决方案
Hive JDBC:java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.securi ...
- 大数据系列之数据仓库Hive命令使用及JDBC连接
Hive系列博文,持续更新~~~ 大数据系列之数据仓库Hive原理 大数据系列之数据仓库Hive安装 大数据系列之数据仓库Hive中分区Partition如何使用 大数据系列之数据仓库Hive命令使用 ...
- 通过JDBC连接hive
hive是大数据技术簇中进行数据仓库应用的基础组件,是其它类似数据仓库应用的对比基准.基础的数据操作我们可以通过脚本方式以hive-client进行处理.若需要开发应用程序,则需要使用hive的jdb ...
- 基于CDH5.x 下面使用eclipse 操作hive 。使用java通过jdbc连接HIVESERVICE 创建表
基于CDH5.x 下面使用eclipse 操作hive .使用java通过jdbc连接HIVESERVICE 创建表 import java.sql.Connection; import java.s ...
- 【原创】大叔经验分享(38)beeline连接hiveserver2报错impersonate
beeline连接hiveserver2报错 Error: Could not open client transport with JDBC Uri: jdbc:hive2://localhost: ...
- Hive(3)-meta store和hdfs详解,以及JDBC连接Hive
一. Meta Store 使用mysql客户端登录hadoop100的mysql,可以看到库中多了一个metastore 现在尤其要关注这三个表 DBS表,存储的是Hive的数据库 TBLS表,存储 ...
- Java使用JDBC连接Hive
最近一段时间,处理过一个问题,那就是hive jdbc的连接问题,其实也不是大问题,就是url写的不对,导致无法连接.问题在于HiveServer2增加了别的安全验证,导致正常的情况下,传递的参数无法 ...
- Java使用JDBC连接Impala
前段时间,有一个项目在连接Impala的时候,可以测试连接成功,但是查询不出表.但是通过impala-shell的时候,是可以查询出来的,我觉的这种方式查询出来的话,可能和jdbc的方式不一样,因为i ...
随机推荐
- Chrome Extensions API & options
Chrome Extensions API options https://developer.chrome.com/extensions https://developer.chrome.com/e ...
- git & configs
git & configs https://alvinalexander.com/git/git-show-change-username-email-address https://stac ...
- vs2015常用代码块与自定义代码块
常用代码块 代码段名 描 述 #if 该代码段用#if和#endif命令围绕代码 #region 该代码段用#region和#endregion命令围绕代码 ~ 该代码段插入一个析构函数 att ...
- RT-thread内核之异常与中断
一.什么是中断? 中断有两种,一种是CPU本身在执行程序的过程中产生的,一种是由CPU外部产生的. cpu外部中断,就是通常所讲的“中断”(interrupt).对于执行程序来说,这种“中断”的发生完 ...
- P1349 广义斐波那契数列
题目描述 广义的斐波那契数列是指形如an=p*an-1+q*an-2的数列.今给定数列的两系数p和q,以及数列的最前两项a1和a2,另给出两个整数n和m,试求数列的第n项an除以m的余数. 输入输出格 ...
- CentOS httpd服务(Apache)
1.从ISO镜像安装,Apache 服务的软件包名称为 httpd #检查源配置[root@localhost media]# cat /etc/yum.repos.d/CentOS-Media.re ...
- 【题解】CF#855 G-Harry Vs Voldemort
个人感觉挺有意思的,然而被颜神D无聊惹(- ̄▽ ̄)- 这题我们可以首先试图去统计以每一个点作为 w 点所能对答案造成的贡献是多少.不难发现,当且仅当 u 和 v 都在 w 所在边双的一侧的时候不能构成 ...
- [bzoj4391] [Usaco2015 dec]High Card Low Card 贪心 线段树
---题面--- 题解: 观察到以决策点为分界线,以点数大的赢为比较方式的游戏都是它的前缀,反之以点数小的赢为比较方式的都是它的后缀,也就是答案是由两段答案拼凑起来的. 如果不考虑判断胜负的条件的变化 ...
- [Leetcode] maximun subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [Leetcode] jump game ii 跳跃游戏
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...