Hive的客户端操作

本文介绍了如何启动Hive的远程服务,并通过两种方式操作Hive:JDBC和Thrift Client。使用JDBC时,需在Java项目中导入相关Hadoop与Hive的jar包,创建工具类进行连接。对于Thrift Client的实现,文章也进行了说明。

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 0

Thrift客户端操作

创建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






评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值