Pentaho Analysis Services: Embedding Mondrian

Embedding Mondrian in a Java Application

By Will Gorman; last updated April, 2007.

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.

## Setting up the Environment

First, you need to download mondrian. You can get the latest release from SourceForge.

### Download the Latest Release

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.

## Installing the Database

Before you run this simple example, you must install the standard FoodMart dataset. This is described in the installation guide.

## The Source Code

Here is a simple example of embedding mondrian in a java class. A connection is retrieved, a query is generated and finally the query is executed. Open the file SimpleEmbeddedExample.java in the main Mondrian directory and paste the contents below.

> `import mondrian.olap.*;
> import java.io.*;
> public class SimpleEmbeddedExample {
> public static void main(String args[]) {
> // First, set up a valid connection string
> String connStr = "Provider=mondrian;" +
> "Catalog=demo/FoodMart.xml;" +
> "JdbcDrivers=com.mysql.jdbc.Driver;" +
> "Jdbc=jdbc:mysql://localhost/foodmart?user=foodmart&password=foodmart;" +
> "jdbcUser=foodmart;" +
> "jdbcPassword=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 DriveManager
> Connection connection = DriverManager.getConnection(connStr, null, true);
> // Fourth, generate a MDX Query object
> Query query = connection.parseQuery(queryStr);
> // Fifth, execute the query
> Result result = connection.execute(query);
> // Finally, print out the result
> StringWriter sw = new StringWriter();
> PrintWriter pw = new PrintWriter(sw);
> result.print(pw);
> pw.flush();
> System.out.println(sw.toString());
> }
> }
> `

Note that you should replace the specific jdbc information with your own JDBC connection properties.

## Compiling the Example

To compile this example via the command line:

> `javac -cp "lib/mondrian.jar" SimpleEmbeddedExample.java`

## Running the Example

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/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.
> Axis #0:
> {}
> Axis #1:
> {[Measures].[Unit Sales]}
> Axis #2:
> {[Store].[All Stores]}
> Row #0: 266,773
> `

View Mondrian's API for more details on traversing the Result object.

Author: Will Gorman; last updated April, 2007.
Version: $Id$ (log )
Copyright (C) 2005-2007 Pentaho