Adobe Flex 3 Help

Writing a query for a simple OLAP cube

In a simple OLAP cube, all of the dimensions of the cube define a single level. For example, you have flat data that contains three fields:

data:Object = {
    product:"ColdFusion"
    quarter :"Q1"
    revenue: "100.00", 
}

The data fields of each record can contain the following values:

  • The product field can have the values: ColdFusion, Flex, Dreamweaver, and Illustrator.
  • The quarter field can have the values: Q1, Q2, Q3, and Q4.

The OLAP schema for this data defines two dimensions, each with a single level, as the following code shows:

<mx:OLAPCube name="FlatSchemaCube" 
    dataProvider="{flatData}" 
    id="myMXMLCube"
    complete="runQuery(event);">
     
    <mx:OLAPDimension name="ProductDim">
        <mx:OLAPAttribute name="Product" dataField="product"/>
        <mx:OLAPHierarchy name="ProductHier" hasAll="true">
            <mx:OLAPLevel attributeName="Product"/>
        </mx:OLAPHierarchy>
    </mx:OLAPDimension>

    <mx:OLAPDimension name="QuarterDim">
        <mx:OLAPAttribute name="Quarter" dataField="quarter"/>
        <mx:OLAPHierarchy name="QuarterHier" hasAll="true">
            <mx:OLAPLevel attributeName="Quarter"/>
        </mx:OLAPHierarchy> 
    </mx:OLAPDimension>
    
    <mx:OLAPMeasure name="Revenue" 
        dataField="revenue" 
        aggregator="SUM"/>
</mx:OLAPCube>

After you call OLAPCube.refresh() to initialize the cube, it has the following structure:

ProductDim                             // Dimension
    Product                             // Hierarchy
        (All)                            // Member
        ColdFusion                            // Member
        Flex                            // Member
        Dreamweaver                            // Member
        Illustrator                            // Member

    ProductHier                             // Hierarchy 
        (All)                            // Level
            ColdFusion                            // Member
            Flex                            // Member
            Dreamweaver                            // Member
            Illustrator                            // Member
            
        Product                             // Level
            ColdFusion                            // Member
            Flex                            // Member
            Dreamweaver                            // Member
            Illustrator                            // Member

QuarterDim                             // Dimension 
    Quarter                             // Hierarchy
        (All)                            // Member
        Q1                            // Member
        Q2                            // Member
        Q3                            // Member
        Q4                            // Member

    QuarterHier                             // Hierarchy
        (All)                            // Level
            Q1                            // Member
            Q2                            // Member
            Q3                            // Member
            Q4                            // Member
            
        Quarter                            // Level
            Q1                            // Member
            Q2                            // Member
            Q3                            // Member
            Q4                            // Member

Notice in this cube:

  • The ProductDim dimension contains two hierarchies: Product and ProductHier. The cube creates a hierarchy for each level specified by the OLAPLevel class, and for each hierarchy specified by the OLAPHierarchy class. The same is true for the QuarterDim dimension; it also contains two hierarchies.
  • The order of the members, meaning the values along each dimension, is based on the order in which the members appear in the flat data. In this example, the quarters appear in order of Q1, Q2, Q3, and Q4 because the flat data was sorted by quarter. However, if the data was not sorted by quarter, the members along the QuarterDim could appear in any order.
  • Each dimension contains a single level; therefore the (All) level contains the same data as the first level in the hierarchy. For example, the (All) level of the ProdcutHier hierarchy contains the same data as the Product level. For a complex cube, the (All) level would contain additional information.

For an example of a complex cube, see Writing a query for a complex OLAP cube.

Defining a query for a simple cube

An OLAP query has the following basic form:

// Create an instance of OLAPQuery to represent the query. 
var query:OLAPQuery = new OLAPQuery;
            
// Get the row axis from the query instance.
var rowQueryAxis:IOLAPQueryAxis = query.getAxis(OLAPQuery.ROW_AXIS);
// Create an OLAPSet instance to configure the axis.
var productSet:OLAPSet = new OLAPSet;
// Use OLAPSet.addElements() or OLAPSet.addElement() to add members to the row axis.
productSet.addElements(...);
// Add the OLAPSet instance to the axis.
rowQueryAxis.addSet(productSet);

// Get the column axis from the query instance, and configure it
// to aggregate the columns by the Quarter dimension. 
var colQueryAxis:IOLAPQueryAxis = query.getAxis(OLAPQuery.COLUMN_AXIS);         
var quarterSet:OLAPSet= new OLAPSet;
// Use OLAPSet.addElements() or OLAPSet.addElement() to add members to the column axis.
productSet.addElements(...);
colQueryAxis.addSet(quarterSet);

You use the following methods and properties to extract information from an OLAP cube based on the attributes of the dimension, and pass it to the OLAPSet.addElements() or OLAPSet.addElement() method:

Method and property

Description

OLAPCube.findDimension(name:String):IOLAPDimension

Returns a dimension of a schema as an instance of the OLAPDimension class.

OLAPDimension.findAttribute(name:String):IOLAPAttributeHierarchy

Returns an instance of the IOLAPAttributeHierarchy interface, which is an attribute hierarchy that includes an IList instance of all members of the attribute.

OLAPDimension.findMember(name:String):IOLAPMember

Returns an IOLAPMember instance that represents a member with the specified name within the dimension.

IOLAPAttributeHierarchy.children

Contains all members of the level as an IList instance, but does not include the (All) member.

IOLAPAttributeHierarchy.members

Contains all members of the level as an IList instance, including the (All) member.

The following table shows the information returned for combinations of these methods and properties:

Reference to method and property

Returns

findDimension("ProductDim").findAttribute("Product").children

ColdFusion, Flex, Dreamweaver, and Illustrator

findDimension("ProductDim").findAttribute("Product").members

(All), ColdFusion, Flex, Dreamweaver, and Illustrator

findDimension("ProductDim").findMember("Flex")

Flex

findDimension("QuarterDim").findAttribute("Quarter").children

Q1, Q2, Q3, and Q4

findDimension("QuarterDim").findAttribute("Quarter").members

(All), Q1, Q2, Q3, and Q4

findDimension("QuarterDim").findMember("Q2")

Q2

Rather than use the findAttribute() method, you can drill down through the hierarchy of the cube by calling the following methods:

Method

Description

OLAPDimension.findHierarchy(name:String):IOLAPHierarchy

Returns a hierarchy of a dimension as an instance of OLAPHierarchy. You can specify either an OLAPHierarchy or an OLAPLevel instance to this method.

OLAPHierarchy.findLevel(name:String):IOLAPLevel

Returns a level of a hierarchy as an instance of OLAPLevel.

OLAPLevel.findMember(name:String):IList

Returns an IList instance that contains all IOLAPMember instances that match the String argument. You can then use the IList.getItemAt() method to access the any element in the IList.

The following table shows the information returned for different combinations of these methods:

Reference to method

Returns

findDimension("ProductDim").findhHierarchy("ProductHier"). findLevel("Product").children

ColdFusion, Flex, Dreamweaver, and Illustrator

findDimension("ProductDim").findHierarchy("ProductHier"). findLevel("Product").members

(All), ColdFusion, Flex, Dreamweaver, and Illustrator

IOLAPElement(cube.findDimension("ProductDim"). findHierarchy("ProductHier").findLevel("Product"). findMember("Flex").getItemAt(0))

Flex. Cast the result to an instance of IOLAPElement because getItemAt() returns an Object.

findDimension("QuarterDim").findHierarchy("QuarterHier"). findLevel("Quarter").children

Q1, Q2, Q3, and Q4

findDimension("QuarterDim").findHierarchy("QuarterHier"). findLevel("Quarter").members

(All), Q1, Q2, Q3, and Q4

IOLAPElement(cube.findDimension("QuarterDim"). findHierarchy("QuarterHier").findLevel("Quarter"). findMember("Q2").getItemAt(0))

Q2. Cast the result to an instance of IOLAPElement because getItemAt() returns an Object.

In the case of a simple cube, the OLAPDimension.findHierarchy(), OLAPHierarchy.findLevel(), and OLAPLevel.findMember() methods do not provide you with additional functionality from the OLAPDimension.findAttribute() and OLAPDimension.findMember() methods. They are more commonly used with complex cubes that contain dimensions with multiple levels. For more information, see Writing a query for a complex OLAP cube.

Add all members to an axis

The most common type of query returns a data aggregation for all members of an attribute of a schema. For example, you define an OLAP cube using a schema that contains a ProductDim and a QuarterDim dimension, as shown in the section Writing a query for a simple OLAP cube. You then want to generate a query for all products for all quarters. The following query extracts this information:

private function getQuery(cube:IOLAPCube):IOLAPQuery {
    // Create an instance of OLAPQuery to represent the query. 
    var query:OLAPQuery = new OLAPQuery;

    // Get the row axis from the query instance.
    var rowQueryAxis:IOLAPQueryAxis = query.getAxis(OLAPQuery.ROW_AXIS);
    // Create an OLAPSet instance to configure the axis.
    var productSet:OLAPSet = new OLAPSet;
    productSet.addElements(
        cube.findDimension("ProductDim").findAttribute("Product").children);
    rowQueryAxis.addSet(productSet);

    var colQueryAxis:IOLAPQueryAxis = query.getAxis(OLAPQuery.COLUMN_AXIS); 
    var quarterSet:OLAPSet= new OLAPSet;
    quarterSet.addElements(
        cube.findDimension("QuarterDim").findAttribute("Quarter").children);
    colQueryAxis.addSet(quarterSet);

    return query; 
}

The executing SWF file for the previous example is shown below:

In this example, the column axis uses the findDimension() and findAttribute() methods to drill down into the ProductDim and QuarterDim dimensions to extract sales data. For each axis, you use the IOLAPAttributeHierarchy.children property to populate it with the all members of the attribute, but do not include the (All) member.

This query uses the default measure to populate the query result, where the default measure is the first measure defined in the cube's schema. Therefore, you do have to specify the measure as part of the query. For information on explicitly specifying the measure in the query, see Creating a query using a nondefault measure.

Notice that in this example, you did not have to specify the hierarchy name, ProductHier and QuarterDim, to extract the member from the schema. It is unnecessary to specify the hierarchy name when you only want to extract data for a single level of a dimension.

However, you could rewrite this example to explicitly drill down through the cube by calling the methods OLAPDimension.findHierarchy() and OLAPHierarchy.findLevel(), as the following example shows:

private function getQuery(cube:IOLAPCube):IOLAPQuery {
    // Create an instance of OLAPQuery to represent the query. 
    var query:OLAPQuery = new OLAPQuery;
            
    // Get the row axis from the query instance.
    var rowQueryAxis:IOLAPQueryAxis = query.getAxis(OLAPQuery.ROW_AXIS);
    // Create an OLAPSet instance to configure the axis.
    var productSet:OLAPSet = new OLAPSet;
    productSet.addElements(cube.findDimension(
        "ProductDim").findHierarchy("ProductHier").findLevel("Product").members);
    rowQueryAxis.addSet(productSet);

    var colQueryAxis:IOLAPQueryAxis = query.getAxis(OLAPQuery.COLUMN_AXIS);         
    var quarterSet:OLAPSet= new OLAPSet;
    quarterSet.addElements(
        cube.findDimension("QuarterDim").findHierarchy("QuarterHier").
        findLevel("Quarter").members);
    colQueryAxis.addSet(quarterSet);
            
    return query;       
}

The executing SWF file for the previous example is shown below:

In this example, you drill down through the dimension to access the Product and Quarter levels. You typically use this technique with complex cubes where you are interested in an explicit member of a dimension.

Add explicit members to a query

In the query shown in the previous section, you obtain sales data for all members of the Product and Quarter levels of the schema. But, what if you only want sales data for a single product, or for a single quarter? When you drill down into a dimension, you can specify the individual member of the dimension that you want to query.

For example, you want to create a query to aggregate quarterly sales data only for Flex, but not for any other products. You therefore use the OLAPDimension.findMember() method to specify the name of the member. This method takes a String containing the name of the member, and returns an IOLAPMember instance that defines the member, as the following example shows:

private function getQuery(cube:IOLAPCube):IOLAPQuery {
    // Create an instance of OLAPQuery to represent the query. 
    var query:OLAPQuery = new OLAPQuery;
            
    // Get the row axis from the query instance.
    var rowQueryAxis:IOLAPQueryAxis = query.getAxis(OLAPQuery.ROW_AXIS);
    // Create an OLAPSet instance to configure the axis.
    var productSet:OLAPSet = new OLAPSet;
    productSet.addElement(cube.findDimension("ProductDim").findMember("Flex"));
    rowQueryAxis.addSet(productSet);
            
    var colQueryAxis:IOLAPQueryAxis = 
    query.getAxis(OLAPQuery.COLUMN_AXIS);         
    var quarterSet:OLAPSet= new OLAPSet;
    quarterSet.addElements(
        cube.findDimension("QuarterDim").findAttribute("Quarter").children);
    colQueryAxis.addSet(quarterSet);
            
    return query;       
}

The executing SWF file for the previous example is shown below:

Notice that in the previous example, you use the OLAPSet.addElement() method to add the Flex member to the OLAPSet instance, rather than the OLAPSet.addElements() method. This is because you are adding a single member to the axis, rather than multiple members.

The only issue with using the OLAPDimension.findMember() method is that it returns the first IOLAPMember instance in the cube that matches the String argument. If you think that you might have multiple instances of a member, you can rewrite this example to explicitly drill down through the cube by calling the OLAPDimension.findHierarchy(), OLAPHierarchy.findLevel(), and OLAPLevel.findMember() methods. This situation is more common with cubes that contain complex dimensions. For more information and examples, see Writing a query for a complex OLAP cube.

The OLAPLevel.findMember() method returns an IList instance that contains all IOLAPMember instances that match the String argument. You can then use the IList.getItemAt() method to access any element in the IList, as the following example shows:

private function getQuery(cube:IOLAPCube):IOLAPQuery {
    // Create an instance of OLAPQuery to represent the query. 
    var query:OLAPQuery = new OLAPQuery;

    // Get the row axis from the query instance.
    var rowQueryAxis:IOLAPQueryAxis = query.getAxis(OLAPQuery.ROW_AXIS);
    // Create an OLAPSet instance to configure the axis.
    var productSet:OLAPSet = new OLAPSet;
    // Get the first IOLAPElement instance in the IList instance.
    productSet.addElement(
        IOLAPElement(cube.findDimension("ProductDim").findHierarchy("ProductHier").
        findLevel("Product").findMember("Flex").getItemAt(0)));
    rowQueryAxis.addSet(productSet);

    var colQueryAxis:IOLAPQueryAxis = query.getAxis(OLAPQuery.COLUMN_AXIS);         
    var quarterSet:OLAPSet= new OLAPSet;
    quarterSet.addElement(IOLAPMember(cube.findDimension("TimeDim").
        findHierarchy("Month").findMember("January")));
    quarterSet.addElement(IOLAPMember(cube.findDimension("TimeDim").
        findHierarchy("Month").findMember("February")));
    colQueryAxis.addSet(quarterSet);

    return query;       
}

The executing SWF file for the previous example is shown below: