1. 程式人生 > >在Java Web專案中引入Mondrian多維分析框架

在Java Web專案中引入Mondrian多維分析框架

本文完全轉載自:http://www.linuxidc.com/Linux/2014-09/107008.htm

我也是初學,為了方便以後檢視,所以轉到自己部落格上了。

----------------------------------------------------------------------------------

一,Mondrian簡介

Mondrian是一個開源專案,一個用Java寫成的OLAP引擎。它用MDX語言實現查詢,從關係資料庫(RDBMS)中讀取資料。然後經過Java API以多維的方式對結果進行展示。

Mondrian的使用方式同JDBC驅動類似。可以非常方便的與現有的Web專案整合。

Mondrian OLAP 系統由四個層組成,可分為從終端使用者到資料中心, 順序為:

                  1表現層(the presentation layer)

                  2維度層(the dimensional layer)

                  3集合層(the star layer)

                  4儲存層(the storage layer)

表現層以多維"語法(grammar)(維、度量、單元)”的形式發出查詢,然後OLAP伺服器返回結果。

維度層用來解析、驗證和執行MDX查詢要求。為了提高效率,維度層把要求查詢的單元成批發送到集合層,查詢轉換器接受操作現有查詢的請求,而不是對每個請求都建立一個MDX 宣告。

集合層負責維護和建立集合快取,一個集合是在記憶體中快取一組單元值,這些單元值由一組維的值來確定。

儲存層是一個關係型資料庫(RDBMS)。它負責建立集合的單元資料,和提供維表的成員。

維度層對這些單元發出查詢請求,如果所查詢的單元值不在快取中,則集合管理器(aggregation manager)會向儲存層發出查詢請求。

Mondrian應用在java web專案中的目錄結構:

在Java Web專案中引入Mondrian多維分析框架

webContent目錄下的jpivot、wcf這兩個資料夾包含mondrian展示查詢結果圖表時需要使用的影象和css檔案。

Busy.jsp顯示伺服器忙碌等待頁面、error.jsp顯示出錯頁面、testpage.jsp檔案呼叫了jpivot標籤庫來處理MDX查詢請求,並將最終結果以html的格式返回給使用者。

可以到http://sourceforge.net/projects/mondrian/下載Mondrian的專案包檔案。

Java 9 的新特性發布 http://www.linuxidc.com/Linux/2014-08/105707.htm

Java程式設計思想(第4版) 中文清晰PDF完整版 http://www.linuxidc.com/Linux/2014-08/105403.htm

編寫高質量程式碼 改善Java程式的151個建議 PDF高清完整版 http://www.linuxidc.com/Linux/2014-06/103388.htm

Java 8簡明教程 http://www.linuxidc.com/Linux/2014-03/98754.htm

Java物件初始化順序的簡單驗證 http://www.linuxidc.com/Linux/2014-02/96220.htm

Java物件值傳遞和物件傳遞的總結 http://www.linuxidc.com/Linux/2012-12/76692.htm

二,java Web專案中引入mondrian的幾個步驟

(1)在java web專案中引入相應的包。

下載mondrian專案包後解壓,其中的lib目錄下有一個可直接部署的例項mondrian.war。我們需要將war包解壓,然後從中取出我們所需要的檔案:jpivot、wcf二個資料夾及busy.jsp、error.jsp、testpage.jsp三個檔案,放到web專案的webContent目錄下。

jpivot、wcf這兩個資料夾包含mondrian展示查詢結果圖表時需要使用的影象和css檔案。

jpivot其實是一個自定義jsp的標籤庫,它基於XML/XSLT配置來生成相應的html。

(2)新增配置檔案。

在上述war包的解壓目錄下的WEB-INF目錄下選中jpivot、lib、wcf這三個資料夾,複製到web專案到WEB_INF目錄下。Jpivot、wcf這兩個資料夾包含jpivot和wcf用於生成使用者介面的配置檔案(*.xml、*.xsl)及標籤檔案(*.tld)的定義。lib資料夾包含的是mondrian所要用的jar包。

(3)配置web.xml檔案,使web專案能使用mondrian的功能。

可以直接從mondrian.war包中解壓得到的專案檔案中的WEB_INF路徑下複製web.xml檔案到我們到專案中,在此基礎上進行修改,主要是修改資料庫連線字串connectString的值,並新增專案需要的自定義的Servlet。

(4)在java web 專案的WEB_INF路徑下新建queries資料夾,在其中建立模式(schema)檔案(一個自定義的xml檔案)。

一個模式定義了一個多維資料庫。模式檔案中包含模式名稱Schema、資料立方名稱Cube、資料立方對應的事實表名稱Table、資料立方中的維度Dimension、維度中包含的資料層次Hierarchy、層次中維度對應的資料庫維表名稱Table和層次結構中包含的級別Level。

另外就是模式中包含的統計度量因素Measure(需要度量的指標及其計算方法)

Mondrian實現多維資料分析時,需要一個多維資料的邏輯模型(logical Schema model)、一組資料立方(consisting of Cube)、資料維度(Dimension) 、層次(Hierarchy)、和層級成員(Level members), 並對映到物理模型(關係資料庫)上。

簡單的說,配置一個模式就是配置一個關係資料結構到多維資料結構的對映。

在模式檔案定義完成之後,我們就可以根據它來編寫相應MDX查詢語句了。

模式檔案的例項:

<?xml version="1.0" encoding="UTF-8"?>

<Schema name="tezz">

<Cube name="Sales">

<!-- 事實表(fact table) -->

<Table name="sale" />

<!-- 客戶維 -->

<Dimension name="客戶性別" foreignKey="cusId">

<Hierarchy hasAll="true" allMemberName="所有性別" primaryKey="cusId">

<Table name="Customer"></Table>

<Level name="gender" column="gender"></Level>

</Hierarchy>

</Dimension>

<!-- 產品類別維 -->

<Dimension name="產品類別" foreignKey="proId">

<Hierarchy hasAll="true" allMemberName="所有產品" primaryKey="proId" primaryKeyTable="product">

<join leftKey="proTypeId" rightKey="proTypeId">

<Table name="product" />

<Table name="producttype"></Table>

</join>

<Level name="proTypeId" column="proTypeId"

nameColumn="proTypeName" uniqueMembers="true" table="producttype" />

<Level name="proId" column="proId" nameColumn="proName"

uniqueMembers="true" table="product" />

</Hierarchy>

</Dimension>

<Measure name="數量" column="number" aggregator="sum" datatype="Numeric" />

<Measure name="總銷售額" aggregator="sum" formatString="¥#,##0.00">

<!-- unitPrice*number所得值的列 -->

<MeasureExpression>

<SQL dialect="generic">(unitPrice*number)</SQL>

</MeasureExpression>

</Measure>

<CalculatedMember name="平均單價" dimension="Measures">

<Formula>[Measures].[總銷售額] / [Measures].[數量]</Formula>

<CalculatedMemberProperty name="FORMAT_STRING" value="¥#,##0.00" />

</CalculatedMember>

</Cube>

</Schema>

(5)在模式檔案Schema基礎上編輯用於查詢操作的jsp檔案,使用MDX語法規則。

在queries目錄下新建.jsp檔案,jsp檔案中使用MDX語法編輯mondrian Query,對資料立方Cube進行查詢,從不同的維度上進行select查詢取值。

jsp檔案中的查詢部分如下,包含在標籤<jp:mondrianQuery>中。 配置使用的模式檔案和資料庫,並編輯MDX查詢語句。

 

<jp:mondrianQuery id="query01"

    catalogUri="/WEB-INF/queries/tezz.xml" 

    jdbcDriver="com.mysql.jdbc.Driver"

    jdbcUrl="jdbc:mysql://localhost:3306/zett?user=root&#38;password=root">

select {[Measures].[數量],[Measures].[平均單價],[Measures].[總銷售額]} ON columns,

  {([產品類別].[所有產品],[客戶性別].[所有性別])} ON rows

from [Sales]

</jp:mondrianQuery>

(6)部署專案,執行查詢。

在tomcat中部署java web專案後,在瀏覽器中輸入:

…./projectname/testpage.jsp?query=xxx,瀏覽查詢結果。其中xxx就是queries中的jsp查詢檔案。testpage.jsp呼叫xxx頁面中的查詢,並將返回的結果組織到對應的表格中,進行展示。

效果圖如下,表格比較樸素: 

(4)在java web 專案的WEB_INF路徑下新建queries資料夾,在其中建立模式(schema)檔案(一個自定義的xml檔案)。

一個模式定義了一個多維資料庫。模式檔案中包含模式名稱Schema、資料立方名稱Cube、資料立方對應的事實表名稱Table、資料立方中的維度Dimension、維度中包含的資料層次Hierarchy、層次中維度對應的資料庫維表名稱Table和層次結構中包含的級別Level。

另外就是模式中包含的統計度量因素Measure(需要度量的指標及其計算方法)

Mondrian實現多維資料分析時,需要一個多維資料的邏輯模型(logical Schema model)、一組資料立方(consisting of Cube)、資料維度(Dimension) 、層次(Hierarchy)、和層級成員(Level members), 並對映到物理模型(關係資料庫)上。

簡單的說,配置一個模式就是配置一個關係資料結構到多維資料結構的對映。

在模式檔案定義完成之後,我們就可以根據它來編寫相應MDX查詢語句了。

模式檔案的例項:

<?xml version="1.0" encoding="UTF-8"?>

<Schema name="tezz">

<Cube name="Sales">

<!-- 事實表(fact table) -->

<Table name="sale" />

<!-- 客戶維 -->

<Dimension name="客戶性別" foreignKey="cusId">

<Hierarchy hasAll="true" allMemberName="所有性別" primaryKey="cusId">

<Table name="Customer"></Table>

<Level name="gender" column="gender"></Level>

</Hierarchy>

</Dimension>

<!-- 產品類別維 -->

<Dimension name="產品類別" foreignKey="proId">

<Hierarchy hasAll="true" allMemberName="所有產品" primaryKey="proId" primaryKeyTable="product">

<join leftKey="proTypeId" rightKey="proTypeId">

<Table name="product" />

<Table name="producttype"></Table>

</join>

<Level name="proTypeId" column="proTypeId"

nameColumn="proTypeName" uniqueMembers="true" table="producttype" />

<Level name="proId" column="proId" nameColumn="proName"

uniqueMembers="true" table="product" />

</Hierarchy>

</Dimension>

<Measure name="數量" column="number" aggregator="sum" datatype="Numeric" />

<Measure name="總銷售額" aggregator="sum" formatString="¥#,##0.00">

<!-- unitPrice*number所得值的列 -->

<MeasureExpression>

<SQL dialect="generic">(unitPrice*number)</SQL>

</MeasureExpression>

</Measure>

<CalculatedMember name="平均單價" dimension="Measures">

<Formula>[Measures].[總銷售額] / [Measures].[數量]</Formula>

<CalculatedMemberProperty name="FORMAT_STRING" value="¥#,##0.00" />

</CalculatedMember>

</Cube>

</Schema>

(5)在模式檔案Schema基礎上編輯用於查詢操作的jsp檔案,使用MDX語法規則。

在queries目錄下新建.jsp檔案,jsp檔案中使用MDX語法編輯mondrian Query,對資料立方Cube進行查詢,從不同的維度上進行select查詢取值。

jsp檔案中的查詢部分如下,包含在標籤<jp:mondrianQuery>中。 配置使用的模式檔案和資料庫,並編輯MDX查詢語句。

 

<jp:mondrianQuery id="query01"

    catalogUri="/WEB-INF/queries/tezz.xml" 

    jdbcDriver="com.mysql.jdbc.Driver"

    jdbcUrl="jdbc:mysql://localhost:3306/zett?user=root&#38;password=root">

select {[Measures].[數量],[Measures].[平均單價],[Measures].[總銷售額]} ON columns,

  {([產品類別].[所有產品],[客戶性別].[所有性別])} ON rows

from [Sales]

</jp:mondrianQuery>

(6)部署專案,執行查詢。

在tomcat中部署java web專案後,在瀏覽器中輸入:

…./projectname/testpage.jsp?query=xxx,瀏覽查詢結果。其中xxx就是queries中的jsp查詢檔案。testpage.jsp呼叫xxx頁面中的查詢,並將返回的結果組織到對應的表格中,進行展示。

效果圖如下,表格比較樸素: