Exemple #1
0
  public CellSet execute() throws Exception {

    if (scenario != null && query.getDimension("Scenario") != null) {
      QueryDimension dimension = query.getDimension("Scenario");
      moveDimension(dimension, Axis.FILTER);
      Selection sel =
          dimension.createSelection(
              IdentifierParser.parseIdentifier("[Scenario].[" + getScenario().getId() + "]"));
      if (!dimension.getInclusions().contains(sel)) {
        dimension.getInclusions().add(sel);
      }
    }

    Query mdx = this.query;
    mdx.validate();
    StringWriter writer = new StringWriter();
    mdx.getSelect().unparse(new ParseTreeWriter(new PrintWriter(writer)));
    log.debug("Executing query (" + this.getName() + ") :\n" + writer.toString());
    CellSet cellSet = mdx.execute();
    if (scenario != null && query.getDimension("Scenario") != null) {
      QueryDimension dimension = query.getDimension("Scenario");
      dimension.getInclusions().clear();
      moveDimension(dimension, null);
    }

    return cellSet;
  }
  public static void main(String[] args) throws Exception {

    // Load the driver
    Class.forName("mondrian.olap4j.MondrianOlap4jDriver");

    // Connect
    final Connection connection =
        DriverManager.getConnection(
            "jdbc:mondrian:" // Driver ident
                + "Jdbc=jdbc:hsqldb:file:foodmart/foodmart;" // Relational DB
                + "Catalog=file:foodmart/FoodMart.xml;"); // Catalog

    // We are dealing with an olap connection. we must unwrap it.
    final OlapConnection olapConnection = connection.unwrap(OlapConnection.class);

    // Get a cube object.
    Cube salesCube = olapConnection.getOlapSchema().getCubes().get("Sales");

    // Build a query object.
    Query myQuery = new Query("myQuery", salesCube);

    // Lookup some dimensions
    QueryDimension productDim = myQuery.getDimension("Product");
    QueryDimension storeDim = myQuery.getDimension("Store");
    QueryDimension timeDim = myQuery.getDimension("Time");

    // Place dimensions on some axis
    myQuery.getAxis(Axis.COLUMNS).addDimension(productDim);
    myQuery.getAxis(Axis.ROWS).addDimension(storeDim);
    myQuery.getAxis(Axis.FILTER).addDimension(timeDim);

    // Including a member by metadata
    Member year1997 =
        salesCube.lookupMember(IdentifierNode.ofNames("Time", "1997").getSegmentList());
    timeDim.include(year1997);

    // Including a member by name parts
    productDim.include(
        Selection.Operator.CHILDREN,
        IdentifierNode.ofNames("Product", "Drink", "Beverages").getSegmentList());

    // We can also exclude members
    productDim.exclude(
        IdentifierNode.ofNames("Product", "Drink", "Beverages", "Carbonated Beverages")
            .getSegmentList());

    // Validate this query
    myQuery.validate();

    // Print!
    System.out.println("/********************* QUERY ***********************/");

    System.out.println(myQuery.getSelect().toString());

    System.out.println("/********************* QUERY ***********************/");
    System.out.println(" ");
    System.out.println(" ");
    System.out.println(" ");
    System.out.println(" ");

    System.out.println("/********************* RESULTS ***********************/");

    RectangularCellSetFormatter formatter = new RectangularCellSetFormatter(false);

    PrintWriter writer = new PrintWriter(System.out);

    formatter.format(myQuery.execute(), writer);

    writer.flush();
    System.out.println("/********************* RESULTS ***********************/");
  }