在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. vue通过路由实现页面刷新

    vue 开发微信商城项目,需求如下: 购物车页面跳转到详情页,购物车页面包含了多个组件,点击结算跳转到订单页面,从订单返回时,购物车页面没有刷新,由于购物车组件之间通过bus实现事件传递,页面跳转(非 ...

  2. 线性回归(regression)

    简介 回归分析只涉及到两个变量的,称一元回归分析.一元回归的主要任务是从两个相关变量中的一个变量去估计另一个变量,被估计的变量,称因变量,可设为Y:估计出的变量,称自变量,设为X. 回归分析就是要找出 ...

  3. 新手学python-Day4-作业

    购物车程序 要求: 1.启动程序后,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检查余额是否足够,够了就扣款,不够就提醒 4.可随时退出,退出时,打印已购买 ...

  4. [luogu] P4040 [AHOI2014/JSOI2014]宅男计划(贪心)

    P4040 [AHOI2014/JSOI2014]宅男计划 题目背景 自从迷上了拼图,JYY就变成了个彻底的宅男.为了解决温饱问题,JYY不得不依靠叫外卖来维持生计. 题目描述 外卖店一共有N种食物, ...

  5. 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] A】Paper Airplanes

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 统计每个人需要的sheet个数. 乘上k 然后除p就是需要的pack个数了 [代码] #include <bits/stdc+ ...

  6. 什么是PL/SQL,有什么用

    1.什么是PL/SQL,有什么用  Procedure Language+SQL  PL/SQL是Oracle数据库特有的编程语言.  PL/SQL程序是以SQL为基础,引入了  编程语言特点,例如变 ...

  7. mysql-5.7.10-winx64 安装

    安装ZIP中的EXE文件后,找到安装目录中的my-default.ini加入代码 1 2 3 4 5 6 #新设置的 [mysql] default-character-set=utf8 #新设置的 ...

  8. BTrace介绍和生产环境样例

    BTrace latest realese: release-1.2.5.1 BTrace guide(1.2-20101020): http://kenai.com/projects/btrace/ ...

  9. STM32F407VG (四)时钟配置

    1.STM32 F407VG 的starup_stm32f40_41xxx.s的例如以下位置调用 IMPORT SystemInit,之后调用main函数,所以 进入main函数时候就已经自己主动完毕 ...

  10. java mail邮件发送(带附件) 支持SSL

    java mail邮件发送(带附件)有三个类 MailSenderInfo.java package mail; import java.util.Properties; import java.ut ...