package andes;

import java.io.BufferedWriter;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.ResultSetMetaData;

import java.sql.SQLException;

import java.sql.Statement;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.security.UserGroupInformation;

public class Hive2JdbcTest {

// org.apache.hadoop.hive.jdbc.HiveDriver

// org.apache.hive.jdbc.HiveDriver

private static String driverName = "org.apache.hive.jdbc.HiveDriver";

public static void main(String[] args) throws SQLException {

try {

Class.forName(driverName);

} catch (ClassNotFoundException e) {

e.printStackTrace();

System.exit(1);

}

Configuration conf = new Configuration();

conf.setBoolean("hadoop.security.authorization", true);

conf.set("hadoop.security.authentication", "kerberos");

try {

UserGroupInformation.loginUserFromKeytab("user@domain",

"/home/user/user.keytab");

} catch (IOException e) {

e.printStackTrace();

}

Connection con = DriverManager

.getConnection(

"jdbc:hive2://host:port/db;principal=hive/host

@domain",

"", "");

Statement stmt = con.createStatement();

// stmt.executeQuery("use db");

String sql = "select * from table 2000";

System.out.println("Running: " + sql);

ResultSet res = stmt.executeQuery(sql);

// dump res to file

try {

write2file("c:\\work\\jdbctest.txt", res);

} catch (IOException e) {

e.printStackTrace();

}

res.close();

}

static void write2file(String filepath, ResultSet res) throws SQLException,

IOException {

// filepath="c:\\work\\jdbctest.txt";

FileOutputStream fout;

fout = new FileOutputStream(filepath);

BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fout,

"UTF-8"));

ResultSetMetaData meta = res.getMetaData();

int numberOfColumns = meta.getColumnCount();

while (res.next()) {

for (int i = 1; i < numberOfColumns; i++) {

bw.write(String.valueOf(res.getObject(i)));

bw.write('\t');

}

bw.write(String.valueOf(res.getObject(numberOfColumns)));

bw.newLine();

}

bw.close();

System.out.println("finished");

}

}

java command to run jar file:

java -classpath ".:/home/test/tmp/andes/*:" andes.HiveJdbcClient /home/test/tmp/andes/test.keytab

Hive2 jdbc test的更多相关文章

  1. impala系列: 基本命令和jdbc连接

    --======================= 使用impala-shell 登录 --======================= impala-shell --auth_creds_ok_i ...

  2. Hive 8、Hive2 beeline 和 Hive jdbc

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

  3. 从零自学Hadoop(18):Hive的CLI和JDBC

    阅读目录 序 Hive CLI(old CLI) Beeline CLI(new CLI) JDBC Demo下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出 ...

  4. hive2.1.0安装

    下载hive(http://mirrors.cnnic.cn/apache/hive/) 或者 http://archive.apache.org/dist/hive/(hive历史版本) 在本地进行 ...

  5. CentOS7安装hive-2.1.0

    环境: CentOS7 Hadoop-2.6.4,配置两个节点:master.slave1 mysql-server 过程: 下载.解压hive-2.1.0到/usr/hadoop-2.6.4/thi ...

  6. 通过JDBC连接hive

    hive是大数据技术簇中进行数据仓库应用的基础组件,是其它类似数据仓库应用的对比基准.基础的数据操作我们可以通过脚本方式以hive-client进行处理.若需要开发应用程序,则需要使用hive的jdb ...

  7. jdbc调用sparksql

    将hive-site.xml拷贝到spark目录下conf文件夹 local模式 spark-sql --driver-class-path /usr/local/hive-1.2.1/lib/mys ...

  8. SparkSQL使用之JDBC代码访问Thrift JDBC Server

    启动ThriftJDBCServer: cd $SPARK_HOME/sbin start-thriftserver.sh & 使用jdbc访问ThriftJDBCServer代码段: pac ...

  9. SparkSQL使用之Thrift JDBC server

    Thrift JDBC Server描述 Thrift JDBC Server使用的是HIVE0.12的HiveServer2实现.能够使用Spark或者hive0.12版本的beeline脚本与JD ...

随机推荐

  1. easyui-简单用法寄一些属性

    Easyui 总结 优点: A.简单易用 继承 jQuery 简易使用特性,提供高度抽象接口,短期改善网站易用性. B.开源免费 采用 MIT & GPL 双协议授权,轻松满足自由产品至企业产 ...

  2. 树状数组--K.Bro Sorting

    题目网址: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/D Description Matt’s frie ...

  3. innerHTML和outerHTML有什么区别

    一.区别:1)innerHTML: 从对象的起始位置到终止位置的全部内容,不包括Html标签.2)outerHTML: 除了包含innerHTML的全部内容外, 还包含对象标签本身. 二.例子1: & ...

  4. ThoughtWorks.QRCode生成二维码

    首先引用需要的dll,此处使用的是ThoughtWorks.QRCode.dll,网上可以找到对应的,此处也有一份,点击下载 http://files.cnblogs.com/files/ives/T ...

  5. HTML Jquery

    在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...

  6. C# Out 传值

    public void Out(out int a, out int b) {//out相当于return返回值 //可以返回多个值 //拿过来变量名的时候,里面默认为空值 a=1; b=2; } s ...

  7. ECMAScript 6学习笔记(一):展开运算符

    同步发布于:https://mingjiezhang.github.io/(转载请说明此出处). JavaScript是ECMAScript的实现和扩展,ES6标准的制定也为JavaScript加入了 ...

  8. 链接错误——无法解析的外部符号 ConvertStringToBST

    今天做COM组件时,编译之后,出现了一个数个编译错误:error LNK2019: 无法解析的外部符号 "wchar_t * __stdcall _com_util::ConvertStri ...

  9. Jquey Form 异步提交文件参数并且在http 信息头header中加上一定参数

    1.下载jQuery.Form 包 官网下载:http://jquery.malsup.com/form/#download 2.模拟代码: <!DOCTYPE html> <htm ...

  10. Lync 客户端:无法登陆到Lync,验证服务器中的证书时遇到问题

    安装完Lync客户端后,运行时Lync客户端时,报出如下错误: [原因解析] Lync客户端没有正确安装CA证书链. [解决办法] 第一种方法:将计算机加入域. 第二种方法:不加入域的处理方法: 1. ...