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 ...
随机推荐
- Windows Vista 安装和使用指导 - 停止支持后的几条建议
简介 曾经被广大网民吐槽的Windows Vista现在已经淡出了人们的视线,但仍有一些朋友想要体验一下这个操作系统.Windows Vista是Windows发展路线上的里程碑,相比之前的Windo ...
- centos7下部署FastDFS分布式文件系统
前言 项目中用到文件服务器,有朋友推荐用FastDFS,所以就了解学习了一番,感觉确实颇为强大,在此再次感谢淘宝资深架构师余庆大神开源了如此优秀的轻量级分布式文件系统,本篇文章就记录一下FastDFS ...
- webpack加载器(Loaders)
加载器(Loaders) loader 是对应用程序中资源文件进行转换.它们是(运行在 Node.js 中的)函数,可以将资源文件作为参数的来源,然后返回新的资源文件. 示例 例如,你可以使用 loa ...
- js实现导航固定定位
js实现导航固定定位 <!DOCTY ...
- android studio2.2 配置NDK
1.配置环境: Android studio2.2 配置NDK NDK版本[android-ndk-r13b-windows-x86_64.zip] NDK下载网址:[https://dl.googl ...
- ASP.NET-SOAP、UDDI知识点
1. 什么是SOAP? 答:是简单访问协议.是在分布式环境中,交换信息并实现远程调用的协议.是一个基于XML的协议.使用SOAP,可以不考虑任何传输协议,但通常还是HTTP协议,可以允许任何类型的对象 ...
- windows下使用libsvm3.2
一.官方介绍 libsvm主页:https://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html libsvm介绍文档:http://www.csie.ntu. ...
- Intellij IDEA插件 - Scroll From Source
Intellij IDEA插件 - Scroll From Source 学习了:http://blog.csdn.net/luonanqin/article/details/41088171 可以自 ...
- Loopback測试软件AX1用户手冊 V3.1
点击:AX1 软件下载 1. 什么是AX1 AX1程序是基于windows的PC程序,用来评估 iinChip™的性能,也即是wiznet的硬件TCP/IP芯片. AX1通过网络与iinChip™评估 ...
- 玩转Android Camera开发(三):国内首发---使用GLSurfaceView预览Camera 基础拍照demo
GLSurfaceView是OpenGL中的一个类,也是能够预览Camera的,并且在预览Camera上有其独到之处. 独到之处在哪?当使用Surfaceview无能为力.痛不欲生时就仅仅有使用GLS ...