【Hadoop】Hive开发手册(JavaAPI)

版权声明:小哥哥小姐姐们,本文为小博主原创文章,转载请附上博主博文网址,并标注作者谢谢~~。违者必究 https://blog.csdn.net/HuHui_/article/details/84261775

前言

hadoop系列

  1. 【Hadoop】Hadoop完全分布式集群搭建 https://blog.csdn.net/HuHui_/article/details/83960047
  2. 【Hadoop】Hive搭建 https://blog.csdn.net/HuHui_/article/details/84202077
  3. 【Hadoop】Hdfs开发手册(JavaAPI)https://blog.csdn.net/HuHui_/article/details/83834199

这篇文章感觉有划水…因为直接使用Hive的JavaAPI用的不多。本人一般项目更多是用spark-hive。感兴趣的可以看看【Spark】SparkSql分析结果写入Mysql

https://blog.csdn.net/HuHui_/article/details/83964233

Hive和HDFS应用更多在服务器上。使用系统调度。但也要对Java的JDBC调用Hive要了解。

因为后面会把Hive和HDFS详细shell 命令和实际应用做一个总结blog。

现在是过度篇。

装备

  1. Hadoop环境
  2. Hive环境
  3. Maven依赖
  4. sql基础

Core

JDBC相信大家学过Java都是一个基础。Hive抽象出接口,提供JDBC连接。

HIVE-JDBC其实本质上是扮演一个协议转换的角色,把jdbc的标准协议转换为访问HiveServer服务的协议.

Hive开启远程服务

# hive --service hiveserver & 

Maven依赖

<hadoop.version>2.6.5</hadoop.version>
<hive.version>2.1.0</hive.version>

<dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>${hive.version}</version>
 </dependency>

Java连接Hive代码

/**
 * <b><code>HiveJDBC</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2018/11/19 22:08.
 *
 * @author Hu Weihui
 */
public class HiveJDBC {

    /**
     * The constant driverName.
     *
     * @since hui_project 1.0.0
     */
    private static final String driverName ="org.apache.hive.jdbc.HiveDriver";
    /**
     * The constant url.
     *
     * @since hui_project 1.0.0
     */
    private static final String url="jdbc:hive2://192.168.31.60:10000/default";

    /**
     * application.
     *
     * @param args the input arguments
     * @throws SQLException           the sql exception
     * @throws ClassNotFoundException the class not found exception
     * @since hui_project 1.0.0
     */
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName(driverName);
        Connection connection = DriverManager.getConnection(url, "root", "123456");
        Statement statement = connection.createStatement();
        statement.execute("CREATE DATABASE d_huweihui");
        connection.close();
    }


    /**
     * 创建数据库.
     *
     * @param statement the statement
     * @throws SQLException the sql exception
     * @since hui_project 1.0.0
     */
    public void createDataBases(Statement statement) throws SQLException {
        String sql = "CREATE DATABASE d_huweihui";

        statement.execute(sql);
    }

    /**
     * 查看数据库.
     *
     * @param statement the statement
     * @throws SQLException the sql exception
     * @since hui_project 1.0.0
     */
    public void showDataBases(Statement statement) throws SQLException {
        String sql = "show databases";

        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()){
            System.out.println(resultSet.getString(1));
        }
    }

}

Result

在这里插入图片描述

Github

https://github.com/ithuhui/hui-bigdata-hadoop/tree/master/src

小哥哥小姐姐们,转载请标注链接和作者,感谢
欢迎随时交流大数据和WEB项目经验和理解。

猜你喜欢

转载自blog.csdn.net/HuHui_/article/details/84261775