Hive的两种操作模式
生活随笔
收集整理的這篇文章主要介紹了
Hive的两种操作模式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Hive的客戶端操作
Hive的客戶端操作
- 通過JDBC操作Hive
- 通過Thrift操作Hive
通過JDBC操作Hive
-
首先 Hive 啟動遠程服務
hive --service hiveserver -
需要的jar包
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";@Testpublic 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 {@Testpublic void testThrift() throws Exception{//創建socket連接TSocket socket = new TSocket("192.168.131.111",10000);TProtocol prot = new TBinaryProtocol(socket);//創建HiveClientHiveClient 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();//結果集以行為單位 被封裝進Listfor (String string : list) {System.out.println(string);}socket.close();} }?
轉載于:https://www.cnblogs.com/itboys/p/11210800.html
總結
以上是生活随笔為你收集整理的Hive的两种操作模式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: miniui 加载文件时会做的一些事情
- 下一篇: vue wabpack 切换开发环境 和