以前一直是用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。