Spring 连接 Hive 数据源的启动应用程序:java.sql.SQLException:org.apache.thrift.transport.TTransportException

Spring boot app connecting Hive Datasource : java.sql.SQLException: org.apache.thrift.transport.TTransportException

我正在使用我的 spring 启动应用程序连接到 Hive 数据源。下面是数据源配置

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.IOException;

@Configuration
public class HiveDataSourceConfig {

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

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

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

    public DataSource getHiveDataSource() {

        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl(url);
        dataSource.setDriverClassName("org.apache.hive.jdbc.HiveDriver");
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        return dataSource;
    }

    @Bean(name = "jdbcTemplate")
    public JdbcTemplate getJDBCTemplate() throws IOException {
        return new JdbcTemplate(getHiveDataSource());
    }
}

这是我连接的 JDBC-Url

jdbc:hive2://hpeeeee.hpc.company.com:8443/;ssl=true;sslTrustStore=/Users/arun/Downloads/truststore.jks;trustStorePassword=password123;transportMode=http;httpPath=one/default/hive

我连接到配置单元数据库以获取 8000 多条记录。连接工作得很好,有时我得到以下异常

java.sql.SQLException: org.apache.thrift.transport.TTransportException: org.apache.http.NoHttpResponseException

有时我得到以下异常

org.apache.hive.service.cli.HiveSQLException: Invalid SessionHandle:

当我用谷歌搜索这些异常的原因时,我可以看到一些回复表明这是由于 Hive-Server 的问题...

然而,当我重新启动我的应用程序(连接到配置单元数据库)时,此故障消失并在一段时间后恢复。

有什么想法吗?

@Arun 正如您的评论之一所述“它工作得很好..只有在空闲一段时间后,我才得到异常” 这个好像和连接池的问题有关。由于 active/idle/timeout 一些空闲时间后的属性,当新请求进入时,池中没有正确可用的连接对象。

通过 Spring 引导,您可以像下面那样以比上面示例代码中所示更好的方式使用和池。

在应用程序属性文件中

// These are tomcat connection pool properties.
// Sprign boot properties document link provided below 
spring.datasource.hivedb.username=
spring.datasource.hivedb.password=
spring.datasource.hivedb.url=
spring.datasource.hivedb.driver-class-name=
spring.datasource.hivedb.initial-size=<<define as per your load>>
spring.datasource.hivedb.validation-query=<<for hive select query>>
spring.datasource.hivedb.validation-query-timeout=3
spring.datasource.hivedb.test-on-borrow=true
spring.datasource.hivedb.max-wait=60000

在 Java 配置文件中

@Configuration
public class HiveDataSourceConfig {

@Bean(name = "hiveDataSource")
@ConfigurationProperties(value = "spring.datasource.hivedb",ignoreUnknownFields = false)   public DataSource hiveDataSource() {
return DataSourceBuilder.create().build();
}
    
@Bean(name = "jdbcTemplate")
public JdbcTemplate jdbcTemplate(){
return new JdbcTemplate(hiveDataSource());
}
}

Spring引导数据属性文件link

https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html#data-properties

本文档提供了 spring 框架为框架支持的多个连接池定义的属性的详细信息。使用它们作为调整它们的参考。

如我的示例应用程序属性所示,您需要根据需要测试和调整它们。