Hive的客户端操作

来源:互联网 发布:杰洛特的母亲 知乎 编辑:程序博客网 时间:2024/05/22 15:17

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 = 23tid = 2 tname = Marray age = 20tid = 3 tname = ruby age = 22tid = 4 tname = jj age = 23tid = 5 tname = qh age = 22tid = 6 tname = ff age = 25Process 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