1

I am new to Mondrian. I am using it in my project for OLAP operations. I am testing it with Foodmart database. The problem is that I need the OLAP operations results in JSON format. I know that mondrian has the same structure as JSON in the form of hierarchies. I want to generate a JSON file as an output from the result of mondrian MDX query. The result should be similar to OLAP operations. I don't know how to iterate over the result generated from MDX query. Here is the code.

String connStr =   "Provider=mondrian;" +
                    "Catalog=/WEB-INF/FoodMart.xml;" +
                    "JdbcDrivers=com.mysql.jdbc.Driver;" +
                    "Jdbc=jdbc:mysql://localhost/foodmart;" +
                    "jdbcUser=root;" +
                    "jdbcPassword=;";

String queryStr ="select {[Measures].[Unit Sales], [Measures].[Store Cost], [Measures].>Store Sales]} ON COLUMNS,"+"Crossjoin(Hierarchize(Union({[Promotion Media].[All Media]}, >[Promotion Media].[All Media].Children)), {[Product].[All Products]})
ON ROWS"+" from [Sales]"+"where [Time].[1997]";

Connection connection = DriverManager.getConnection(connStr, null);        
Query query = connection.parseQuery(queryStr);

Result result = connection.execute(query);
result.print(new PrintWriter(System.out));

Actually I need to perform OLAP operations on data warehouse which is stored in MySQL. The resulted data should be in JSON format which I will pass to D3 http://mbostock.github.com/d3 for visualizations. For data format I have to use JSON format. Please any suggestions how to iterate MDX result and convert it in JSON file. I am using Pentaho Mondrian for this purpose. Thanks.

4

2 回答 2

1

这是我想你想做的一个例子:

Class.forName("mondrian.olap4j.MondrianOlap4jDriver"); //load the driver

Connection connection = DriverManager.getConnection("Provider=mondrian;" +
                "Catalog=/WEB-INF/FoodMart.xml;" +
                "JdbcDrivers=com.mysql.jdbc.Driver;" +
                "Jdbc=jdbc:mysql://localhost/foodmart;" +
                "jdbcUser=root;" +
                "jdbcPassword=;");

OlapWrapper wrapper = (OlapWrapper) connection;
OlapConnection olapConnection = wrapper.unwrap(OlapConnection.class);

CellSet cellSet = statement.executeOlapQuery(query);
CellSetAxis rows = cellSet.getAxes().get(1); //cube rows
CellSetAxis columns = cellSet.getAxes().get(0); //cube columns

int resultSize = rows.getPositionCount() * columns.getPositionCount();
String resultValues[] = new String[resultSize];
int valueIndex = 0;

for (Position row : rows) {
    for (Position column : columns) {
        Cell cell = cellSet.getCell(column, row);
        String cellValue = cell.getFormattedValue();
        resultValues[valueIndex++] = cellValue;
    }
}

Gson gson = new Gson(); //gson library instance
String resultString = gson.toJson(responseValues); //json string
olapConnection.close();
connection.close();
于 2015-04-09T15:52:43.797 回答
1

如果您正在使用 PHP,您可以使用此库将 xmla 结果转换为 Json http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/

于 2012-08-20T20:38:39.177 回答