Spring JdbcTemplate可以连接到hive吗?

Can Spring JdbcTemplate connect to hive?

我正在开发一个基于 spring 的 java 网络项目。我想使用 Spring JdbcTemplate 连接到配置单元。但是当我测试我的服务时,出现了这个错误信息

"org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'org.apache.hadoop.hive.jdbc.HiveDrive'".

项目是idea maven创建的,但是hive jdbc driver是本地jar(位于WEB-INF/lib)。所以我不确定这个错误是因为我的项目仍然无法识别本地jdbc驱动jar的问题还是因为JdbcTemplate不支持hive连接。有人可以帮我弄清楚吗?提前谢谢你。

这是我的代码:

JdbcTemplate 定义:

    <bean id="dataSourceTDW" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSourceTDW"/>
    </bean>

DAO class:

@Repository(value = "tdwQueryImp")
public class QueryDAOImp implements QueryDAO {
    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<Map<String,Object>> execute(String sql) {
        return jdbcTemplate.queryForList(sql);
    }
}

你可以参考下面link :

http://saurzcode.in/2015/01/connect-hiveserver2-service-jdbc-client/

以及更多

http://hadooptutorial.info/hive-jdbc-client-example/

https://community.hortonworks.com/articles/53629/writing-a-spring-boot-microservices-to-access-hive.html

我通过将数据源的 class 从 org.apache.commons.dbcp.BasicDataSource 更改为 org.springframework.jdbc.datasource.SimpleDriverDataSource 解决了我的问题。

以下是 bean 配置:

<bean id="hiveDriver" class="org.apache.hadoop.hive.jdbc.HiveDriver"/>

<bean id="dataSourceTDW" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <constructor-arg name="driver" ref="hiveDriver"/>
    <constructor-arg name="url" value="${url}"/>
    <constructor-arg name="username" value="${username}" />
    <constructor-arg name="password" value="${password}" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSourceTDW"/>
</bean>

Kerberos 示例:

@Component
public class HiveDataSource extends SimpleDriverDataSource {

private static final Logger logger = LoggerFactory.getLogger(HiveDataSource.class);

private final Subject subject;

@Autowired 
HiveDataSource(Subject subject, Driver hiveDriver, String jdbcUrl) {
    this.subject = subject;
    setUrl(jdbcUrl);
    setDriver(hiveDriver);
}

@Override
protected Connection getConnectionFromDriver(final Properties props) throws SQLException {

    try {
        return Subject.doAs(subject, (PrivilegedExceptionAction<Connection>)() -> getDriver().connect(getUrl(), props));
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Failed to get Hive JDBC connection.", e);
    }

    return null;

}
}

这里 HiveDriver bean 定义为:

@Bean
HiveDriver hiveDriver() {
    HiveDriver impl = new HiveDriver() {
        @Override
        public Connection connect(String url, Properties info) throws SQLException {

            return acceptsURL(url) ? new HiveConnection(url, info) {
                @Override
                public void setAutoCommit(boolean autoCommit) throws SQLException {
                    /* do nothing */
                };

            } : null;
        }
    };
    return impl;
}