• Hive Java API操作


    前提条件

    Linux下安装好Hadoop2.7.3、Hive2.3.6

    步骤

    (1) 修改hadoop的core-site.xml添加如下配置

    1. <property>
    2. <name>hadoop.proxyuser.hadoop.hosts</name>
    3. <value>*</value>
    4. </property>
    5. <property>
    6. <name>hadoop.proxyuser.hadoop.groups</name>
    7. <value>*</value>
    8. </property>

    注意:

    配置中hadoop.proxyuser.<用户名>.hosts 和 hadoop.proxyuser.<用户名>.groups 的用户名要改为Linux的用户名,例如Linux的用户名为hadoop就把<用户名>改为hadoop

    (2) 启动hadoop

    start-all.sh

    (3) 启动hiveserver2服务

    hiveserver2

    此时当前终端进入阻塞状态,不要关闭它。可以另外启动一个终端,jps查看进程,看到RunJar进程。

    (4) 在新的终端下,使用beeline通过jdbc连接hiveserver2

    beeline -u jdbc:hive2://node1:10000/defaul -n hadoop -p

    -u 为hive的url地址

    -n 为hive所在Linux机器当前登录的用户名

    -p 为Linux机器的登录密码

     运行命令,根据提示输入密码后,看到如下箭头所示,为连接成功

    (5) 编码

    新建maven工程,工程名例如:hive-api

    添加pom.xml依赖

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.apache.hadoop</groupId>
    4. <artifactId>hadoop-common</artifactId>
    5. <version>2.7.3</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>org.apache.hive</groupId>
    9. <artifactId>hive-jdbc</artifactId>
    10. <version>2.3.6</version>
    11. </dependency>
    12. <dependency>
    13. <groupId>junit</groupId>
    14. <artifactId>junit</artifactId>
    15. <version>4.13.2</version>
    16. <scope>test</scope>
    17. </dependency>
    18. </dependencies>

    编码,在src\test\java\org.example包下创建

    HiveJDBCTest.java

     代码如下:

    1. package org.example;
    2. import org.example.api.JDBCUtils;
    3. import org.junit.After;
    4. import org.junit.Before;
    5. import org.junit.Test;
    6. import java.sql.*;
    7. public class HiveJDBCTest {
    8. private static String driverName="org.apache.hive.jdbc.HiveDriver";//驱动名称
    9. private static String url = "jdbc:hive2://node1:10000/default";//连接hive的default数据库
    10. private static String user = "hadoop";//hive所在机器的用户名
    11. private static String password = "hadoop";//hive所在机器的密码
    12. private static Connection conn = null;//连接
    13. private static Statement stmt = null;//声明
    14. private static ResultSet rs = null;//结果集
    15. @Before
    16. public void init() throws ClassNotFoundException, SQLException {
    17. Class.forName(driverName);
    18. conn = DriverManager.getConnection(url, user, password);
    19. stmt = conn.createStatement();
    20. }
    21. /**
    22. * 创建数据库
    23. * @throws SQLException
    24. */
    25. @Test
    26. public void createDatabase() throws SQLException {
    27. String sql = "create database testdb1912";
    28. System.out.println("Running: " + sql);
    29. stmt.execute(sql);
    30. }
    31. /**
    32. * 查询所有数据库
    33. * @throws SQLException
    34. */
    35. @Test
    36. public void showDatabases() throws SQLException {
    37. String sql = "show databases";
    38. System.out.println("Running: " + sql);
    39. rs = stmt.executeQuery(sql);
    40. while (rs.next()){
    41. System.out.println(rs.getString(1));//从1开始,查看第一列
    42. // System.out.println("========");
    43. // System.out.println(rs.getString(2));//没有第二列会报错Invalid columnIndex: 2
    44. }
    45. }
    46. /**
    47. * 创建表
    48. * @throws SQLException
    49. */
    50. @Test
    51. public void createTable() throws SQLException {
    52. String sql = "create table dept_api1(deptno int, dname string, loc string)" +
    53. "row format delimited fields terminated by ','";
    54. System.out.println("Running:"+sql);
    55. stmt.execute(sql);
    56. }
    57. @Test
    58. public void showTables() throws SQLException {
    59. String sql = "show tables";
    60. System.out.println("Running:"+sql);
    61. rs = stmt.executeQuery(sql);
    62. while (rs.next()){
    63. System.out.println(rs.getString(1));
    64. }
    65. }
    66. /**
    67. * 查看表结构
    68. * @throws SQLException
    69. */
    70. @Test
    71. public void descTable() throws SQLException {
    72. String sql = "desc dept_api";
    73. System.out.println("Running:"+sql);
    74. rs = stmt.executeQuery(sql);
    75. while (rs.next()){
    76. // 表结构: 列名 列类型
    77. System.out.println(rs.getString(1)+"\t"+rs.getString(2));
    78. }
    79. }
    80. /**
    81. * 加载数据
    82. * @throws SQLException
    83. */
    84. @Test
    85. public void loadData() throws SQLException {
    86. // String filePath = "D:/test/inputemp/dept.csv";
    87. // String sql = "load data local inpath 'D:/test/inputemp/dept.csv' overwrite into table dept_api";
    88. // String sql = "LOAD DATA LOCAL INPATH 'D:\\test\\inputemp\\dept.csv' " + "OVERWRITE INTO TABLE dept_api";
    89. // 注意:这里相当于连接了hiveserver2的客户端,hiverserver2在linux上
    90. // 路径为Linux的本地文件,用windows路径会报错
    91. String sql = "LOAD DATA LOCAL INPATH '/home/hadoop/dept.csv' " + "OVERWRITE INTO TABLE dept_api1";
    92. System.out.println("Running:"+sql);
    93. stmt.execute(sql);
    94. }
    95. /**
    96. * 统计记录数
    97. * @throws SQLException
    98. */
    99. @Test
    100. public void countData() throws SQLException {
    101. String sql = "select count(1) from dept_api1";
    102. System.out.println("Running:"+sql);
    103. rs = stmt.executeQuery(sql);
    104. while (rs.next()){
    105. System.out.println(rs.getInt(1));
    106. }
    107. }
    108. /**
    109. * 删除数据库
    110. * @throws SQLException
    111. */
    112. @Test
    113. public void dropDB() throws SQLException {
    114. String sql = "drop database if exists testdb1";
    115. System.out.println("Running:"+sql);
    116. stmt.execute(sql);
    117. }
    118. /**
    119. * 删除表
    120. * @throws SQLException
    121. */
    122. @Test
    123. public void deleteTable() throws SQLException {
    124. String sql = "drop table if exists dept_api1";
    125. System.out.println("Running:"+sql);
    126. stmt.execute(sql);
    127. }
    128. @After
    129. public void destory() throws SQLException {
    130. if(rs !=null){
    131. rs.close();
    132. }
    133. if(stmt != null){
    134. stmt.close();
    135. }
    136. if(conn !=null){
    137. conn.close();
    138. }
    139. }
    140. }

    分别进行单元测试。

    更多代码:

    JDBCUtils.java
    1. package org.example.api;
    2. import java.sql.*;
    3. public class JDBCUtils {
    4. // Hive的驱动
    5. private static String driver = "org.apache.hive.jdbc.HiveDriver";
    6. // Hive的URL地址
    7. // private static String url = "jdbc:hive2://192.168.193.140:10000/default";
    8. private static String url = "jdbc:hive2://node1:10000/default";
    9. // 注册数据库的驱动
    10. static{
    11. try {
    12. Class.forName(driver);
    13. } catch (ClassNotFoundException e) {
    14. e.printStackTrace();
    15. }
    16. }
    17. // 获取数据库Hive的链接
    18. public static Connection getConnection(){
    19. try {
    20. return DriverManager.getConnection(url,"hadoop","hadoop");
    21. // return DriverManager.getConnection(url);
    22. } catch (SQLException throwables) {
    23. throwables.printStackTrace();
    24. }
    25. return null;
    26. }
    27. // 释放资源
    28. public static void release(Connection conn, Statement st, ResultSet rs){
    29. if(rs != null){
    30. try {
    31. rs.close();
    32. } catch (SQLException throwables) {
    33. throwables.printStackTrace();
    34. } finally {
    35. rs = null;
    36. }
    37. }
    38. if(st != null){
    39. try {
    40. st.close();
    41. } catch (SQLException throwables) {
    42. throwables.printStackTrace();
    43. } finally {
    44. st = null;
    45. }
    46. }
    47. if(conn != null){
    48. try {
    49. conn.close();
    50. } catch (SQLException throwables) {
    51. throwables.printStackTrace();
    52. } finally {
    53. conn = null;
    54. }
    55. }
    56. }
    57. }

    HiveDemo.java
    1. package org.example.api;
    2. import java.sql.Connection;
    3. import java.sql.ResultSet;
    4. import java.sql.SQLException;
    5. import java.sql.Statement;
    6. public class HiveDemo {
    7. public static void main(String[] args) {
    8. // String sql = "select * from emp1";
    9. String sql = "create database testdb1";
    10. Connection conn = null;
    11. Statement st = null;
    12. ResultSet rs = null;
    13. try {
    14. conn = JDBCUtils.getConnection();
    15. st = conn.createStatement();
    16. rs = st.executeQuery(sql);
    17. while(rs.next()){
    18. String ename = rs.getString("ename");
    19. double sal = rs.getDouble("sal");
    20. System.out.println(ename + "\t" + sal);
    21. }
    22. } catch (SQLException throwables) {
    23. throwables.printStackTrace();
    24. } finally {
    25. JDBCUtils.release(conn,st,rs);
    26. }
    27. }
    28. }

    完成!enjoy it!

  • 相关阅读:
    2. 线性表的基本概念 + 基本操作
    用面向对象的方式操作 JSON 甚至还能做四则运算 JSON 库
    开工第一天,这个api超时优化把我干趴下了!
    实现Runnable接口
    鉴源论坛 · 观擎丨基于模型的方法在民机机载软件中的应用
    C数据结构-堆的实现思路和堆排序的实现
    EOS区块链keosd的RPC API
    (附源码)springboot苔藓植物科普网站 毕业设计 345641
    OpenWrt kernel install分析(1)
    springboot系列(十八):如何Windows安装redis?你玩过么|超级详细,建议收藏
  • 原文地址:https://blog.csdn.net/qq_42881421/article/details/125402248