Java連接hive簡單操作
首先在使用java連接前我們需要在虛擬機上開啟hiveserver2(hiveserver2需要安裝),hiveserve2的作用是實現hive可以進行並發操作,否則沒有辦法實現java對hive的操作。
使用命令:hiveserver2即可打開hiveserver2服務,需要注意的是,開啟服務后這個命令窗口就會被占用,即出現一下情況:
此時已經代表服務已經開啟,我們就不需要動這個終端窗口了,可以再開一個,因為下面這個窗口會顯示你的hive操作是否成功,成功此時ok,錯誤會顯示錯誤原因。即下圖
注意點:如果在啟動hive出現:cannot access /usr/local/spark/lib/spark-assembly-*.jar: No such file or directory,是因為這個jar包在新版本的spark中的位置已經改變!我們要做的只是將hive中的啟動文件中的sparkAssemblyPath這一行更改為你安裝的spark的jar包路徑即可。我們找到hive文件夾下的bin目錄,打開hive文件,找到這句話
# add Spark assembly jar to the classpath if [[ -n "$SPARK_HOME" ]] then sparkAssemblyPath=`ls ${SPARK_HOME}/lib/spark-assembly-*.jar`
CLASSPATH="${CLASSPATH}:${sparkAssemblyPath}"
fi
然后改成下面內容即可:
# add Spark assembly jar to the classpath if [[ -n "$SPARK_HOME" ]] then sparkAssemblyPath=`ls ${SPARK_HOME}/jars/*.jar` CLASSPATH="${CLASSPATH}:${sparkAssemblyPath}" fi
下面就可以進行編程操作了。
1.添加依賴:
<dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</artifactId> <version>3.1.2</version> </dependency>
注意點:不少次添加依賴之后,project就會報錯Failed to read artifact descriptor**jar之類的錯誤,基本上就是加入依賴只有jar沒有下載成功或者不完全,也試過不少中方法,總結一下:
1.需要有一個良好的網絡環境,有的時候即使是顯示下完了也是有這樣的顯示,那就是沒有下載好,這個良好的網絡環境其實很難說,因為我用移動的網絡很可能一下午都沒有下好,電信可能一會就結束了,很費解。
2.使用國內的maven倉庫,建議使用阿里雲或者是華為雲,阿里雲在我看來下載速度可能更好一點。
<mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror>
3.點擊頁面右邊的mavenProject重新下載資源:
4.在文件中右擊pom.xml選擇maven,點擊以下選項嘗試重新下載。
hive操作基本的api:

public class HiveApi { // 驅動,固定的 private static String driverName = "org.apache.hive.jdbc.HiveDriver"; // 默認就是10000端口,ip地址使用hive服務器的 private static String url = "jdbc:hive2://192.168.133.130:10000/default"; // hive連接的用戶名和密碼,默認就算是下面這兩個 private static String user = "賬號"; private static String password = "密碼"; // 公共使用的變量 private static Connection conn = null; private static Statement stmt = null; private static ResultSet rs = null; // 加載驅動、創建連接 public static void init() throws Exception { Class.forName(driverName); conn = DriverManager.getConnection(url,user,password); stmt = conn.createStatement(); } // 釋放資源 public static void destory() throws Exception { if ( rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } // 測試代碼(每次都需要現在加載,執行萬后釋放) public static void main(String[] args) throws Exception { init(); // 創建表功能通過 // createTable(); // 顯示表名稱 // showTables(); // 顯示表描述 // descTable(); // 本地數據導入 // loadData(); // 查詢數據 selectData(); // 運行mapreduce作業 //countData(); // 執行刪除 // dropTable(); destory(); } // 創建表 public static void createTable() throws Exception { String sql = "create table capital_info (id int, question string) row format delimited fields terminated by ','"; stmt.execute(sql); } // 查詢所有表 public static void showTables() throws Exception { String sql = "show tables"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString(1)); } } // 查看表結構 public static void descTable() throws Exception { String sql = "desc capital_info"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString(1) + "\t" + rs.getString(2)); } } // 加載數據 public static void loadData() throws Exception { String filePath = "/usr/local/data/capital_info.txt"; String sql = "load data local inpath '" + filePath + "' overwrite into table capital_info"; stmt.execute(sql); } // 查詢數據 public static void selectData() throws Exception { String sql = "select * from capital_info limit 20"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out.println(rs.getString("id") + "\t\t" + rs.getString("question")); } } // 統計查詢(會運行mapreduce作業) public static void countData() throws Exception { String sql = "select count(1) from capital_info"; rs = stmt.executeQuery(sql); while (rs.next()) { System.out.println(rs.getInt(1) ); } } // 刪除數據庫表 public static void dropTable() throws Exception { String sql = "drop table if exists capital_info"; stmt.execute(sql); } }
注意點:hive的賬號和密碼可以在hive目錄下conf目錄的hive-site.xml中可以查看到: