Mondiran创建连接

Mondrian作为OLAP服务器与传统数据库不同,它不直接保存数据,而是将MDX转换为SQL。创建连接时, Mondrian使用特定的URL格式,如`jdbc:mondrian:...`,包含`Jdbc`和`Catalog`属性。通过`DriverManager.getConnection()`,Mondrian的`MondrianOlap4jDriver`驱动注册并创建连接。在连接过程中, Mondrian解析URL,处理属性,注册驱动,最后创建`MondrianOlap4jConnection`,实现与OLAP引擎的交互。整个过程涉及类加载、DriverManager、注册驱动和连接池等机制。
摘要由CSDN通过智能技术生成
以前使用jdbc创建连接的时候使用的url是这样的形式: jdbc:mysql://hostname:port/database?key1=value1&key2=value2,在URL需要以"jabc:mysql"开头的,其中需要声明数据库服务器的地址和端口、数据库名还有一些其它的属性,以"?"分割,每一个属性使用"&"字符分割,但是mondrian作为OLAP服务器和mysql这类的关系型数据库还是有所区别的,毕竟它本身不保存任何数据,它更像是一个工具类的东西(类似于hive),能够把MDX翻译成一串SQL语句再交由关系数据库执行,所以它不能独立得称为一个服务器(在我的理解中,对于服务器的使用只需要知道IP和port以及一些其他的参数就足够了)。
     为了能够使用java自带的DriverManager创建connection,mondrian做了这个兼容,在使用mysql这样的数据库的时候我们会首先执行如下的操作:
Class. forName(driver);
connection = DriverManager. getConnection(url , username ,password );

getConnection函数返回的是 java.sql.Connection对象,这个Connection是通用的数据库连接,但是mondrian也是用了相同的方式创建到OLAP引擎的连接,它的driver为mondrian.olap4j.MondrianOlap4jDriver,它的url有自己独特的结构,下面就看一下在mondrian中是如何创建连接的,除了之上的两部,还需要再进行其他的操作:
Class. forName(driver);
connection = DriverManager. getConnection(url , username ,password );
OlapConnection olapConnection = connection.unwrap(OlapConnection.class);

     在mondrian中的url格式如下:jdbc:mondrian: Jdbc=jdbc:mysql://10.241.20.157:3306/foodmart?user=root&password=root; Catalog=C:\\Users\\Administrator\\Desktop\\nrtp\\FoodMart.xml;可以看出url中的每一项是通过";"分割的,类似于mysql的开头是"jdbc:mysql",mondrian连接的url的开头必须是"jdbc:mondrian:",另外还包括"Jdbc"和"Catalog"属性字段,在DriverManager的getConnection静态方法中执行如下操作:
    public static Connection getConnection(String url,
        String user, String password) throws SQLException {
        java.util.Properties info = new java.util.Properties();
        if (user != null) {
            info.put("user", user);
        }
        if (password != null) {
            info.put("password", password);
        }
        return (getConnection(url, info, Reflection.getCallerClass()));
    }

这个函数是所有的数据库连接创建的方式,其实就是创建一个Properties对象,然后加上user和password属性,然后调用其它的getConnection方法:
    private static Connection getConnection(
        String url, java.util.Properties info, Class<?> caller) throws SQLException {
        /*
         * When callerCl is null, we should check the application's
         * (which is invoking this class indirectly)
         * classloader, so that the JDBC driver class outside rt.jar
         * can be loaded from here.
         */
        ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
        synchronized (DriverManager.class) {
            // synchronize loading of the correct classloader.
            if (callerCL == null) {
                callerCL = Thread.currentThread().getContextClassLoader();
            }
        }

        if(url == null) {
            throw new SQLException("The url cannot be null", "08001");
        }

        println("DriverManager.getConnection(\"" + url + "\")");

        //
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值