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. 并发用户数与 TPS 之间的关系

    1.  背景 在做性能测试的时候,很多人都用并发用户数来衡量系统的性能,觉得系统能支撑的并发用户数越多,系统的性能就越好:对TPS不是非常理解,也根本不知道它们之间的关系,因此非常有必要进行解释. 2 ...

  2. JQ JSON数据类型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 数据结构和算法 – 4.字符串、 String 类和 StringBuilder 类

    4.1.String类的应用 class String类应用 { static void Main(string[] args) { string astring = "Now is The ...

  4. 12.享元模式(Flyweight Pattern)

    using System; using System.Collections; namespace ConsoleApplication5 { class Program { /// <summ ...

  5. ExcelReport第一篇:使用ExcelReport导出Excel

    导航 目   录:基于NPOI的报表引擎——ExcelReport 下一篇:ExcelReport源码解析 概述 本篇将通过导出学生成绩的示例演示“使用ExcelReport导出Excel”的步骤. ...

  6. sort函数用法

    原文链接:http://blog.csdn.net/csust_acm/article/details/7326418 sort函数的用法 做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己 ...

  7. subversion安装使用

    这里仅针对subversion进行说明,未完待续. 一.下载subversion并安装: a.subversion 要做svn服务器的必须装 b.Tortoisesvn 仅仅是访问svn服务器的客户端 ...

  8. C语言判断文件是否存在(转)

    int   access(const   char   *filename,   int   amode); amode参数为0时表示检查文件的存在性,如果文件存在,返回0,不存在,返回-1. 这个函 ...

  9. Android入门开发之SD卡读写操作(转)

    SD卡的读写是我们在开发android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String  sdCardRoot = Environment.getE ...

  10. 在64位Win7中使用Navicat Premium 和PL\SQL Developer连接Oracle数据库备忘

    最近接手了一个项目,服务器端数据库是oracle 11g 64位.由于主要工作不是开发,也不想在自己的电脑上安装庞大的oracle数据库,因此寻思着只通过数据库管理工具连接数据库进行一些常用的查询操作 ...