【问题标题】:Spring boot app connecting Hive Datasource : java.sql.SQLException: org.apache.thrift.transport.TTransportException连接 Hive 数据源的 Spring Boot 应用程序:java.sql.SQLException: org.apache.thrift.transport.TTransportException
【发布时间】:2020-10-26 07:12:03
【问题描述】:

我正在使用我的 Spring Boot 应用程序连接到 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:无效的 SessionHandle:

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

但是,当我重新启动我的应用程序(连接到 hive 数据库)时,此故障消失并在一段时间后再次出现。

有什么想法吗?

【问题讨论】:

  • 您能否检查配置单元驱动程序是否与您尝试连接的配置单元分布相匹配?有时有两个不同版本的 hive dist。你可能有类似的问题。
  • 当我重新启动我的应用程序时,它工作得非常好..只有在一段时间的空闲时间后,我才得到异常
  • Hive URL 是什么?您是否通过负载均衡器连接?如果平衡器配置不正确,则连接可能会在 HiveServer2 的不同实例之间反弹,并且由于其他 HiveServer2 没有注册该客户端,因此连接将被拒绝并显示“Invalid SessionHandle”。
  • 有没有办法通过 Spring boot 解决它?以便它终止所有无效会话并获得一个新会话
  • 我已经用 JDBC URL 详细信息更新了我的实际帖子

标签: spring spring-boot hive spring-jdbc jdbctemplate


【解决方案1】:

@Arun 正如您的评论之一所说“它工作得非常好..只有在一段时间的空闲时间之后,我才得到例外”这在我看来和连接池问题。由于在有新请求进入时经过一段时间的空闲时间后的活动/空闲/超时属性,池中没有正确可用的连接对象。

使用 Spring boot,您可以像下面这样更好地使用和池化,然后在上面的示例代码中显示。

在应用程序属性文件中

// 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 boot 数据属性文档链接

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

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

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

【讨论】:

    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 2015-11-11
    • 2017-11-24
    • 2018-09-11
    • 2015-05-31
    • 2020-02-11
    • 2015-07-10
    • 1970-01-01
    相关资源
    最近更新 更多