【Hadoop】Hive-JavaAPI开发手册

Note

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。

现在是过度篇。

Github

地址:https://github.com/ithuhui/hui-base-java
模块:【hui-base-hadoop】
分支:master
位置:com.hui.base.hadoop.hive.HiveJDBC

Ready

  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-base-java

在【hui-base-hadoop】- com.hui.base.hadoop.hive

Author

 作者:HuHui
 转载:欢迎一起讨论web和大数据问题,转载请注明作者和原文链接,感谢
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值