美文网首页
druid使用presto数据源

druid使用presto数据源

作者: 虫儿飞ZLEI | 来源:发表于2019-10-15 14:35 被阅读0次

1. 说明

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


下面以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<Filter>(){{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<Bean1> bean1List = prestoDao.select1();
        List<Bean2> bean2List = prestoDao.select2();
        List<String> nsrmc = prestoDao.select3();
        return "timeout";
    }

xml:

    <select id="select1" resultType="com.base.web.presto.prestotest.domain.Bean1">
SELECT dzdz.djxh,hxzg.uuid
FROM dzdz.dzdz.dj_nsrxx dzdz right join hxzg.hx_zgxt.sb_zyssb_zb hxzg on dzdz.nsrsbh = hxzg.nsrsbh
    </select>
    <select id="select2" resultType="com.base.web.presto.prestotest.domain.Bean2">
      SELECT mysql.password,hive."desc"
FROM mysql22046.jcpt.auth_user mysql left join hive."default".hxzg_hive_dbs hive on mysql.id = hive.db_id
    </select>

    <select id="select3" resultType="java.lang.String">
        SELECT nsrmc
FROM hive.sjjsq_db.dwa_base_djxx limit 10
    </select>

debug运行:


数据都查询到了

相关文章

网友评论

      本文标题:druid使用presto数据源

      本文链接:https://www.haomeiwen.com/subject/kalsmctx.html