druid使用presto数据源

1. 说明

presto并不能算是数据源,它只是一个数据查询引擎,通过presto集成多种数据源。
我这里已经安装好了presto并且集成了两个oracle、一个mysql、一个hive、一个kudu


druid使用presto数据源_第1张图片

下面以spring boot为例介绍如何在druid中集成presto,实现数据查询

2. 修改配置文件

spring:
  datasource-presto:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.facebook.presto.jdbc.PrestoDriver
    url: jdbc:presto://ip:8099
    username: 1234
    filters: default

3. 添加datasource的bean


    @Bean(name="prestoDataSource")
//    @Qualifier("kuduDataSource")
    @ConfigurationProperties(prefix ="spring.datasource-presto")
    public DataSource prestoDataSource(){
        DruidDataSource druidDataSource=new DruidDataSource();

        List list= new ArrayList(){{add(logFilter);}};
        druidDataSource.setProxyFilters(list);
        return druidDataSource;
    }

4. 增加数据源配置

@Configuration
@MapperScan(basePackages = {"com.base.web.presto.*.dao"},sqlSessionFactoryRef = "prestoSessionFactory")
public class PrestoConfig {
    private static final String MAPPER_LOCATION = "classpath:prestomybatis/*/*.xml";

    @Autowired
    @Qualifier("prestoDataSource")
    private DataSource prestoDataSource;

    @Bean(name = "prestoTransactionManager")
    public DataSourceTransactionManager prestoTransactionManager() {
        return new DataSourceTransactionManager(prestoDataSource);
    }

    @Bean(name = "prestoSessionFactory")
    public SqlSessionFactory prestoSessionFactory(@Qualifier("prestoDataSource") DataSource prestoDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(prestoDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(PrestoConfig.MAPPER_LOCATION));
        //添加驼峰命名法 映射
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setMapUnderscoreToCamelCase(true);
        sessionFactory.setConfiguration(configuration);
        return sessionFactory.getObject();
    }
}

配置结束

5. 代码测试

controller:

    @Autowired
    private PrestoDao prestoDao;
    @ResponseBody
    @GetMapping("/test")
    String test() throws InterruptedException {
        List bean1List = prestoDao.select1();
        List bean2List = prestoDao.select2();
        List nsrmc = prestoDao.select3();
        return "timeout";
    }

xml:

    
    

    

debug运行:


druid使用presto数据源_第2张图片
数据都查询到了

你可能感兴趣的:(druid使用presto数据源)