關于hive的入門知識點,網上有很詳細的資料,我是看的 http://yiibai.com/hive/ 提供的教程,在這里我就不重復了。
然后最近一直在蒙圈中,在使用過程中有問題一直解決不了,卡了好久。
下面來寫hive的jdbc程序。 我還是在之前hadoop的項目中寫的,首先引入hive的依賴包,位于安裝目錄下,我這里的地址是/usr/local/hive/lib 我引入了所有含有hive的jar包,然后項目里之前引用的jline包是舊版本的,這里也替換為yarn目錄下的最新版的(上篇文章中替換了yarn下的此包) 然后下面是簡單的代碼:
package hive;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import org.junit.Before;import org.junit.Test;public class Hive { PRivate static final String driveName = "org.apache.hive.jdbc.HiveDriver"; private static final String url = "jdbc:hive2://localhost:10000/testdb"; private static Connection conn = null; @Before public void init() throws ClassNotFoundException, SQLException { Class.forName(driveName); conn = DriverManager.getConnection(url,"APP","mine"); } @Test public void query() throws SQLException{ Statement stmt = conn.createStatement(); System.out.println("aaa"); ResultSet result = stmt.executeQuery("select * from employee"); while(result.next()){ System.out.println(result.getInt(1)+" "+result.getString(2)+" "+result.getDouble(3)+" "+result.getString(4)); } }}運行時可能會報Transport Used for JDBC connection: null的異常,原因應該是hive的服務沒有啟動,在控制臺中啟動hiveserver2
cd $HIVE_HOMEbin/hiverserver2然后再運行應該就沒有問題了
新聞熱點
疑難解答