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. css3文字导航鼠标悬停气泡动画特效源码下载

    效果体验:http://hovertree.com/texiao/css3/8/ 效果图: 点击这里下载:http://hovertree.com/h/bjaf/8d5vmddq.htm 更多特效:h ...

  2. jQuery中的动画与特效

    1.显示和隐藏hide()和show() 对于动画来说,显示和隐藏是最基本的效果之一,简单介绍jQuery的显示和隐藏. <script type="text/javascript&q ...

  3. 世界上不存在什么RedBSD,SuseBSD或者ArchBSD,Turb...

    世界上不存在什么RedBSD,SuseBSD或者ArchBSD,TurboBSD之类的东西.

  4. 百度FIS入门

    1.fis作为nodejs的模块来管理的,所以首先得安装nodejs,看我前面的安装nodejs的文章. 2.官方的案例下载包https://github.com/hefangshi/fis-quic ...

  5. (旧)子数涵数·PS——文字人物

    首先我们来看一下我用到的素材(在百度图库里下载的). 一.打开PS,在PS中打开素材. 二.复制一个图层(好习惯不解释). 三.图像->调整->阈值,或者按下图示按钮后选择阈值,弹出阈值窗 ...

  6. Linux命令详解之–ls命令

    今天开始为大家介绍下Linux中常用的命令,首先给大家介绍下Linux中使用频率最高的命令--ls命令. 更多Linux命令详情请看:Linux命令速查手册 linux ls命令用于显示指定工作目录下 ...

  7. IT外包行业与职业发展

          在IT行业,总是有一些IT外包公司的存在,凡是存在的都是合理的.当你做为IT从业人员应该尽量避免去外包公司工作 .特别是你从事软件开发工作.     先来说说缘由,一些外包公司本来是从事软 ...

  8. SwipeRefreshLayout下拉刷新

    1.SwipeRefreshLayout是Google在support v4 19.1版本的library更新的一个下拉刷新组件,实现刷新效果更方便. 弊端:只有下拉 //设置刷新控件圈圈的颜色 sw ...

  9. windows下redis启动报Creating Server TCP listening socket *:6379: bind: No such file or directory

    解决方法: 按顺序输入如下命令就可以连接成功 # redis-cli.exe# > shutdown# > exit# redis-server.exe redis.windows.con ...

  10. centos 7安装mysql报错-bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory

    [root@localhost mysql]# ./scripts/mysql_install_db  --user=mysql -bash: ./scripts/mysql_install_db: ...