打开Java API for OLAP
Olap4j is an open Java API for accessing OLAP data.
It is an extension to JDBC. For example, its
OlapConnection
class extends
java.sql.Connection,
from which you can create an
OlapStatement,
and execute to create a
CellSet
(analogous to a
java.sql.ResultSet).
There are also similar mechanisms for browsing metadata.
As a result, olap4j is easy to learn if you have JDBC
experience and know a little about OLAP.
Olap4j requires ant (version 1.7 or later) and JDK 1.7 to build. (Once built, it also runs under JDK 1.5 and 1.6.)
$ git clone git://github.com/olap4j/olap4j.git
$ cd olap4j
$ ant
You can now write and run a simple program against olap4j. For
example, you can write:
import org.olap4j.*;
import org.olap4j.metadata.Member;
import java.sql.*;
Class.forName("org.olap4j.driver.xmla.XmlaOlap4jDriver");
Connection connection =
DriverManager.getConnection(
"jdbc:xmla:Server=http://example.com:8080/mondrian/xmla");
OlapConnection olapConnection = connection.unwrap(OlapConnection.class);
OlapStatement statement = olapConnection.createStatement();
CellSet cellSet =
statement.executeOlapQuery(
"SELECT {[Measures].[Unit Sales]} ON 0,\n"
+ "{[Product].Children} ON 1\n"
+ "FROM [Sales]");
for (Position row : cellSet.getAxes().get(1)) {
for (Position column : cellSet.getAxes().get(0)) {
for (Member member : row.getMembers()) {
System.out.println(member.getUniqueName());
}
for (Member member : column.getMembers()) {
System.out.println(member.getUniqueName());
}
final Cell cell = cellSet.getCell(column, row);
System.out.println(cell.getFormattedValue());
System.out.println();
}
}
Or, if you are using the in-process mondrian driver, include mondrian.jar
and its dependencies in your classpath, and change the
appropriate lines in the above code to the following:
Class.forName("mondrian.olap4j.MondrianOlap4jDriver");
Connection connection =
DriverManager.getConnection(
"jdbc:mondrian:"
+ "Jdbc='jdbc:odbc:MondrianFoodMart';"
+ "Catalog='file://c:/open/mondrian/demo/FoodMart.xml';"
+ "JdbcDrivers=sun.jdbc.odbc.JdbcOdbcDriver;");
The core API of olap4j version 1.0 is a Long Term Support (LTS) release,
but some parts of the olap4j project will remain considered as experimental,
thus subject to change in future releases.
Core packages are as follows:
The following packages are considered experimental and are subject to change:
Olap4j version 2.0 is currently under development. Goals are:
We aim to be backwards compatible in the same way that each JDBC release is backwards compatible:
Version 2 specification is here.
General project information:
Related projects:
If you have downloaded a release: