Java连接hive进行操作的方式有多种,接触到了两种:

首先,hive要起动远程服务接口,命令:

hive --service hiveserver -p 50000 &

1. 通过jdbc驱动连接hive

当然还有其他的连接方式,比如ODBC等,这种方式很常用。

不稳定,经常会被大数据量冲挂,不建议使用。

package cn.ac.iscas.hiveclient;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties; public class HiveJDBCClient { private static String driverName;
private static String url;
private static String user;
private static String password; private Connection conn = null;
private Statement stmt = null; static {
Properties prop = new Properties();
InputStream in = HiveJDBCClient.class.getResourceAsStream("hiveCli.properties");
try{
prop.load(in);
driverName = prop.getProperty("driverName");
url = prop.getProperty("url");
user = prop.getProperty("user");
password = prop.getProperty("password");
}catch (IOException e){
e.printStackTrace();
}
} public boolean execute(String sql){
boolean rs = false;
try {
conn = getConn();
stmt = conn.createStatement();
rs = stmt.execute(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
if( null != conn){
conn.close();
conn = null;
}
if( null != stmt){
stmt.close();
stmt = null;
}
}catch (SQLException e){
e.printStackTrace();
}
}
return rs;
} public ResultSet executeQuery(String sql){
ResultSet rs = null;
try {
conn = getConn();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try{
if( null != conn){
conn.close();
conn = null;
}
if( null != stmt){
stmt.close();
stmt = null;
}
}catch (SQLException e){
e.printStackTrace();
}
}
return rs;
} private static Connection getConn() throws ClassNotFoundException,
SQLException{
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url,user,password);
return conn;
} public static void main(String[] args){
HiveJDBCClient hc = new HiveJDBCClient();
ResultSet rs = hc.executeQuery("desc carsrecord");
try {
while(rs.next()){
System.out.println(rs.getString(1));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

2. 通过hive thrift连接

package cn.ac.iscas.hiveclient;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import org.apache.hadoop.hive.service.HiveClient;
import org.apache.hadoop.hive.service.HiveServerException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException; public class HiveThreatClient {
static TTransport transport;
static TProtocol protocol;
static HiveClient client ;
static String ip;
static int port; static {
Properties prop = new Properties();
InputStream in = HiveJDBCClient.class.getResourceAsStream("hiveCli.properties");
try{
prop.load(in);
ip = prop.getProperty("ip");
port = Integer.valueOf(prop.getProperty("port"));
}catch (IOException e){
e.printStackTrace();
}
} public static List<String> execute(String query) throws HiveServerException,
TException,TTransportException{
List<String> result = new ArrayList<String>();
transport = new TSocket(ip,port);
protocol = new TBinaryProtocol(transport);
client = new HiveClient(protocol);
transport.open();
client.send_execute(query);
client.recv_execute();
//client.send_commit_txn(rqst);
//client.execute(query);
List<String> list = client.fetchN(10);
while(null!=list && list.size()>0){
for(String r :list){
System.out.println(r);
result.add(r);
}
list = client.fetchN(10);
}
client.shutdown();
transport.close();
return result;
} public static void main(String[] args){
try {
//HiveThreatClient.execute("desc carsrecord");
//HiveThreatClient.execute("select distinct addressname from carsrecord where collectiontime='2015-02-02'");
//load data inpath '/2015/02/2015-02-01.dat' overwrite into table carsrecord partition(collectiontime='2015-02-01')
for(int i = 10; i < 29; i++){
String day = i > 9 ? ""+i:"0"+i;
String stat = "load data inpath '/2015/02/2015-02-"+day+".dat' overwrite into table carsrecord partition(collectiontime='2015-02-"+day+"')";
System.out.println(stat);
HiveThreatClient.execute(stat);
}
//HiveThreatClient.execute("select * from carsrecord where collectiondate>='2014-01-01' and collectiondate<'2014-01-03'");
} catch (HiveServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TTransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

需要引入的jar包:

  • <classpathentry kind="lib" path="hive-service-0.13.1.jar"/>
        <classpathentry kind="lib" path="hive-exec-0.13.1.jar"/>

    <classpathentry kind="lib" path="slf4j-api-1.6.6.jar"/>

    <classpathentry kind="lib" path="hive-metastore-0.13.1.jar"/>

    <classpathentry kind="lib" path="libfb303-0.9.0.jar"/>

hive Java API的更多相关文章

  1. 使用hive客户端java api读写hive集群上的信息

    上文介绍了hdfs集群信息的读取方式,本文说hive 1.先解决依赖 <properties> <hive.version>1.2.1</hive.version> ...

  2. hive-通过Java API操作

    通过Java API操作hive,算是测试hive第三种对外接口 测试hive 服务启动 package org.admln.hive; import java.sql.SQLException; i ...

  3. Sqoop Java API 导入应用案例

    环境信息: Linux+JDK1.7 Sqoop 1.4.6-cdh5.5.2 hadoop-core 2.6.0-mr1-cdh5.5.2 hadoop-common 2.6.0-cdh5.5.2 ...

  4. 使用JAVA API 解析ORC File

    使用JAVA API 解析ORC File orc File 的解析过程中,使用FileInputFormat的getSplits(conf, 1)函数, 然后使用 RecordReaderreade ...

  5. Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结

    转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...

  6. hbase java API跟新数据,创建表

    package hbaseCURD; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import o ...

  7. _00017 Kafka的体系结构介绍以及Kafka入门案例(0基础案例+Java API的使用)

    博文作者:妳那伊抹微笑 itdog8 地址链接 : http://www.itdog8.com(个人链接) 博客地址:http://blog.csdn.net/u012185296 博文标题:_000 ...

  8. Phoenix简介概述,Phoenix的Java API 相关操作优秀案例

    Phoenix简介概述,Phoenix的Java API 相关操作优秀案例 一.Phoenix概述简介 二.Phoenix实例一:Java API操作 2.1 phoenix.properties 2 ...

  9. Atitit 图像处理 调用opencv 通过java  api   attilax总结

    Atitit 图像处理 调用opencv 通过java  api   attilax总结 1.1. Opencv java api的支持 opencv2.4.2 就有了对java api的支持1 1. ...

随机推荐

  1. 八皇后(dfs+回溯)

    重看了一下刘汝佳的白板书,上次写八皇后时并不是很懂,再写一次: 方法1:逐行放置皇后,然后递归: 代码: #include <bits/stdc++.h> #define MAXN 8 # ...

  2. 与你相遇好幸运,Tippecanoe在Centos下の安装

    全新的CentOS 7 x86_64 安装编译工具 yum install -y gcc automake autoconf libtool make yum insyall -y gcc gcc-c ...

  3. Babelfish(二分查找,字符串的处理略有难度,用sscanf输入)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28581   Accepted: 12326 题目链接: ...

  4. CXF学习(2) helloworld

    0.新建一个项目取名wsserver. pom.xml 文件如下 <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...

  5. BZOJ1004 [HNOI2008]Cards(Polya计数)

    枚举每个置换,求在每个置换下着色不变的方法数,先求出每个循环的大小,再动态规划求得使用给定的颜色时对应的方法数. dp[i][j][k]表示处理到当前圈时R,B,G使用量为i,j,k时的方法数,背包思 ...

  6. Go 中的反射要点

    简介 反射是元数据编程的一种形式,指的是程序获得本身结构的一种能力.不同语言的反射模型实现不一样,本文中的反射,仅仅指的是Go语言中的反射模型. 类型以及接口 这个基本概念需要清晰,这里不详细展开. ...

  7. Async/Await 最佳实践

    其实好久以前就看过这个文章,以及类似的很多篇文章.最近在和一个新同事的交流中发现原来对async的死锁理解不是很透彻,正好最近时间比较充裕就再当一回搬运工. 本文假定你对.NET Framework ...

  8. Oracle【IT实验室】数据库备份与恢复之五:Flashback

    Flashback在开发环境(有时生产环境的特殊情况下)是很有用的一个工具.     5.1 9i Flashback 简介     5.1.1  原理 当数据  update  或  delete  ...

  9. 来访统计的JS代码

    <script language="JavaScript"> var caution = false function setCookie(name, value, e ...

  10. linux中预留的$变量

    $0表示bash脚本的文件名 $1表示第一个参数 $*表示参数列表$0, $1, $2… $@表示"$1"/"$2"...每个变量都是独立的,用双引号括起来 $ ...