喵星之旅-狂奔的兔子-java连接hive数据库

在Spring Boot项目中添加Hive JDBC依赖

首先,你需要在Spring Boot项目的pom.xml文件中添加Hive JDBC驱动的依赖。Maven仓库中通常可以找到Hive JDBC驱动的依赖项,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>2.1.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>*</artifactId>
</exclusion>
<exclusion>
<groupId>org.eclipse.jetty.aggregate</groupId>
<artifactId>jetty-all</artifactId>
</exclusion>
</exclusions>
</dependency>

配置Hive数据库连接信息

接下来,你需要在Spring Boot的配置文件(通常是application.properties或application.yml)中添加Hive数据库的连接信息。我这里是nacos中:

1
2
3
4
5
6
7
xman:
hive:
url: jdbc:hive2://<hive-server-host>:<hive-server-port>/<database-name>
username: <your-username>
password: <your-password>
driver: org.apache.hive.jdbc.HiveDriver

请将替换为实际的Hive服务器地址、端口号、数据库名、用户名和密码。

创建用于操作Hive的Service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;

@Service
public class HiveService {

private JdbcTemplate jdbcTemplate;

@Value("${xman.hive.url}")
private String dbUrl;

@Value("${xman.hive.username}")
private String username;

@Value("${xman.hive.password}")
private String password;

@Value("${xman.hive.driver}")
private String driverClassName;

@PostConstruct
public void init() {
DataSource dataSource = new DriverManagerDataSource(dbUrl, username, password, driverClassName);
this.jdbcTemplate = new JdbcTemplate(dataSource);
}

public void executeQuery(String sql) {
jdbcTemplate.execute(sql);
}

// 其他Hive操作方法...
}

文章目录
  1. 在Spring Boot项目中添加Hive JDBC依赖
  2. 配置Hive数据库连接信息
  3. 创建用于操作Hive的Service
|