Skip to content

Commit

Permalink
MONDRIAN: added Embedding Mondrian with olap4j API document
Browse files Browse the repository at this point in the history
[git-p4: depot-paths = "//open/mondrian/": change = 10500]
  • Loading branch information
Will Gorman committed Feb 1, 2008
1 parent 15cc10b commit 18b0e7c
Showing 1 changed file with 169 additions and 0 deletions.
169 changes: 169 additions & 0 deletions doc/embedding_mondrian_olap4j.html
@@ -0,0 +1,169 @@
<html>
<!--
== $Id$
== This software is subject to the terms of the Common Public License
== Agreement, available at the following URL:
== http://www.opensource.org/licenses/cpl.html.
== Copyright (C) 2005-2007 Julian Hyde
== All Rights Reserved.
== You must accept the terms of that agreement to use this software.
-->

<head>
<link rel="stylesheet" type="text/css" href="stylesheet.css"/>
<title>Pentaho Analysis Services: Embedding Mondrian</title>
</head>
<body>
<!-- doc2web start -->

<!-- page title -->
<div class="contentheading">Embedding Mondrian in a Java Application</div>
<!-- end page title -->

<p>By Will Gorman; last updated January, 2008.</p>

<hr noshade size="1">

<p>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. </p>
<p>Starting with Mondrian 3.0, we now use the olap4j standard APIs. Please visit <a href="http://www.olap4j.org">http://www.olap4j.org</a> for more documentation.</p>
<h2>Setting up the Environment<a name="setting_up_the_environment">&nbsp;</a></h2>

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

<h3>Download the Latest Release<a name="Download_the_latest_source_release">&nbsp;</a></h3>

<p>Download the latest <code>mondrian-<i>version</i>.zip</code> from <a href="http://sourceforge.net/projects/mondrian">
SourceForge</a>, and unzip. Now find the <code>mondrian-<i>version</i>-src.zip</code>
inside this distribution, and unzip it within the mondrian binary distribution under the "src" directory.</p>

<h2>Installing the Database<a name="installing_the_database">&nbsp;</a></h2>

<p>Before you run this simple example, you must
install the standard FoodMart dataset. This is described in the
<a href="install.html#2_Set_up_test_data">installation guide</a>.</p>

<h2>The Source Code<a name="the_source_code">&nbsp;</a></h2>
<p>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.</p>
<blockquote>
<code>
import java.util.*;<br>
import java.sql.*;<br>
import org.olap4j.*;<br>
import org.olap4j.metadata.*;<br>
<br>
public class SimpleEmbeddedExample {<br>
<br>
&nbsp; public static void main(String args[]) throws Exception {<br>
<br>
&nbsp;&nbsp;&nbsp; // First, set up a valid connection string<br>
&nbsp;&nbsp;&nbsp; String connStr = "jdbc:mondrian:" +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Catalog=demo/FoodMart.xml;" +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "JdbcDrivers=com.mysql.jdbc.Driver;" +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "Jdbc=jdbc:mysql://localhost/foodmart?user=foodmart&password=foodmart";<br>
<br>
&nbsp;&nbsp;&nbsp; // Second, set up a valid query string<br>
&nbsp;&nbsp;&nbsp; String queryStr = "select " +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "{[Measures].[Unit Sales]} on columns, " +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "{[Store].[All Stores]} on rows " +<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "from [Sales]";<br>
<br>
&nbsp;&nbsp;&nbsp; // Third, retrieve a connection from the DriverManager<br>
&nbsp;&nbsp;&nbsp; Class.forName("mondrian.olap4j.MondrianOlap4jDriver"); <br>
&nbsp;&nbsp;&nbsp; Connection jdbcConn = <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DriverManager.getConnection(connStr, new Properties()); <br>
&nbsp;&nbsp;&nbsp; OlapConnection connection = <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ((OlapWrapper) jdbcConn).unwrap(OlapConnection.class); <br>

<br>
&nbsp;&nbsp;&nbsp; // Fourth, execute the MDX Query<br>
&nbsp;&nbsp;&nbsp; OlapStatement olapStatement = connection.createStatement();<br>
&nbsp;&nbsp;&nbsp; CellSet cellSet = olapStatement.executeOlapQuery(queryStr);<br>
<br>
&nbsp;&nbsp;&nbsp; // Fifth, display the Axes<br>
&nbsp;&nbsp;&nbsp; for (CellSetAxis axis : cellSet.getAxes()) { <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.print(axis.getAxisOrdinal() + ": "); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; boolean firstPos = true; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (Position position : axis.getPositions()) { <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (!firstPos) { <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.print(", "); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.print("{"); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; boolean first = true; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (Member member : position.getMembers()) { <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; if (!first) { <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.print(", "); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.print(member.getUniqueName()); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; first = false; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.print("}"); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; firstPos = false; <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(""); <br>
&nbsp;&nbsp;&nbsp; } <br>
<br>
&nbsp;&nbsp;&nbsp; // Finally, display the Cells <br>
&nbsp;&nbsp;&nbsp; CellSetAxis cols = cellSet.getAxes().get(0); <br>
&nbsp;&nbsp;&nbsp; CellSetAxis rows = cellSet.getAxes().get(1); <br>
&nbsp;&nbsp;&nbsp; for (int row = 0; row < rows.getPositions().size(); row++) { <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println("Row #" + (row + 1) + ":"); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; for (int col = 0; col < cols.getPositions().size(); col++) { <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; List<Integer> positions = new ArrayList<Integer>(2); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; positions.add(col); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; positions.add(row); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; Cell cell = cellSet.getCell(positions); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; System.out.println(cell.getFormattedValue()); <br>
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; } <br>
&nbsp;&nbsp;&nbsp; } <br>
&nbsp; } <br>
<br>
} <br>
<br>
</code>
</blockquote>
<p>Note that you should replace the specific jdbc information with your own JDBC connection properties.</p>
<h2>Compiling the Example <a name="compiling_the_example">&nbsp;</a></h2>
<p>To compile this example via the command line:</p>
<blockquote>
<code>
javac -cp "src/lib/olap4j.jar" SimpleEmbeddedExample.java
</code>
</blockquote>

<h2>Running the Example <a name="compiling the example">&nbsp;</a></h2>
<p>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.</p>
<blockquote>
<code>
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<br>
:src/lib/commons-vfs.jar:src/lib/commons-logging.jar:src/lib/commons-math-1.0.jar:src/lib/javacup.jar<br>
:src/lib/eigenbase-resgen.jar:src/lib/eigenbase-properties.jar:src/lib/eigenbase-xom.jar:lib/mondrian.jar<br>
:$JDBC_DRIVER_JAR_LOCATION" SimpleEmbeddedExample
</code>
</blockquote>
<p> You should see this output:</p>
<blockquote>
<code>
log4j:WARN No appenders could be found for logger (mondrian.olap.MondrianProperties).<br>
log4j:WARN Please initialize the log4j system properly.<br>
COLUMNS: {[Measures].[Unit Sales]}<br>
ROWS: {[Store].[All Stores]}<br>
Row #1:<br>
266,773<br>
</code>
</blockquote>

<hr noshade size="1"/>
<p>
Author: Will Gorman; last updated April, 2007.<br/>
Version: $Id$
(<a href="http://p4web.eigenbase.org/open/mondrian/doc/embedding_mondrian.html?ac=1">log&nbsp;</a>)<br/>
Copyright (C) 2005-2007 Julian Hyde
</p>

<br />

<!-- doc2web end -->

</body>
</html>

0 comments on commit 18b0e7c

Please sign in to comment.