1. 程式人生 > >資料倉庫研究之二--mondrian入門

資料倉庫研究之二--mondrian入門

原文:http://blog.csdn.net/infowain/archive/2006/06/24/829074.aspx

以前一直是用MS Anylize Service的,最近要做的專案是java的,小專案預算有限,所以想找一個開源的java的資料倉庫解決方案來用用。

  在網上查了一下,發現了Mondrian。Mondrian是基於JAVA的資料倉庫引擎,可以整合到web專案中,這一點最吸引我。另外與他搭配的表現層的方案也有不少選擇,Jpivot是元老,pentaho,openi看起來是後起之秀。不管怎樣,還是先研究一下modrian吧

  網上的中文資源比較少,在csdn上找了一下,只發現了兩篇比較有用的

http://dev.csdn.net/develop/article/31/31791.shtm   Mondrian——有影響的“藝術家”     選擇自 kswaking 的 Blog

http://dev.csdn.net/develop/article/68/68661.shtm  窮人的通用OLAP方案III--JPivot表現層     選擇自 calvinxiu 的 Blog

照著做了一下,發現了一些問題,也有了一些心得。

一.環境準備

1.1 首先介紹一下環境

作業系統:Linux

伺服器:Tomcat 5.5

資料庫:MySQL 5.0.21

1.2 下載程式。Mondrian在http://mondrian.sourceforge.net 可以下載,最早他是用MS Analyze Service的教程中FoodMart資料庫作為demo的,那個是access的資料庫。還好現在他有了Platform-Independent的版本,我就下載了那個mondrian-2.1.1-derby.zip 解壓縮之後在lib目錄裡面有一個mondrian-embedded.war,把這個直接放到tomcat的webapps目錄裡面就能夠看到mondrian的demo了。不過後面的測試,我把這個war解開之後放到webapps裡面去,並且目錄把名字改短了點mondrian。啟動tomcat,在瀏覽器輸入

http://localhost/mondiran 看到了demo。需要說明一下的是,mondrian的釋出包含了Jpivot,用它來做展示層,所以不用再去單獨下載Jpivot了。

1.3 資料庫建表,在MySQL資料庫裡面建立table,借用了kswaking的資料庫結構

在這個tiny的系統中,資料庫有3個表tb_employee(職員表),tb_time(時間表),tb_salary(薪酬表)。表結構如下:

drop table tb_employee;

create table tb_employee

(

     employee_id     number,             --職員id    

     employee_name   varchar2(10)        --職員姓名

);

 

drop table tb_time;

create table tb_time

(

    time_id   number,        --時間id

    the_year  char(4),       --

    the_month char(2)        --

);

 

drop table tb_salary;

create  table tb_salary

(

    employee_id  number,                --職員id   

    time_id      number,                --時間id

    salary       number(19,4)           --薪酬

);

 

當然,為了使系統能夠執行,還需要讀者向資料庫表中插入一些資料。

二. mondrian測試

  需要說明的是mondrian使用了MS一樣的MDX語言實現查詢,這對於從MS Analyze Services入門的人真是一個好訊息。

2.1 先編寫schema。

<?xml version="1.0"?>
  <Schema name="Mondrian">
    <Cube name="CubeTest">
    <Table name="tb_salary"/>

    <Dimension name="Employee" foreignKey="employee_id">
      <Hierarchy hasAll="true" primaryKey="employee_id">
        <Table name="tb_employee"/>
        <Level name="employeeID" column="employee_id" uniqueMembers="true">
           <Property name="employeeName" column="employee_name"/>
       </Level>
       </Hierarchy>
    </Dimension>

    <Dimension name="Time" foreignKey="time_id">
      <Hierarchy hasAll="false" primaryKey="time_id">
        <Table name="tb_time"/>
        <Level name="year" column="the_year" uniqueMembers="false"/> 
        <Level name="month" column="the_month" uniqueMembers="false"/>
      </Hierarchy>
    </Dimension>

    <Measure name="Salary" column="salary" aggregator="sum"/>

  </Cube>
</Schema>

這個schema定義了一個cube,包含兩個Dimension和一個Measure。很容易看懂,就不解釋了。
檔案路徑為webapps/mondrian/WEB-INF/queries/mondriantest.xml。

為了後面的測試方便,我把檔案放到了queries目錄裡面。

因為用MySQL建表的時候都用小寫的,所以schema裡面的欄位名也都用了小寫(我一開始也使用大寫的,結果出錯,找不到欄位),calvinxiu的文章說如果是Oracle資料庫,這裡的欄位要用大寫。

 

 

2.2 編寫JSP

 

<%@ page import="mondrian.olap.*"%>
<%
  Connection connection = DriverManager.getConnection("Provider=mondrian; Jdbc=jdbc:mysql://localhost/mondrian; JdbcUser=root; JdbcPassword=; Catalog=file:///usr/local/apache-tomcat-5.5.12/webapps/mondrian/WEB-INF/queries/mondriantest.xml; JdbcDriver=com.mysql.jdbc.Driver", null, false);

  String querystr = " select {[Measures].[Salary]} ON COLUMNS, {[Employee].[employeeId].Members} ON ROWS from CubeTest ";

  Query query=connection.parseQuery(querystr);
  Result result = connection.execute(query);
  out.println("get result");
%>

可以看到mondrian也使用jdbc來連線資料庫的,其中要特別注意的是Catalog指名了schema的位置。

 

檔案路徑webapps/mondrian/mondriantestmdx.jsp

 

2.3 測試

在瀏覽器輸入http://localhost/mondrian/mondriantestmdx.jsp 可以看到顯示的結果 get result,說明一切正常。

 

 

到目前為止,我們只測試了Mondrian,它只負責資料的提取和組織,所以在畫面上沒有看到任何的資料,下一篇文章將繼續研究資料的展現 - Jpivot。