springjdbctemplate可以连接到hive吗?

w9apscun  于 2021-06-26  发布在  Hive
关注(0)|答案(3)|浏览(657)

我正在开发一个基于spring的javaweb项目。我想用SpringJDBCTemplate连接到hive。但是当我测试我的服务时,它显示了这个错误信息
“org.springframework.jdbc.cannotgetjdbcconnectionexception:无法获取jdbc连接;嵌套异常为org.apache.commons.dbcp.sqlnestedexception:无法加载jdbc驱动程序类“org.apache.hadoop.hive.jdbc.hivedrive”。
这个项目是由idea maven创建的,但是hivejdbc驱动程序是一个本地jar(位于webinf/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等级:

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

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

lpwwtiir1#

您可以参考以下链接:
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

bvjxkvbb

bvjxkvbb2#

我通过将数据源的类从 org.apache.commons.dbcp.BasicDataSourceorg.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>
puruo6ea

puruo6ea3#

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;
}

相关问题