Oracle® OLAP Developer's Guide to the OLAP API 10g Release 1 (10.1) Part Number B10335-02 |
|
|
View PDF |
This chapter explains the procedure for discovering the metadata in a data store through the OLAP API.
This chapter includes the following topics:
For the complete code of the examples in this chapter, see the example programs available from the Overview of the Oracle OLAP Java API Reference.
The OLAP API provides access to a collection of Oracle data for which a database administrator has created OLAP Catalog metadata. This collection of data is the data store for the application. The API also provides the ability to create custom metadata objects and map relational table data to the metadata objects, and to create queries that use the data to which the custom objects are mapped.
Potentially, the data store includes all of the measure folders that were created by the database administrator in the OLAP Catalog. However, the scope of the data store that is visible when an application is running depends on the database privileges that apply to the user ID through which the connection was made. A user sees all of the measure folders (as MdmSchema
objects) that the database administrator or the application created, but the user sees the measures and dimensions that are contained in those measure folders only if he or she has access rights to the relational tables to which the measures and dimensions are mapped.
When the database administrator created the OLAP Catalog metadata, the administrator created measures, dimensions, and other OLAP metadata objects by mapping them to columns in database tables or views. In the OLAP API, these objects are accessed as multidimensional metadata (MDM) objects, as described in Chapter 2, " Understanding OLAP API Metadata".
The mapping between the OLAP metadata objects and the MDM objects is automatically performed by Oracle OLAP. You can, however, discover the mapping of the MDM objects by using classes in the oracle.olapi.metadata.mtm
package, as described in Chapter 5, " Working with Metadata Mapping Objects". You can also create your own custom MDM metadata objects and map them to database tables or views using MTM objects.
The metadata objects in the data store help your application to make sense of the data. They provide a way for you to find out what data is available, how it is structured, and what its characteristics are.
Therefore, after connecting, your first step is to find out what metadata is available. Armed with this knowledge, you can present choices to the end user about what data should be selected or calculated and how it should be displayed.
Before investigating the metadata, your application must make a connection to Oracle OLAP, as described in Chapter 3, " Connecting to a Data Store". Then, your application might perform the following steps:
Create an MdmMetadataProvider
.
Get the root MdmSchema
from the MdmMetadataProvider
.
Get the contents of the root MdmSchema
, which include MdmMeasure
, MdmDimension
, MdmMeasureDimension
, and MdmSchema
objects. In addition, get the contents of any subschemas.
Get the components or related objects of each MdmMeasure
and MdmDimension
. For example, get the MdmDimension
objects for each MdmMeasure
, and for each MdmDimension
get its MdmHierarchy
objects.
The next four topics in this chapter describe these steps in detail.
After your application discovers the metadata, it typically goes on to create queries for selecting, calculating, and otherwise manipulating the data. To work with data in these ways, you must get the Source
objects from the MDM objects. These Source
objects are referred to as primary Source
objects. Source
objects specify the data for querying.
This chapter focuses on the initial step of discovering the available metadata, but it also briefly mentions the step of getting a primary Source
from a metadata object. Subsequent chapters of this guide explain how you work with primary Source
objects and create queries based on them.
An MdmMetadataProvider
gives access to the metadata in a data store. It provides OLAP metadata objects, such as measures, dimensions, and measure folders, as corresponding MDM objects, such as MdmMeasure
, MdmDimension
, and MdmSchema
objects.
Before you can create an MdmMetadataProvider
, you must create a DataProvider
as described in Chapter 3, " Connecting to a Data Store". Example 4-1 creates an MdmMetadataProvider
. In the example, dp
is an ExpressDataProvider
.
Getting the root MdmSchema
is the first step in exploring the metadata in your data store.
The metadata objects that are accessible through an MdmMetadataProvider
are organized in a tree-like structure, with the root MdmSchema
at the top. Under the root MdmSchema
are MdmPrimaryDimension
objects and one or more MdmSchema
objects, which are referred to as subschemas. In addition, if an MdmMeasure
object does not belong to any subschema, then it is included under the root.
Subschemas have their own MdmMeasure
and MdmPrimaryDimension
objects. Optionally, they can have their own subschemas as well.
The root MdmSchema
contains all of the MdmPrimaryDimension
objects that are in the subschemas. Therefore, an MdmPrimaryDimension
typically appears twice in the tree. It appears once under the root MdmSchema
and again under the subschema. If an MdmPrimaryDimension
does not belong to a subschema, then it is listed only under the root.
The starting point for discovering the available metadata objects is the root MdmSchema
, which is the top of the tree. The following diagram illustrates an MdmSchema
that has two subschemas and four MdmPrimaryDimension
objects.
In the OLAP Catalog, a database administrator arranges dimensions and measures under one or more top-level measure folders. When Oracle OLAP maps the measure folders to MdmSchema
objects, it always creates the root MdmSchema
over the MdmSchema
objects for the top-level measure folders. Therefore, even if the database administrator creates only one measure folder, its corresponding MdmSchema
is a subschema under the root.
For more information about MDM metadata objects and how they map to OLAP metadata objects, see Chapter 2, " Understanding OLAP API Metadata".
The root MdmSchema
contains MdmPrimaryDimension
objects, MdmSchema
objects, and possibly MdmMeasure
objects. In addition, the root MdmSchema
has an MdmMeasureDimension
that has a List
of all of the MdmMeasure
objects.
The following code gets a List
of the MdmPrimaryDimension
objects that are in rootSchema
, which is the root MdmSchema
. The List
does not include the MdmMeasureDimension
.
The following code gets a List
of MdmSchema
objects that are in rootSchema
.
For each MdmSchema
that is under the root MdmSchema
, you can call its getMeasures
, getDimensions
, and getSubSchemas
methods. The procedures are the same as those for getting the contents of the root MdmSchema
.
Example 4-5 gets the MdmMeasureDimension
that is in the root MdmSchema
. Use this method only on the root MdmSchema
, because only the root MdmSchema
has the MdmMeasureDimension
. The example displays the names of the MdmMeasure
objects that are contained by the MdmMeasureDimension
.
Example 4-5 Getting the MdmMeasureDimension and Its Contents
MdmMeasureDimension mdmMeasureDim = (MdmMeasureDimension) rootSchema.getMeasureDimension(); List mdmMeasureDimMeasures = mdmMeasureDim.getMeasures(); Iterator mdmMeasureDimMeasuresItr = mdmMeasureDimMeasures.iterator(); MdmMeasure measure = null; System.out.println("The measures in the MdmMeasureDimension are:"); while (mdmMeasureDimMeasuresItr.hasNext()) { measure = (MdmMeasure) mdmMeasureDimMeasuresItr.next(); System.out.println("\t" + measure.getName()); }
Having discovered the list of MdmMeasure
and MdmDimension
objects, the next step in metadata discovery involves finding out the characteristics of those objects.
A primary characteristic of an MdmMeasure
is that it has related MdmPrimaryDimension
objects. Example 4-6 gets a List
of MdmPrimaryDimension
objects for mdmUnits
, which is an MdmMeasure
.
Example 4-6 Getting the Dimensions of an MdmMeasure
List dimsOfUnits = mdmUnits.getDimensions();
The getMeasureInfo
method, which is in the Example 4-9, shows one way to iterate through the MdmPrimaryDimension
objects belonging to an MdmMeasure
.
An MdmPrimaryDimension
has one or more component MdmHierarchy
objects, which you can obtain by calling its getHierarchies
method. That method returns a List
of MdmHierarchy
objects. If an MdmHierarchy
is an MdmLevelHierarchy
, then it has levels that you can obtain by calling its getLevels
method.
Example 4-7 demonstrates how you can get the MdmHierarchy
objects for an MdmPrimaryDimension
. The example displays the OLAP Catalog names of the MdmHierarchy
objects.
Example 4-7 Getting the MdmHierarchy Components of an MdmPrimaryDimenison
List mdmHiers = mdmPrimaryDim.getHierarchies(); Iterator mdmHiersItr = mdmHiers.iterator(); System.out.println("The MdmHierarchy components of " + mdmPrimaryDim.getName() + " are:"); while (mdmHiersItr.hasNext()) { MdmHierarchy mdmHier = (MdmHierarchy) mdmHiersItr.next(); System.out.println("\t" + mdmHier.getName()); }
The getDimInfo
method in Example 4-9 shows one way to get the following metadata objects for an MdmDimension
.
Its concrete class.
Its MdmHierarchy
objects.
Its default MdmHierarchy
object.
The MdmAttribute
objects returned by its getAttributes
method.
Its parent, ancestors, level, and level depth attributes.
Methods are also available for obtaining other MdmPrimaryDimension
characteristics. See the Oracle OLAP Java API Reference for descriptions of all of the methods of the MDM classes.
A metadata object represents a set of data, but it does not provide the ability to create queries on that data. Its function is informational, recording the existence, structure, and characteristics of the data. It does not give access to the data values.
In order to access the data values for a metadata object, an application gets the Source
object for that metadata object. A Source
for a metadata object is called a primary Source
.
To get the primary Source
for a metadata object, an application calls the getSource
method of that metadata object. For example, if an application needs to display the quantity of product units sold during the year 1999, then it must use the getSource
method of the MdmMeasure
for that data, which is mdmUnits
in the following example.
Example 4-8 Getting a Primary Source for a Metadata Object
Source units = mdmUnits.getSource();
For more information about getting and working with primary Source
objects, see Chapter 6, " Understanding Source Objects".
The sample code that follows is a simple Java program called SampleMetadataDiscoverer10g
. The program discovers the metadata objects that are under the root MdmSchema
of a data store. The output of the program lists the names and related objects for the MdmMeasure
and MdmDimension
objects in the root MdmSchema
and the MdmSchema
subschema for the Global relational schema.
After presenting the program code, this topic presents the output of the program when it is run against a data store that consists of the Global relational schema. In the OLAP metadata, the Global schema is represented as the GLOBAL_CAT
measure folder. Through an OLAP API connection, the GLOBAL_CAT
measure folder maps to an MdmSchema
whose name is GLOBAL_CAT
.
The program in Example 4-9 gets the OLAP Catalog metadata objects that map to the tables and views of the Global relational schema. It passes the command line arguments that specify the server on which the Oracle Database instance is running and a user name and password to the connectToDB
method of a MyConnection10g
object, which establishes a connection to the database.
The code for the MyConnection10g
is not shown here, but the procedure for connecting is described in Chapter 3, " Connecting to a Data Store". The complete code for the MyConnection10g
and the SampleMetadataDiscoverer10g
classes is available from the Overview of the Oracle OLAP Java API Reference.
Example 4-9 Discovering the OLAP Catalog Metadata
package globalExamples; import oracle.express.olapi.data.full.ExpressDataProvider; import oracle.olapi.metadata.mdm.*; import oracle.olapi.data.source.Source; import java.util.List; import java.util.Iterator; /** * Discovers the MDM metadata objects in the Global schema. * This file and the MyConnection10g.java file are available from the * Overview of the <I>Oracle OLAP Java API Reference</I>. */ public class SampleMetadataDiscoverer10g { /** * Constant to use to display less information about metadata objects. */ static final int TERSE = 0; /** * Constant to use to display more information about metadata objects. */ static final int VERBOSE = 1; private MdmSchema root = null; private MdmPrimaryDimension mdmDim = null; public SampleMetadataDiscoverer10g() { } /** * Creates an object that makes a connection to an Oracle database * and gets MDM metadata objects. */ public void run(String[] args) { // Connect through JDBC to an instance of an Oracle database // and get a DataProvider. MyConnection10g myConn = new MyConnection10g(); ExpressDataProvider dp = myConn.connectToDB(args, TERSE); // Get the default MdmMetadataProvider from the DataProvider. MdmMetadataProvider mp = null; try { mp = (MdmMetadataProvider) dp.getDefaultMetadataProvider(); } catch (Exception e) { System.out.println("Cannot create the MDM metadata provider." + e); } // Get metadata information about the root MdmSchema and its subschemas. try { root = mp.getRootSchema(); System.out.println("The root MdmSchema is " + root.getName() + ".\n"); getSchemaInfo(root, VERBOSE); } catch (Exception e) { System.out.println("Encountered exception. " + e); } // Get the Source for the dimension that was saved in getDimInfo. System.out.println("\nMaking a Source object for dimension " + mdmDim.getName() + "."); Source dimSource = mdmDim.getSource(); System.out.println("Made the Source."); // Close the ExpressDataProvider and the connection. dp.close(); System.out.println("\nClosed the DataProvider."); myConn.closeConnection(); System.out.println("Closed the connection."); } /** * Gets information about an MdmSchema. */ public void getSchemaInfo(MdmSchema schema, int outputStyle) { if (schema == root) { System.out.println("The MdmPrimaryDimension components of" + "the root schema are:"); } else { System.out.println(" The MdmPrimaryDimension components of schema " + schema.getName() + " are:"); } // Get the dimension information for the MdmSchema. MdmPrimaryDimension oneDim = null; int i = 1; try { List dims = schema.getDimensions(); Iterator dimIter = dims.iterator(); // Save the first dimension to use later for getting its Source. mdmDim = (MdmPrimaryDimension) dims.get(0); // Iterate through the list of MdmPrimaryDimension objects and get // information about each one. while (dimIter.hasNext()) { oneDim = (MdmPrimaryDimension) dimIter.next(); getDimInfo(i, oneDim, outputStyle); i++; } } catch (Exception e) { System.out.println(" Encountered exception. " + e); } // If the MdmSchema is the root MdmSchema, get the // MdmMeasureDimension amd get its measures. MdmMeasure oneMeasure = null; MdmMeasureDimension mdmMeasureDim = (MdmMeasureDimension) schema.getMeasureDimension(); if (mdmMeasureDim != null) { System.out.println("The measures of the MdmMeasureDimension are:"); List mdmMeasures = mdmMeasureDim.getMeasures(); Iterator mdmMeasuresIter = mdmMeasures.iterator(); while (mdmMeasuresIter.hasNext()) { oneMeasure = (MdmMeasure) mdmMeasuresIter.next(); getMeasureInfo(oneMeasure, outputStyle); System.out.println(" "); } } // Get the measures from the MdmSchema. try { List mdmMeasures = schema.getMeasures(); if (mdmMeasures.size() > 0) { Iterator mdmMeasuresIter = mdmMeasures.iterator(); System.out.println("\n The measures of schema " + schema.getName() + " are:"); while (mdmMeasuresIter.hasNext()) { oneMeasure = (MdmMeasure) mdmMeasuresIter.next(); getMeasureInfo(oneMeasure, outputStyle); } } } catch (Exception e) { System.out.println(" Encountered exception. " + e); } // Get the subschema information for the MdmSchema. MdmSchema oneSchema = null; try { List subSchemas = schema.getSubSchemas(); Iterator subSchemaIter = subSchemas.iterator(); while (subSchemaIter.hasNext()) { oneSchema = (MdmSchema) subSchemaIter.next(); // To get information on subschemas other than the Global // schema, GLOBAL_CAT, remove the if condition and call // the getSchemaInfo method. if (oneSchema.getName().equals("GLOBAL_CAT")) getSchemaInfo(oneSchema, TERSE); } } catch (Exception e) { System.out.println(" Encountered exception. " + e); } } /** * Gets information about an MdmMeasure. */ public void getMeasureInfo(MdmMeasure measure, int outputStyle) { System.out.println(" " + measure.getName()); if (outputStyle == VERBOSE) { // Get the dimensions of the MdmMeasure. try { List mDims = measure.getDimensions(); Iterator mDimIter = mDims.iterator(); System.out.println(" Its dimensions are: "); while (mDimIter.hasNext()) { System.out.println(" " + ((MdmDimension) mDimIter.next()).getName()); } } catch (Exception e) { System.out.println(" Encountered exception. " + e); } } } /** * Gets information about an MdmDimension. */ public void getDimInfo(int count, MdmPrimaryDimension dim, int outputStyle) { if (outputStyle == TERSE) System.out.println(" " + dim.getName()); else if (outputStyle == VERBOSE) { System.out.println(count + ": MdmPrimaryDimension Name: " + dim.getName()); String description = dim.getDescription(); if (description.length() > 0) System.out.println(" Description: " + dim.getDescription()); // Determine the type of the MdmPrimaryDimension. try { if (dim instanceof MdmStandardDimension) { System.out.println(" It is an MdmStandardDimension."); } else if (dim instanceof MdmTimeDimension) { System.out.println(" It is an an MdmTimeDimension."); } else if (dim instanceof MdmMeasureDimension) { System.out.println(" It is an MdmMeasureDimension."); } } catch (Exception e) { System.out.println(" Encountered exception. " + e); } // Get the attributes of the MdmPrimaryDimension System.out.println(" Its attributes are:"); try { List attributes = dim.getAttributes(); Iterator attrIter = attributes.iterator(); while (attrIter.hasNext()) { System.out.println(" Attribute: " + ((MdmAttribute) attrIter.next()).getName()); } } catch (Exception e) { System.out.println(" Encountered exception. " + e); } // Get the hierarchies of the MdmPrimaryDimension getHierInfo(dim); System.out.println(" "); } } /** * Gets the MdmHierarchy components of an MdmPrimaryDimension. */ public void getHierInfo(MdmPrimaryDimension dim) { List mdmHiers = dim.getHierarchies(); Iterator mdmHiersItr = mdmHiers.iterator(); MdmHierarchy mdmHier = null; MdmLevelHierarchy mdmLevelHier = null; boolean isLevelHierarchy = false; int i = 1; System.out.println(" The MdmHierarchy components of " + dim.getName() + " are:"); while (mdmHiersItr.hasNext()) { mdmHier = (MdmHierarchy) mdmHiersItr.next(); System.out.println(" "+ i + ": " + mdmHier.getName()); if (mdmHier.isDefaultHierarchy()) { System.out.println(" "+ i + ": " + mdmHier.getName()); " is the default MdmHierarchy of " + dim.getName() + "."); } if (mdmHier instanceof MdmLevelHierarchy) { mdmLevelHier = (MdmLevelHierarchy) mdmHier; System.out.println(" It is an MdmLevelHierarchy."); isLevelHierarchy = true; } else if (mdmHier instanceof MdmValueHierarchy) { System.out.println(" It is an MdmValueHierarchy"); } System.out.println(" Its attributes are:"); if (isLevelHierarchy) { System.out.println(" Level attribute: " + mdmLevelHier.getLevelAttribute().getName()); System.out.println(" Level depth attribute: " + mdmLevelHier.getLevelDepthAttribute().getName()); } System.out.println(" Parent attribute: " + mdmHier.getParentAttribute().getName()); System.out.println(" Ancestors attribute: " + mdmHier.getAncestorsAttribute().getName()); if (isLevelHierarchy) getLevelInfo(mdmLevelHier); i++; } } /** * Gets the MdmLevel components of an MdmLevelHierarchy. */ public void getLevelInfo(MdmLevelHierarchy mdmHier) { List mdmLevels = mdmHier.getLevels(); Iterator mdmLevelsItr = mdmLevels.iterator(); System.out.println(" Its levels are:"); while (mdmLevelsItr.hasNext()) { MdmLevel mdmLevel = (MdmLevel) mdmLevelsItr.next(); System.out.println(" " + mdmLevel.getName()); } } /** * Creates a new SampleMetadataDiscoverer10g object and calls its * run method. */ public static void main(String[] args) { new SampleMetadataDiscoverer().run(args); } }
The output from the sample program consists of text lines produced by Java statements such as the following one.
System.out.println("The root MdmSchema is " + root.getName() + ".\n");
When the program is run on the Global schema, the output includes the following items:
The name of the root MdmSchema
, which is ROOT
.
The names and other information for the MdmPrimaryDimension
objects in the root MdmSchema
.
The measures in the MdmMeasureDimension
.
The dimensions and measures of the GLOBAL_CAT
MdmSchema
.
Because the GLOBAL_CAT
MdmSchema
is the only subschema under the root MdmSchema
, its MdmPrimaryDimension
objects are identical to those in the root.
Two lines that indicate that the code got the primary Source
for an MdmPrimaryDimension
.
Here is the output. In order to conserve space, some blank lines have been omitted.
The root MdmSchema is ROOT. The MdmPrimaryDimension components of the root schema are: 1: MdmPrimaryDimension Name: CHANNEL It is an MdmStandardDimension. Its attributes are: Attribute: Long_Description Attribute: Short_Description The MdmHierarchy components of CHANNEL are: 1: CHANNEL_ROLLUP CHANNEL_ROLLUP is the default MdmHierarchy of CHANNEL. It is an MdmLevelHierarchy. Its attributes are: Level attribute: D_GLOBAL.CHANNELLEVEL_ATTRIBUTE Level depth attribute: D_GLOBAL.CHANNELLEVELDEPTH_ATTRIBUTE Parent attribute: D_GLOBAL.CHANNELPARENT_ATTRIBUTE Ancestors attribute: D_GLOBAL.CHANNELANCESTORS_ATTRIBUTE Its levels are: ALL_CHANNELS CHANNEL 2: MdmPrimaryDimension Name: CUSTOMER It is an MdmStandardDimension. Its attributes are: Attribute: Long_Description Attribute: Short_Description The MdmHierarchy components of CUSTOMER are: 1: MARKET_ROLLUP It is an MdmLevelHierarchy. Its attributes are: Level attribute: D_GLOBAL.CUSTOMERLEVEL_ATTRIBUTE Level depth attribute: D_GLOBAL.CUSTOMERLEVELDEPTH_ATTRIBUTE Parent attribute: D_GLOBAL.CUSTOMERPARENT_ATTRIBUTE Ancestors attribute: D_GLOBAL.CUSTOMERANCESTORS_ATTRIBUTE Its levels are: TOTAL_MARKET MARKET_SEGMENT ACCOUNT SHIP_TO 2: SHIPMENTS_ROLLUP SHIPMENTS_ROLLUP is the default MdmHierarchy of CUSTOMER. It is an MdmLevelHierarchy. Its attributes are: Level attribute: D_GLOBAL.CUSTOMERLEVEL_ATTRIBUTE Level depth attribute: D_GLOBAL.CUSTOMERLEVELDEPTH_ATTRIBUTE Parent attribute: D_GLOBAL.CUSTOMERPARENT_ATTRIBUTE Ancestors attribute: D_GLOBAL.CUSTOMERANCESTORS_ATTRIBUTE Its levels are: ALL_CUSTOMERS REGION WAREHOUSE SHIP_TO 3: MdmPrimaryDimension Name: PRODUCT It is an MdmStandardDimension. Its attributes are: Attribute: Long_Description Attribute: Package Attribute: Short_Description The MdmHierarchy components of PRODUCT are: 1: PRODUCT_ROLLUP PRODUCT_ROLLUP is the default MdmHierarchy of PRODUCT. It is an MdmLevelHierarchy. Its attributes are: Level attribute: D_GLOBAL.PRODUCTLEVEL_ATTRIBUTE Level depth attribute: D_GLOBAL.PRODUCTLEVELDEPTH_ATTRIBUTE Parent attribute: D_GLOBAL.PRODUCTPARENT_ATTRIBUTE Ancestors attribute: D_GLOBAL.PRODUCTANCESTORS_ATTRIBUTE Its levels are: TOTAL_PRODUCT CLASS FAMILY ITEM 4: MdmPrimaryDimension Name: TIME It is an an MdmTimeDimension. Its attributes are: Attribute: End_Date Attribute: Long_Description Attribute: Short_Description Attribute: Time_Span The MdmHierarchy components of TIME are: 1: CALENDAR CALENDAR is the default MdmHierarchy of TIME. It is an MdmLevelHierarchy. Its attributes are: Level attribute: D_GLOBAL.TIMELEVEL_ATTRIBUTE Level depth attribute: D_GLOBAL.TIMELEVELDEPTH_ATTRIBUTE Parent attribute: D_GLOBAL.TIMEPARENT_ATTRIBUTE Ancestors attribute: D_GLOBAL.TIMEANCESTORS_ATTRIBUTE Its levels are: YEAR QUARTER MONTH The measures of the MdmMeasureDimension are: UNIT_COST Its dimensions are: PRODUCT TIME UNIT_PRICE Its dimensions are: PRODUCT TIME UNITS Its dimensions are: CHANNEL CUSTOMER PRODUCT TIME Schema: GLOBAL_CAT The MdmPrimaryDimension components of schema GLOBAL_CAT are: CHANNEL CUSTOMER PRODUCT TIME The measures of schema GLOBAL_CAT are: UNITS UNIT_COST UNIT_PRICE Making a Source object for dimension CHANNEL. Made the Source Closed the DataProvider. Closed the connection.