SpringBoot连接Kylin,亲测有效

SpringBoot连接Kylin,亲测有效

  1. 配置依赖
    
    
      org.apache.kylin
      kylin-jdbc
      2.6.4
    
  1. 配置文件的参数配置
#kylin
spring.datasource.kylin.name=kylin
spring.datasource.kylin.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.kylin.driver-class-name=org.apache.kylin.jdbc.Driver
spring.datasource.kylin.jdbc-url=jdbc:kylin://127.0.0.1:7070/kylin
spring.datasource.kylin.username=name
spring.datasource.kylin.password=password
#10s
spring.datasource.kylin.maxWaitTime=10000
spring.datasource.kylin.poolSize=10
  1. 创建连接参数实体类
@Data
public class KylinProperties {

    private String name;

    private String type;

    private String jdbcUrl;

    private String username;

    private String password;

    private String driverClassName;

    private long maxWaitTime;

    private int poolSize;
}
  1. 自定义kylin数据源
@Slf4j
public class KylinDataSource implements DataSource {

    private LinkedList connectionPoolList = new LinkedList<>();

    private long maxWaitTime;

    public KylinDataSource(KylinProperties kylinProperties) {
        try {
            this.maxWaitTime = kylinProperties.getMaxWaitTime();
            Driver driverManager = (Driver) Class.forName(kylinProperties.getDriverClassName()).newInstance();
            Properties info = new Properties();
            info.put("user", kylinProperties.getUsername());
            info.put("password", kylinProperties.getPassword());
            for (int i = 0; i < kylinProperties.getPoolSize(); i++) {
                Connection connection = driverManager.connect(kylinProperties.getJdbcUrl(), info);
                connectionPoolList.add(ConnectionProxy.getProxy(connection, connectionPoolList));
            }
            log.info("PrestoDataSource has initialized {} size connection pool", connectionPoolList.size());
        } catch (Exception e) {
            log.error("kylinDataSource initialize error, ex: ", e);
        }
    }

    @Override
    public Connection getConnection() throws SQLException {
        synchronized (connectionPoolList) {
            if (connectionPoolList.size() <= 0) {
                try {
                    connectionPoolList.wait(maxWaitTime);
                } catch (InterruptedException e) {
                    throw new SQLException("getConnection timeout..." + e.getMessage());
                }
            }

            return connectionPoolList.removeFirst();
        }
    }

    @Override
    public Connection getConnection(String username, String password) throws SQLException {
        return null;
    }

    @Override
    public  T unwrap(Class iface) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class iface) throws SQLException {
        return false;
    }

    @Override
    public PrintWriter getLogWriter() throws SQLException {
        return null;
    }

    @Override
    public void setLogWriter(PrintWriter out) throws SQLException {

    }

    @Override
    public void setLoginTimeout(int seconds) throws SQLException {

    }

    @Override
    public int getLoginTimeout() throws SQLException {
        return 0;
    }

    @Override
    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
        return null;
    }

    static class ConnectionProxy implements InvocationHandler {

        private Object obj;
        private LinkedList pool;
        private String DEFAULT_CLOSE_METHOD = "close";

        private ConnectionProxy(Object obj, LinkedList pool) {
            this.obj = obj;
            this.pool = pool;
        }

        public static Connection getProxy(Object o, LinkedList pool) {
            Object proxed = Proxy.newProxyInstance(o.getClass().getClassLoader(),
                    new Class[]{Connection.class}, new ConnectionProxy(o, pool));
            return (Connection) proxed;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals(DEFAULT_CLOSE_METHOD)) {
                synchronized (pool) {
                    pool.add((Connection) proxy);
                    pool.notify();
                }
                return null;
            } else {
                return method.invoke(obj, args);
            }
        }
    }
}
  1. 配置kylinjdbc模板
@Slf4j
@Configuration
public class KylinConfig {

    @Bean(name = "kylinProperties")
    @ConfigurationProperties(prefix = "spring.datasource.kylin")
    public KylinProperties creatKylinProperties() {
        return new KylinProperties();
    }

    @Bean(name = "kylinDataSource")
    public DataSource KylinDataSource(@Qualifier("kylinProperties") KylinProperties kylinProperties) {
        log.info("-------------------- kylin init ---------------------");
        return new KylinDataSource(kylinProperties);
    }

    @Bean(name = "kylinTemplate")
    public JdbcTemplate prestoJdbcTemplate(@Qualifier("kylinDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
  1. 使用模板
    @Autowired
    @Qualifier("kylinTemplate")
    private JdbcTemplate kylinTemplate;

你可能感兴趣的:(大数据,spring,kylin,java,spring,boot,大数据)