The two modes of operation Hive

Hive client operating

Hive client operating

  • By JDBC operations Hive
  • By operating Hive Thrift
By JDBC operations Hive
  • First, start remote service Hive

    hive --service hiveserver
  • Required jar package

public class TestQuery {

    private static final String dirver ="org.apache.hadoop.hive.jdbc.HiveDriver";
    private static final String url ="jdbc:hive://192.168.131.111:10000/default";

    @Test
    public void testQuery() throws Exception{

        Class.forName(dirver);
        String sql = "select * from emp";
        Connection conn = DriverManager.getConnection(url);
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql);

        while(rs.next()){
            //不能写列名
            String ename = rs.getString(2);
            int sal = rs.getInt(6);
            System.out.println(ename+"\t"+sal);
        }
        JDBCUtil.release(conn, st, rs);
    }
}
By operating Hive Thrift
package com.zd.thrift;

import java.util.List;
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.junit.Test;

public class TestThrift {
    @Test
    public void testThrift() throws Exception{

        //创建socket连接
        TSocket socket = new TSocket("192.168.131.111",10000);
        TProtocol prot = new TBinaryProtocol(socket);

        //创建HiveClient
        HiveClient client = new HiveClient(prot);

        //打开socket通信
        socket.open();

        //执行HQL语句
        //client.execute("desc emp");
        client.execute("select * from emp where deptno=10");

        //取出返回的结果
        List<String> list = client.fetchAll();

        //结果集以行为单位   被封装进List
        for (String string : list) {
            System.out.println(string);
        }
        socket.close();
    }
}

 

Guess you like

Origin www.cnblogs.com/itboys/p/11210800.html