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. mysql列类型

    mysql三大列类型 整型 tinyint(占据空间:1个字节 存储范围  有符号  -128-127   无符号  0-255) smallint   mediumint    int    big ...

  2. nc 显示服务器开放的端口

    # nc -z -w xxxx.com - Connection to xxxx.com port [tcp/ftp] succeeded! Connection to xxxx.com port [ ...

  3. MVC缓存03,扩展方法实现视图缓存

    关于缓存,先前尝试了: ● 在"MVC缓存01,使用控制器缓存或数据层缓存"中,分别在控制器和Data Access Layer实现了缓存 ● 在"MVC缓存02,使用数 ...

  4. MVC - 10.CodeFrist

    微软示例 1.(对新数据库使用 Code First):http://msdn.microsoft.com/zh-cn/data/jj193542 2.(连接和模型):http://msdn.micr ...

  5. MVC4 遇到问题总结

    1.路径编写: 举例1.<img  src="../Login/VailCode" width="108" height="40"&g ...

  6. V for Vendetta

    V for Vendetta V字仇杀队 复仇者V 安迪·沃卓斯基 and Larry Wachowski 思想,是最强大的武器.因为,世界上的独裁政府,有一个共同特点就是推行洗脑和愚民政策. 经典台 ...

  7. 学习linux内核时常碰到的汇编指令(1)

     转载:http://blog.sina.com.cn/s/blog_4be6adec01007xvg.html 80X86 汇编指令符号大全 +.-.*./∶算术运算符. &∶宏处理操作符. ...

  8. 手持终端PDA应用固定资产管理系统(资产查询 盘点)软件程序系统

    一.产品概述 固定资产管理系统,是针对企事业单位内部资产管理中出现的工作量大.过程繁琐.追踪困难等一系列难题开发的一套先进管理软件.软件实现了对资产的多种方式管理,目前包括条形码.二维码.RFID管理 ...

  9. 安装 phpredis 扩展

    /************************************************//********************* phpredis  ***************** ...

  10. git 基础使用

    1: 安装客户端 2: 注册使用github 3: 具体操作 3-1: 右键打开:git bash here 执行 ssh-keygen -t rsa -C "youremail@examp ...