DengQN·一个普通程序员;
【Crud boy日常】Springboot集成HikariCP+Hive
2024-04-29 15:58 634
#hive#springboot#HikariCP

依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>3.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>3.2.4</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
        <version>3.2.4</version>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>5.1.0</version>
    </dependency>


    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>4.0.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>4.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.apache.thrift</groupId>
        <artifactId>libthrift</artifactId>
        <version>0.20.0</version>
    </dependency>

    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-service</artifactId>
        <version>4.0.0</version>
        <exclusions>
            <exclusion>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>*</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.2</version>
    </dependency>
</dependencies>

此处hive-exechive-service 需要把部分包剔除,否则影响springboot初始化注入。

配置

  • application.properties
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.jdbcUrl=jdbc:hive2://127.0.0.1:10000/testmydb
spring.datasource.hikari.username=root
spring.datasource.hikari.password=xxx
spring.datasource.hikari.driver-class-name=org.apache.hive.jdbc.HiveDriver
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.idle-timeout=600000
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.pool-name=HiveHikariCP

使用hikari cp,调用HiveDriver

  • HiveDataSourceConfig
@Configuration
public class HiveDataSourceConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    public HikariDataSource dataSource() {
        return new HikariDataSource();
    }

    @Bean(name = "hiveJdbcTemplate")
    public JdbcTemplate hiveJdbcTemplate(@Qualifier("dataSource") DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
}

接下来就可以愉快玩耍了

  • DemoController
@RestController
@RequestMapping("/api/demo")
public class DemoController {

	@Autowired
	private DataSource dataSource;

	@GetMapping("/databases")
	public ResponseEntity<List<String>> getDatabaseNames() throws SQLException {
		Statement statement = dataSource.getConnection().createStatement();
		boolean showDatabases = statement.execute("show databases");
		List<String> databaseNames = new ArrayList<>();
		if (showDatabases) {
			ResultSet resultSet = statement.getResultSet();
			while (resultSet.next()){
				databaseNames.add(resultSet.getString(1));
			}
		}
		return ResponseEntity.ok(databaseNames);
	}


}