赞
踩
本文的代码已上传至GitHub,有兴趣的同学可以参阅:https://github.com/ylw-github/java-hive-demo
在前面的教程,已经初步入门hive
了,有兴趣的同学可以参阅:
程序员最终还是要回归到代码的,所以接下来主要讲解使用jdbc
来操作hive
。
阅读过前面几篇博客的,可以直接跳过
开发前需要先启动hadoop
以及hiveserver
,同时也要设置hive
的用户名和密码,具体的操作可以参考:《Hive教程(07)- Hive自定义用户名密码验证(已开源)》
使用beeline
登录:
beeline
!connect jdbc:hive2://localhost:10001
执行创建数据库语句:
create database if not exists company_db;
use company_db;
在DBeaver
也能看到company_db
创建成功:
备注:如果是mac
系统,安装DBeaver
的教程可以参考之前写的博客《Mac下安装DBeaver》。
IDEA
新建java-hive-demo
项目,配置pom.xml
内容如下(其实就只添加了hive-jdbc
依赖):
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.ylw</groupId> <artifactId>java-hive-demo</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <developers> <developer> <name>Yang Lin Wei</name> <url>https://yanglinwei.blog.csdn.net/</url> </developer> </developers> <dependencies> <dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>2.3.3</version> <exclusions> <exclusion> <groupId>org.eclipse.jetty.aggregate</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> </dependencies> </project>
首先定义常量(Const.java
):
package com.ylw.constant; /** * 常量定义 * * @author : YangLinWei * @createTime: 2022/2/23 9:23 上午 */ public interface Const { /*** hive服务器地址 **/ String HIVE_DB_URL = "jdbc:hive2://127.0.0.1:10001/company_db"; /*** hive登录账号 **/ String USER_NAME = "root"; /*** hive登录密码 **/ String PASSWORD = "123"; }
然后定义hive
连接工具类:
package com.ylw.util; import com.ylw.constant.Const; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /** * Hive连接工具类 * * @author : YangLinWei * @createTime: 2022/2/23 9:22 上午 */ public class HiveConnect { private static Connection connection = null; /** * 获取hive连接单例 * * @return hive连接单例 */ public static Connection getConnection() { if (null == connection) { synchronized (HiveConnect.class) { if (null == connection) { try { Class.forName("org.apache.hive.jdbc.HiveDriver"); HiveConnect.connection = DriverManager.getConnection(Const.HIVE_DB_URL, Const.USER_NAME, Const.PASSWORD); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } } } return connection; } }
ok
,项目基本搭建完成了,现在可以做jdbc
的测试了。
单元测试工具类在HiveTest.java
。
先贴出创建表的方法:
public static boolean createEmployeeTable() throws SQLException { String sql = "CREATE TABLE IF NOT EXISTS " + "employee(name string," + "work_place array<string>," + "sex_age struct<sex:string, age:int>," + "score map<string, int>," + "depart_title map<string, string>) " + "row format delimited " + "fields terminated by '|' " + "collection items terminated by ',' " + "map keys terminated by ':'" + "lines terminated by '\n' " + "stored as textfile"; Statement statement = HiveConnect.getConnection().createStatement(); return statement.execute(sql); }
执行完成后,在Dbeaver
可以看到建表成功:
本地新建数据employee.txt
文件,内容如下:
Michael|Montreal,Toronto|Male,30|DB:80|Product:Developer
Will|Montreal|Male,35|Perl:85|Product:Lead,Test:Lead
Shelley|New York|Female,27|Python:80|Test:Lead,COE:Architect
Lucy|Vancouver|Female,57|Sales:89,HR:94|Sales:Lead
执行导入代码:
public static boolean loadData() throws SQLException {
String loadSql = "load data local inpath '本地路径/employ.txt' into table employee";
Statement statement = HiveConnect.getConnection().createStatement();
return statement.execute(loadSql);
}
DBeaver
可以看到导入成功:
执行sql查询,代码如下:
public static void query() throws SQLException {
String sql = "select name ,work_place[0] from employee";
PreparedStatement pstm = HiveConnect.getConnection().prepareStatement(sql);
ResultSet rs = pstm.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2));
}
pstm.close();
rs.close();
}
可以看到控制台有数据打印:
本文的代码已上传至GitHub,有兴趣的同学可以参阅:https://github.com/ylw-github/java-hive-demo,本文完!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。