📄 embedding_mondrian_olap4j.html
字号:
<html><!-- == $Id: //open/mondrian-release/3.0/doc/embedding_mondrian_olap4j.html#2 $ == 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"> </a></h2><p>First, you need to download mondrian. You can get the latest releasefrom SourceForge.</p><h3>Download the Latest Release<a name="Download_the_latest_source_release"> </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"> </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"> </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> public static void main(String args[]) throws Exception {<br><br> // First, set up a valid connection string<br> String connStr = "jdbc:mondrian:" +<br> "Catalog=demo/FoodMart.xml;" +<br> "JdbcDrivers=com.mysql.jdbc.Driver;" +<br> "Jdbc=jdbc:mysql://localhost/foodmart?user=foodmart&password=foodmart";<br><br> // Second, set up a valid query string<br> String queryStr = "select " +<br> "{[Measures].[Unit Sales]} on columns, " +<br> "{[Store].[All Stores]} on rows " +<br> "from [Sales]";<br><br> // Third, retrieve a connection from the DriverManager<br> Class.forName("mondrian.olap4j.MondrianOlap4jDriver"); <br> Connection jdbcConn = <br> DriverManager.getConnection(connStr, new Properties()); <br> OlapConnection connection = <br> ((OlapWrapper) jdbcConn).unwrap(OlapConnection.class); <br><br> // Fourth, execute the MDX Query<br> OlapStatement olapStatement = connection.createStatement();<br> CellSet cellSet = olapStatement.executeOlapQuery(queryStr);<br><br> // Fifth, display the Axes<br> for (CellSetAxis axis : cellSet.getAxes()) { <br> System.out.print(axis.getAxisOrdinal() + ": "); <br> boolean firstPos = true; <br> for (Position position : axis.getPositions()) { <br> if (!firstPos) { <br> System.out.print(", "); <br> } <br> System.out.print("{"); <br> boolean first = true; <br> for (Member member : position.getMembers()) { <br> if (!first) { <br> System.out.print(", "); <br> } <br> System.out.print(member.getUniqueName()); <br> first = false; <br> } <br> System.out.print("}"); <br> firstPos = false; <br> } <br> System.out.println(""); <br> } <br><br> // Finally, display the Cells <br> CellSetAxis cols = cellSet.getAxes().get(0); <br> CellSetAxis rows = cellSet.getAxes().get(1); <br> for (int row = 0; row < rows.getPositions().size(); row++) { <br> System.out.println("Row #" + (row + 1) + ":"); <br> for (int col = 0; col < cols.getPositions().size(); col++) { <br> List<Integer> positions = new ArrayList<Integer>(2); <br> positions.add(col); <br> positions.add(row); <br> Cell cell = cellSet.getCell(positions); <br> System.out.println(cell.getFormattedValue()); <br> } <br> } <br> } <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"> </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"> </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: //open/mondrian-release/3.0/doc/embedding_mondrian_olap4j.html#2 $ (<a href="http://p4web.eigenbase.org/open/mondrian/doc/embedding_mondrian.html?ac=1">log </a>)<br/> Copyright (C) 2005-2007 Julian Hyde</p><br /><!-- doc2web end --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -