1、启动Hive远程服务
hive --service hiveserver
俩种方式来操作
1)、JDBC
2)、Thrift Client
JDBC
新建java项目
从hadoop与hive包中导入如下jar包
然后创建工具类
package com.txr.utils;
import java.sql.*;
/**
* Created by zj-db0236 on 2017/6/27.
*/
public class JDBCUtils {
private static String dirver = "org.apache.hadoop.hive.jdbc.HiveDriver";
private static String url ="jdbc:hive://localhost:10000/test";
//注册驱动
static {
try {
Class.forName(dirver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取链接
public static Connection getConnection(){
try {
return DriverManager.getConnection(url);
} catch (SQLException e) {
e.printStackTrace( );
}
return null;
}
//释放资源
public static void release(Connection conn, Statement st, ResultSet rs){
if(rs !=null){
try {
rs .close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
rs=null;
}
}
if(st !=null){
try {
st.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
st=null;
}
}
if(conn !=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally {
conn=null;
}
}
}
}
创建测试类
package com.txr.hive;
import com.txr.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* Created by zj-db0236 on 2017/6/27.
*/
public class HiveJDBCDemo {
public static void main(String[] args) {
Connection conn=null;
Statement st =null;
ResultSet rs=null;
String sql="select * from t2";
try{
//获取链接
conn = JDBCUtils.getConnection();
//创建运行环境
st=conn.createStatement();
//运行HQL
rs =st.executeQuery(sql);
//处理数据
while(rs.next()){
//取出员工的姓名和薪水
int id =rs.getInt(1);
String name=rs.getString(2);
int age =rs.getInt(3);
System.out.println("tid = "+id +" tname = "+name+" age = "+age);
}
}catch (SQLException e) {
e.printStackTrace();
}
}
}
测试结果
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
tid = 1 tname = Tom age = 23
tid = 2 tname = Marray age = 20
tid = 3 tname = ruby age = 22
tid = 4 tname = jj age = 23
tid = 5 tname = qh age = 22
tid = 6 tname = ff age = 25
Process finished with exit code 0Thrift客户端操作
创建ThriftClient类
package com.txr.hive;
import org.apache.hadoop.hive.service.HiveClient;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransportException;
import java.util.List;
/**
* Created by zj-db0236 on 2017/6/27.
*/
public class HiveThriftClient {
public static void main(String[] args) throws Exception {
//创建Socket:连接
final TSocket tSocket =new TSocket("localhost",10000);
//创建一个协议
final TProtocol tProtocol =new TBinaryProtocol(tSocket);
//创建Hive Client
final HiveClient client =new HiveClient(tProtocol);
//打开Socket
tSocket.open();
//执行HQL
client.execute("desc t2");
//处理结果
List<String> columns = client.fetchAll();
for (String col:columns){
System.out.println(col);
}
//释放资源
tSocket.close();
}
}
测试结果:
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
tid int
tname string
age int
Process finished with exit code 0
本文介绍了如何启动Hive的远程服务,并通过两种方式操作Hive:JDBC和Thrift Client。使用JDBC时,需在Java项目中导入相关Hadoop与Hive的jar包,创建工具类进行连接。对于Thrift Client的实现,文章也进行了说明。
2733

被折叠的 条评论
为什么被折叠?



