springboot上用mybaties与phoenix和hbase进行整合

配置类

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
 
import javax.sql.DataSource;
 
/**
 *@描述: hbase-phoenix 数据源模版
 */
@Configuration
@PropertySource(value="classpath:application.properties")
@MapperScan(basePackages = HbasePhoenixDataSourceConfig.PACKAGE,sqlSessionFactoryRef = HbasePhoenixDataSourceConfig.HBASEPHOENIX_SQL_SESSION_FACTORY)
public class HbasePhoenixDataSourceConfig {
    static final String HBASEPHOENIX_SQL_SESSION_FACTORY = "hbasePhoenixSqlSessionFactory";
    static final String PACKAGE = "com.kevin.dao.hbase";
    static final String MAPPER_LOCATION = "classpath:mapper/hbase/*.xml";
 
    @Value("${hbase.phoenix.datasource.url}")
    private String url;
 
 
    @Value("${hbase.phoenix.datasource.driverClassName}")
    private String driverClass;
 
    @Bean(name = "hbasePhoenixDataSource")
    public DataSource hbasePhoenixDataSource() {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClass);
        dataSource.setUrl(url);
        return dataSource;
    }
 
    @Bean(name = "hbasePhoenixTransactionManager")
    public DataSourceTransactionManager hbasePhoenixTransactionManager(@Qualifier("hbasePhoenixDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
 
    @Bean(name = HBASEPHOENIX_SQL_SESSION_FACTORY)
    public SqlSessionFactory hbasePhoenixSqlSessionFactory(@Qualifier("hbasePhoenixDataSource") DataSource hbasePhoenixDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(hbasePhoenixDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(HbasePhoenixDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}

pom文件需要加一下几个

        
			org.apache.phoenix
			phoenix-core
			5.0.0-HBase-2.0
		
		
            org.apache.hadoop
            hadoop-common
            2.8.4
        
		
			com.alibaba
			druid-spring-boot-starter
			${druid.version}
		
		
			com.baomidou
			mybatis-plus
			${mybatisplus.version}
		
		
			com.baomidou
			mybatisplus-spring-boot-starter
			${mybatisplus.spring.boot.version}
		

application.properties文件需配置连接信息

#Spring data phoenix 访问配置
logging.level.com.innovem.phoenix.dao=debug
mybatis.config-location=mybatis-config.xml
datasource.maxPoolSize=20
datasource.minIdle=2
datasource.validationTimeout=300000
datasource.idleTimeout=600000
datasource.connectionTestQuery=select 1+1
mybatis.mapperLocations=
hbase.phoenix.datasource.url=jdbc:phoenix:nodename或IP:2181
hbase.phoenix.datasource.driverClassName=org.apache.phoenix.jdbc.PhoenixDriver

然后就可以使用注解方式取phoenix数据了,在这之前需要在phoenix中映射下hbase数据,使用create view 的方式

@Mapper
public interface ExampleMapper {

	@Select("SELECT × FROM \"table_name\" ")
	Set getEntityList() ;
			
}

 

你可能感兴趣的:(java,Hadoop)