mdxolapmondrianolap4j

What should I use instead of deprecated mondrian.olap.Connection::execute?


So I am trying to use Mondrian 3.14 in Java 1.8, so I did something like this :

String catalogFilePath = "<path>/mdx_schema.xml";
String connectUrl = "Provider=mondrian;" +
            "Jdbc=jdbc:postgresql://example.com:5432/database?user=user&password=pass;" +
            "JdbcDrivers=org.postgresql.Driver;" +
            "Catalog=file:" + catalogFilePath + ";";
Connection connection = DriverManager.getConnection(connectUrl, null);

String qs = "<query>";
Query q = connection.parseQuery(qs);
Result r = connection.execute(q);

However, as stated here, Connection::execute is deprecated :

Deprecated. This method is deprecated and will be removed in mondrian-4.0. It operates by internally creating a statement. Better to use olap4j and explicitly create a statement.

So, what should I use in olap to execute the query ?


Solution

  • So I actually found the answer here :

    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    import org.olap4j.CellSet;
    import org.olap4j.OlapConnection;
    import org.olap4j.OlapStatement;
    import org.olap4j.OlapWrapper;
    import org.olap4j.layout.RectangularCellSetFormatter;
    import java.sql.SQLException;
    
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("mondrian.olap4j.MondrianOlap4jDriver");
    
        String catalogFilePath = "<path>/mdx_schema.xml";
        String connectUrl = "jdbc:mondrian:" +
        "Jdbc=jdbc:postgresql://example.com:5432/database?user=user&password=pass;" +
        "JdbcDrivers=org.postgresql.Driver;" +
        "Catalog=file:" + catalogFilePath + ";";
    
        Connection connection = DriverManager.getConnection(connectUrl);
        OlapWrapper wrapper = (OlapWrapper) connection;
        OlapConnection olapConnection = wrapper.unwrap(OlapConnection.class);
        OlapStatement statement = olapConnection.createStatement();
    
        String qs = "<query>";
    
        CellSet cellSet = statement.executeOlapQuery(qs);
    
        RectangularCellSetFormatter formatter = new RectangularCellSetFormatter(false);
        PrintWriter writer = new PrintWriter(System.out);
        formatter.format(cellSet, writer);
        writer.flush();
    
        connection.close();
    }