Hive的兩種操做模式

Hive的客戶端操做

Hive的客戶端操做java

  • 經過JDBC操做Hive
  • 經過Thrift操做Hive
經過JDBC操做Hive
  • 首先 Hive 啓動遠程服務sql

    hive --service hiveserver
  • 須要的jar包apache

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); } }
經過Thrift操做Hive
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(); } }
相關文章
相關標籤/搜索