在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的更多相关文章

  1. Hive JDBC——深入浅出学Hive

    第一部分:搭建Hive JDBC开发环境 搭建:Steps •新建工程hiveTest •导入Hive依赖的包 •Hive  命令行启动Thrift服务 •hive --service hiveser ...

  2. JAVA jdbc获取数据库连接

    JDBC获取数据库连接的帮助类 import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManage ...

  3. 通过jdbc获取数据库中的表结构

    通过jdbc获取数据库中的表结构 主键 各个表字段类型及应用生成实体类   1.JDBC中通过MetaData来获取具体的表的相关信息.可以查询数据库中的有哪些表,表有哪些字段,字段的属性等等.Met ...

  4. JDBC获取表的主键

    JDBC获取表的主键 案例,创建订单,并根据订单号向订单明细表插入数据 sql语句: 创建两表 create table orders(  id number(4) primary key,  cus ...

  5. Hive学习之六 《Hive进阶— —hive jdbc》 详解

    接Hive学习五 http://www.cnblogs.com/invban/p/5331159.html 一.配置环境变量 hive jdbc的开发,在开发环境中,配置Java环境变量 修改/etc ...

  6. Hive 8、Hive2 beeline 和 Hive jdbc

    1.Hive2 beeline  Beeline 要与HiveServer2配合使用,支持嵌入模式和远程模式 启动beeline 打开两个Shell窗口,一个启动Hive2 一个beeline连接hi ...

  7. 使用JDBC获取能自动增加的主键

    本篇讲述如何使用JDBC获取能自动增加的主键的值.有时候我们在向数据库插入数据时希望能返回主键的值,而不是通过查询的方式.一般来说,在多表相互关联主键约束,也就是说别的表的外键约束是该表的主键,那么在 ...

  8. JDBC获取数据库Connection的工具抽取

    使用JDBC获取数据库的连接,大字分为三个步骤 1.获取驱动包名,定义URL,database_username,database_password 2.获取Connection对象 3.利用Conn ...

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

  10. 【Spring Boot】使用JDBC 获取相关的数据

    使用JDBC 获取相关的数据 什么是JDBC Java Database Connectivity 是一种用于执行SQL语句的Java API,与数据库建立连接.发送 操作数据库的语句并处理结果. S ...

随机推荐

  1. ubuntu12.04开启Framebuffer

    一.framebuffer概述 Framebuffer在Linux中是作为设备来实现的,它是对图形硬件的一种抽象,代表着显卡中的帧缓冲区(Framebuffer).通过Framebuffer设备,上层 ...

  2. 2019-03-19 用SSIS把SQLServer中的数据导出来保存到Excel中

    Control FLow 点击空白处,右键打开Variable,配置存储过程 Excel路径 在SQL Server 中新建一个存储过程,用于从数据表提取特定的数据 create proc Prici ...

  3. [AngularJS]Chapter 5 与服务器交互

    第八章有关于缓存的东西. [通过$http交互] 传统的AJAX请求如下 var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange ...

  4. HDU 4331 Contest 4

    一个很直观的想法是,求出每个点上下左右能到达的最大长度.然后枚举其斜边...没想到过了.... 当然,题解有一个很巧妙的优化,利用树状数组,那个太巧妙了. #include<iostream&g ...

  5. Oracle sql 子字符串长度判断

    Oracle sql 子字符串长度判断 select t.* from d_table t ,) ,instr(t.col,; 字符串的前两位都是数字: select * from d_table t ...

  6. [React] Integration test a React component that consumes a Render Prop

    In this lesson, I use Enzyme and Jest's Snapshot functionality to write an integration test for a co ...

  7. linux下使用DBCA(database configuration assistant)创建oracle数据库

    前提:切换到图形界面 到Oracle的bin文件夹下,使用oracle用户.运行dbca就可以.和windows的效果一样. 假设出现乱码 export LANG="en_US:UTF-8& ...

  8. Collection 和 Collections 的差别?

    Collection 是 java.util 下的接口,它是各种集合的父接口,继承于它的 接口主要有 Set 和 List:Collections 是个 java.util 下的类.是针对集合的 帮助 ...

  9. 多项福利回馈会员,且看Hao123怎样玩转“霸权主义”

        谈到"霸权主义",我们非常easy将其与国际政治联系在一起.只是.霸权主义可不全然用来形容政治,在7月14日,Hao123上线了一个会员福利活动,命名为"Hao1 ...

  10. JAVA设计模式之【观察者模式】

    观察者模式 交通信号灯是汽车的观察目标,汽车是观察者 一个对象的状态或行为的变化将导致其他对象的状态或行为也发生变化 为了描述这种一对多或一对一的联动,观察者模式应运而生 在观察者模式中,发生改变的对 ...