Hive-jdbc获取sessionId
在HiveStatement中有一个sessHandle:
public class HiveStatement implements java.sql.Statement {
...
private final TSessionHandle sessHandle; // 这个代表了hive的session,通过sessionId可以去hive服务器或者hadoop目录中获取hive的查询日志,在hiveserver2中,这个sessionId是UUID类生成的
...
TSessionHandle有个getSessionId方法可以获取到sessionId
public class TSessionHandle implements org.apache.thrift.TBase<TSessionHandle, TSessionHandle._Fields>, java.io.Serializable, Cloneable {
...
private THandleIdentifier sessionId; // required
...
public THandleIdentifier getSessionId() {
return this.sessionId;
}
...
THandleIdentifier类有guid和secret
public class THandleIdentifier implements org.apache.thrift.TBase<THandleIdentifier, THandleIdentifier._Fields>, java.io.Serializable, Cloneable {
...
private ByteBuffer guid; // 这个代表了sessionId的字符串(UUID)
private ByteBuffer secret; // required
...
但是guid是个ByteBuffer,不知道怎么反序列化成字符串
后来我看了hiveserver2的代码,在hive-service.jar中,有个HandleIdentifier类可以对THandleIdentifier的byte数据进行反序列化:
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hive.service.cli;
import java.nio.ByteBuffer;
import java.util.UUID;
import org.apache.hive.service.cli.thrift.THandleIdentifier;
/**
* HandleIdentifier.
*
*/
public class HandleIdentifier {
private final UUID publicId;
private final UUID secretId;
public HandleIdentifier() {
publicId = UUID.randomUUID();
secretId = UUID.randomUUID();
}
public HandleIdentifier(UUID publicId, UUID secretId) {
this.publicId = publicId;
this.secretId = secretId;
}
public HandleIdentifier(THandleIdentifier tHandleId) {
ByteBuffer bb = ByteBuffer.wrap(tHandleId.getGuid());
this.publicId = new UUID(bb.getLong(), bb.getLong());
bb = ByteBuffer.wrap(tHandleId.getSecret());
this.secretId = new UUID(bb.getLong(), bb.getLong());
}
public UUID getPublicId() {
return publicId;
}
public UUID getSecretId() {
return secretId;
}
public THandleIdentifier toTHandleIdentifier() {
byte[] guid = new byte[16];
byte[] secret = new byte[16];
ByteBuffer guidBB = ByteBuffer.wrap(guid);
ByteBuffer secretBB = ByteBuffer.wrap(secret);
guidBB.putLong(publicId.getMostSignificantBits());
guidBB.putLong(publicId.getLeastSignificantBits());
secretBB.putLong(secretId.getMostSignificantBits());
secretBB.putLong(secretId.getLeastSignificantBits());
return new THandleIdentifier(ByteBuffer.wrap(guid), ByteBuffer.wrap(secret));
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((publicId == null) ? 0 : publicId.hashCode());
result = prime * result + ((secretId == null) ? 0 : secretId.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof HandleIdentifier)) {
return false;
}
HandleIdentifier other = (HandleIdentifier) obj;
if (publicId == null) {
if (other.publicId != null) {
return false;
}
} else if (!publicId.equals(other.publicId)) {
return false;
}
if (secretId == null) {
if (other.secretId != null) {
return false;
}
} else if (!secretId.equals(other.secretId)) {
return false;
}
return true;
}
@Override
public String toString() {
return publicId.toString();
}
}
这个类没有依赖太多东西,可以直接复制到自己项目中使用
String sessionId = new HandleIdentifier(sessHandle).getPublicId();
这样行了
然后hive会把一个session的日志记录在本地的一个文件(${hive.exec.local.scratchdir}/${user}/${sessionId})和hdfs的一个文件(${hive.exec.scratchdir}/${user}/${sessionId})中,可以去跟踪这些文件去看sql的执行情况。
更简单的方法是调用HiveStatement.getQueryLog()去获取查询日志
Hive-jdbc获取sessionId的更多相关文章
- Hive JDBC——深入浅出学Hive
第一部分:搭建Hive JDBC开发环境 搭建:Steps •新建工程hiveTest •导入Hive依赖的包 •Hive 命令行启动Thrift服务 •hive --service hiveser ...
- JAVA jdbc获取数据库连接
JDBC获取数据库连接的帮助类 import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManage ...
- 通过jdbc获取数据库中的表结构
通过jdbc获取数据库中的表结构 主键 各个表字段类型及应用生成实体类 1.JDBC中通过MetaData来获取具体的表的相关信息.可以查询数据库中的有哪些表,表有哪些字段,字段的属性等等.Met ...
- JDBC获取表的主键
JDBC获取表的主键 案例,创建订单,并根据订单号向订单明细表插入数据 sql语句: 创建两表 create table orders( id number(4) primary key, cus ...
- Hive学习之六 《Hive进阶— —hive jdbc》 详解
接Hive学习五 http://www.cnblogs.com/invban/p/5331159.html 一.配置环境变量 hive jdbc的开发,在开发环境中,配置Java环境变量 修改/etc ...
- Hive 8、Hive2 beeline 和 Hive jdbc
1.Hive2 beeline Beeline 要与HiveServer2配合使用,支持嵌入模式和远程模式 启动beeline 打开两个Shell窗口,一个启动Hive2 一个beeline连接hi ...
- 使用JDBC获取能自动增加的主键
本篇讲述如何使用JDBC获取能自动增加的主键的值.有时候我们在向数据库插入数据时希望能返回主键的值,而不是通过查询的方式.一般来说,在多表相互关联主键约束,也就是说别的表的外键约束是该表的主键,那么在 ...
- JDBC获取数据库Connection的工具抽取
使用JDBC获取数据库的连接,大字分为三个步骤 1.获取驱动包名,定义URL,database_username,database_password 2.获取Connection对象 3.利用Conn ...
- Hive JDBC:java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException): User: root is not allowed to impersonate anonymous
今天使用JDBC来操作Hive时,首先启动了hive远程服务模式:hiveserver2 &(表示后台运行),然后到eclipse中运行程序时出现错误: java.sql.SQLExcepti ...
- 【Spring Boot】使用JDBC 获取相关的数据
使用JDBC 获取相关的数据 什么是JDBC Java Database Connectivity 是一种用于执行SQL语句的Java API,与数据库建立连接.发送 操作数据库的语句并处理结果. S ...
随机推荐
- BZOJ 3295 [CQOI2011]动态逆序对 (三维偏序CDQ+树状数组)
题目大意: 题面传送门 还是一道三维偏序题 每次操作都可以看成这样一个三元组 $<x,w,t>$ ,操作的位置,权值,修改时间 一开始的序列看成n次插入操作 我们先求出不删除时的逆序对总数 ...
- 半虚拟化驱动virtio-Windows
下载网站 Downloads - KVM http://www.linux-kvm.org/page/Downloads yum 安装: [root@kvm-server ~]# wget https ...
- js 如何判断浏览器是否禁用cookie
语法:navigator.cookieEnabled: 如果浏览器启用了cookie,该属性值为true.如果禁用了cookie,则值为false. navigator: JavaScript中的一个 ...
- [luogu] P3210 [HNOI2010]取石头游戏(贪心)
P3210 [HNOI2010]取石头游戏 题目描述 A 公司正在举办一个智力双人游戏比赛----取石子游戏,游戏的获胜者将会获得 A 公司提供的丰厚奖金,因此吸引了来自全国各地的许多聪明的选手前来参 ...
- keil5下载程序时出现“internal command error”解决方法
今天下载的时候,插入下载器,查看Debug可用看到“internal command error”,一直找不到原因,后来查看上一个工程才发现,上一个程序禁用了Seral Wire 引脚,导致的. 解决 ...
- UVALive 5545 Glass Beads
Glass Beads Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Origin ...
- Ubuntu 安装wps-office
本系列文章由 @YhL_Leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50288483 本人的ubuntu系统是 ...
- 关于nodejs的线程模型可以看这篇文章
虽然还是有一些没有讲全,但是整体还是讲的很不错的. http://www.ruanyifeng.com/blog/2014/10/event-loop.html
- 取消记录tableView选中效果
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self. ...
- iOS企业证书网页分发全过程具体解释(图文并茂史无前例的具体哦)
iOS企业证书网页分发全过程具体解释 苹果的企业级证书公布的应用.是不用设备授权就可以直接安装,而且不限设备上限. 为了方便分发,苹果有协议实现通过网页链接直接下载安装企业级的应用. 首先须要说明它的 ...