Note
hadoop系列
- 【Hadoop】Hadoop完全分布式集群搭建 https://blog.csdn.net/HuHui_/article/details/83960047
- 【Hadoop】Hive搭建 https://blog.csdn.net/HuHui_/article/details/84202077
- 【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
- Hadoop环境
- Hive环境
- Maven依赖
- 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和大数据问题,转载请注明作者和原文链接,感谢