By Will Gorman; last updated January, 2008.
This document shows a simple example of embedding mondrian in a java application. The steps include downloading Mondrian, installing a database, writing a simple application, compiling the application, and running the application.
Starting with Mondrian 3.0, we now use the olap4j standard APIs. Please visit http://www.olap4j.org for more documentation.
First, you need to download mondrian. You can get the latest release from SourceForge.
Download the latest mondrian-version.zip
from
SourceForge, and unzip. Now find the mondrian-version-src.zip
inside this distribution, and unzip it within the mondrian binary distribution under the "src" directory.
Before you run this simple example, you must install the standard FoodMart dataset. This is described in the installation guide.
Here is a simple example of embedding mondrian in a java class. A connection is retrieved, and a query is executed. Open the file SimpleEmbeddedExample.java in the main Mondrian directory and paste the contents below.
import java.util.*;
import java.sql.*;
import org.olap4j.*;
import org.olap4j.metadata.*;
public class SimpleEmbeddedExample {
public static void main(String args[]) throws Exception {
// First, set up a valid connection string
String connStr = "jdbc:mondrian:" +
"Catalog=demo/FoodMart.xml;" +
"JdbcDrivers=com.mysql.jdbc.Driver;" +
"Jdbc=jdbc:mysql://localhost/foodmart?user=foodmart&password=foodmart";
// Second, set up a valid query string
String queryStr = "select " +
"{[Measures].[Unit Sales]} on columns, " +
"{[Store].[All Stores]} on rows " +
"from [Sales]";
// Third, retrieve a connection from the DriverManager
Class.forName("mondrian.olap4j.MondrianOlap4jDriver");
Connection jdbcConn =
DriverManager.getConnection(connStr, new Properties());
OlapConnection connection =
((OlapWrapper) jdbcConn).unwrap(OlapConnection.class);
// Fourth, execute the MDX Query
OlapStatement olapStatement = connection.createStatement();
CellSet cellSet = olapStatement.executeOlapQuery(queryStr);
// Fifth, display the Axes
for (CellSetAxis axis : cellSet.getAxes()) {
System.out.print(axis.getAxisOrdinal() + ": ");
boolean firstPos = true;
for (Position position : axis.getPositions()) {
if (!firstPos) {
System.out.print(", ");
}
System.out.print("{");
boolean first = true;
for (Member member : position.getMembers()) {
if (!first) {
System.out.print(", ");
}
System.out.print(member.getUniqueName());
first = false;
}
System.out.print("}");
firstPos = false;
}
System.out.println("");
}
// Finally, display the Cells
CellSetAxis cols = cellSet.getAxes().get(0);
CellSetAxis rows = cellSet.getAxes().get(1);
for (int row = 0; row < rows.getPositions().size(); row++) {
System.out.println("Row #" + (row + 1) + ":");
for (int col = 0; col < cols.getPositions().size(); col++) {
List positions = new ArrayList(2);
positions.add(col);
positions.add(row);
Cell cell = cellSet.getCell(positions);
System.out.println(cell.getFormattedValue());
}
}
}
}
Note that you should replace the specific jdbc information with your own JDBC connection properties.
To compile this example via the command line:
javac -cp "src/lib/olap4j.jar" SimpleEmbeddedExample.java
Below is the java command line that will execute the SimpleEmbeddedExample class. Note that you must replace $JDBC_DRIVER_JAR_LOCATION with the correct path to your specific JDBC driver.
java -cp ".:src/lib/olap4j.jar:src/lib/log4j-1.2.9.jar:src/lib/commons-dbcp.jar:src/lib/commons-pool.jar:src/lib/commons-collections.jar
:src/lib/commons-vfs.jar:src/lib/commons-logging.jar:src/lib/commons-math-1.0.jar:src/lib/javacup.jar
:src/lib/eigenbase-resgen.jar:src/lib/eigenbase-properties.jar:src/lib/eigenbase-xom.jar:lib/mondrian.jar
:$JDBC_DRIVER_JAR_LOCATION" SimpleEmbeddedExample
You should see this output:
log4j:WARN No appenders could be found for logger (mondrian.olap.MondrianProperties).
log4j:WARN Please initialize the log4j system properly.
COLUMNS: {[Measures].[Unit Sales]}
ROWS: {[Store].[All Stores]}
Row #1:
266,773
Author: Will Gorman; last updated April, 2007.
Version: $Id$
(log )
Copyright (C) 2005-2009 Pentaho