文章目录
Hive JDBC启动hiveserver2新建maven项目并添加两个依赖编写JDBC代码Hive JDBC
启动hiveserver2
这里 & 是设置后台运行
hive --service hiveserver2 &或者hiveserver2 &
新建maven项目并添加两个依赖
<dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-common</artifactId><version>2.7.6</version></dependency><!-- /artifact/org.apache.hive/hive-jdbc --><dependency><groupId>org.apache.hive</groupId><artifactId>hive-jdbc</artifactId><version>1.2.1</version></dependency>
编写JDBC代码
import java.sql.*;public class HiveJDBCOp {public static void main(String[] args) throws ClassNotFoundException, SQLException {// 1、加载驱动Class.forName("org.apache.hive.jdbc.HiveDriver");// 2、创建连接Connection conn = DriverManager.getConnection("jdbc:hive2://master:10000/test1", "root", "");// 3、创建Statement// Statement st = conn.createStatement();// 4、执行SQL语句 select * from students limit 10// ResultSet rs = st.executeQuery("select * from students limit 10");// 使用prepareStatement 防止SQL注入的问题PreparedStatement pSt = conn.prepareStatement("select * from students where clazz=?");// 设置参数pSt.setString(1, "文科一班");// PreparedStatement pSt = conn.prepareStatement("select clazz,count(*) as cnt from students group by clazz");ResultSet rs = pSt.executeQuery();// 5、遍历ResultSet获取数据while (rs.next()) {int id = rs.getInt("id");String name = rs.getString("name");int age = rs.getInt("age");String gender = rs.getString("gender");String clazz = rs.getString("clazz");System.out.println(id + "," + name + "," + age + "," + gender + "," + clazz);}// 关闭连接rs.close();pSt.close();conn.close();}}
运行结果:1500100016,潘访烟,23,女,文科一班1500100037,顾向松,22,女,文科一班1500100055,卫鸿熙,24,男,文科一班1500100069,束采波,22,女,文科一班1500100082,凌思菱,22,女,文科一班1500100091,蔺水风,22,女,文科一班1500100094,韩醉柳,22,女,文科一班1500100101,计又琴,23,女,文科一班1500100118,蔺昆宇,21,男,文科一班1500100121,张雪卉,22,女,文科一班1500100125,蒙涵衍,23,男,文科一班1500100128,巫鸿哲,24,男,文科一班1500100142,闵华晖,21,男,文科一班1500100165,姜昆皓,22,男,文科一班1500100178,杜芷天,21,女,文科一班1500100217,弘寄南,21,女,文科一班1500100232,石寄琴,21,女,文科一班1500100273,相寒安,22,女,文科一班1500100276,庾运鹏,24,男,文科一班1500100308,黄初夏,23,女,文科一班1500100314,符南莲,24,女,文科一班1500100315,慕绮彤,23,女,文科一班1500100333,乐绮烟,22,女,文科一班1500100338,白之玉,23,女,文科一班1500100339,终胤运,23,男,文科一班1500100342,米昊明,21,男,文科一班1500100370,翟笑天,21,女,文科一班1500100376,庾胤运,21,男,文科一班1500100385,潘忆之,23,女,文科一班1500100407,束昊磊,21,男,文科一班1500100442,郎泽洋,23,男,文科一班1500100456,鄂运凯,24,男,文科一班1500100458,邵凡白,24,女,文科一班1500100463,苏若芹,22,女,文科一班1500100478,邰迎荷,22,女,文科一班1500100509,柏又槐,23,女,文科一班1500100536,栾昊苍,23,男,文科一班1500100537,茹高旻,22,男,文科一班1500100578,殷泽洋,24,男,文科一班1500100585,穆海超,21,男,文科一班1500100587,钭恨山,23,女,文科一班1500100593,景越泽,24,男,文科一班1500100602,殳昌黎,23,男,文科一班1500100640,纪昌黎,23,男,文科一班1500100646,窦海阳,24,男,文科一班1500100652,农鸿晖,21,男,文科一班1500100668,侯从寒,23,女,文科一班1500100685,施昆颉,23,男,文科一班1500100709,俞昂杰,23,男,文科一班1500100724,路星腾,21,男,文科一班1500100744,池若松,22,女,文科一班1500100763,鄂笑容,21,女,文科一班1500100766,荀傲薇,23,女,文科一班1500100770,屠迎梅,23,女,文科一班1500100778,寇怀寒,24,女,文科一班1500100779,於安珊,23,女,文科一班1500100835,柯晨朗,22,男,文科一班1500100849,寿昊英,22,男,文科一班1500100863,曹念蕾,22,女,文科一班1500100867,禹静枫,24,女,文科一班1500100871,贝惜梦,24,女,文科一班1500100875,马向南,21,女,文科一班1500100879,钮怀曼,24,女,文科一班1500100921,林智阳,21,男,文科一班1500100928,戚昌盛,22,男,文科一班1500100943,许昌黎,21,男,文科一班1500100946,秋海白,23,女,文科一班1500100951,平彭泽,22,男,文科一班1500100957,杜易云,21,女,文科一班1500100973,钮幼南,23,女,文科一班1500100985,申飞珍,21,女,文科一班1500100986,左天曼,22,女,文科一班
当然还有增删改等操作,以后靓仔将会一一呈现给大家!