Channels ▼
RSS

Tools

olap4j: Online Analytical Processing for Java


Executing Queries

Executing a query against an OLAP server is similar to what you would do for a relational database. Queries can consist of a query parsed into a SelectNode object, as in Listing Five(a), or executed using a String of MDX, as in Listing Five(b).

Listing Five(a): Executing a query with SelectNode.

// We can execute the parsed SelectNode itself...
CellSet data =
  oConn
    .createStatement()
      .executeOlapQuery(parsedObject);

Listing Five(b): Executing a query with MDX.

// ...or use the MDX query directly.
data =
  oConn
    .createStatement()
      .executeOlapQuery(myQuery);

Reading the Results of an OLAP Query

Once the query is executed, you can explore the returned data. A CellSet object is different than a ResultSet. You must think of CellSets as data organized on any number of axes. On each of those axes, there are a series of positions. Each position describes a set of tuples, formed by cross-joining all the members of the current axis.

Listing Six: Iterating over the results of an olap4j query

// Iteration over a two-axis query
for (
  Position axis_0 
  : data.getAxes().get( Axis.ROWS.axisOrdinal() ).getPositions()) 
{
  for (
    Position axis_1
    : data.getAxes().get(Axis.COLUMNS.axisOrdinal()).getPositions())
  {
    Object value =
      data.getCell(axis_0, axis_1)
        .getValue();
  }
}

The code in Listing Six demonstrates how to iterate over the results of a query that included only two axes; columns and rows. If your query uses more than two axes, will need to iterate over each of them. In our simple two-dimensional use case, olap4j includes a very handy helper package to help display the results of the query:


// We use the utility formatter.
RectangularCellSetFormatter formatter =
  new RectangularCellSetFormatter(false);

// Print out.
PrintWriter writer = new PrintWriter(System.out);
formatter.format(cellSet, writer);
writer.flush();

Going Further

With the information presented so far, you should now be able to connect, execute queries against an OLAP server and obtain meaningful results. The olap4j project can get you far beyond this point. The Resources section contains links to materials that showcase the most advanced features, including how to build a user interface to explore OLAP data sources using a programmatic query model, statistical simulations, and real-time updates.

Resources

olap4j.org: Home of the olap4j project.

olap4j.org/api: The latest API reference.

olap4j download resources.


— Luc Boudreau is a co-manager of the olap4j project and Mondrian, an open source OLAP engine. He works at Pentaho Corporation as a Senior Software Engineer and is an active member of many community projects related to business intelligence.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video