我应该使用什么代替不推荐使用的mondrian.olap.Connection :: execute?

时间:2018-11-21 14:51:38

标签: mdx olap mondrian olap4j

所以我试图在Java 1.8中使用Mondrian 3.14,所以我做了这样的事情:

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);

但是,如here所述,Connection::execute已过时:

  

已弃用。此方法已弃用,并将在mondrian-4.0中删除。它通过内部创建一个语句来运行。最好使用olap4j并显式创建一条语句。

那么,我应该在olap中使用什么来执行查询?

1 个答案:

答案 0 :(得分:1)

所以我实际上找到了答案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();
}