以前一直是用MS Anylize Service的,最近要做的項(xiàng)目是java的,小項(xiàng)目預(yù)算有限,所以想找一個(gè)開(kāi)源的java的數(shù)據(jù)倉(cāng)庫(kù)解決方案來(lái)用用。
在網(wǎng)上查了一下,發(fā)現(xiàn)了Mondrian。Mondrian是基于JAVA的數(shù)據(jù)倉(cāng)庫(kù)引擎,可以集成到web項(xiàng)目中,這一點(diǎn)最吸引我。另外與他搭配的表現(xiàn)層的方案也有不少選擇,Jpivot是元老,pentaho,openi看起來(lái)是后起之秀。不管怎樣,還是先研究一下modrian吧
網(wǎng)上的中文資源比較少,在csdn上找了一下,只發(fā)現(xiàn)了兩篇比較有用的
http://dev.csdn.net/develop/article/31/31791.shtm Mondrian——有影響的“藝術(shù)家” 選擇自 kswaking 的 Blog
http://dev.csdn.net/develop/article/68/68661.shtm 窮人的通用OLAP方案III--JPivot表現(xiàn)層 選擇自 calvinxiu 的 Blog
照著做了一下,發(fā)現(xiàn)了一些問(wèn)題,也有了一些心得。
一.環(huán)境準(zhǔn)備
1.1 首先介紹一下環(huán)境
操作系統(tǒng):Linux
服務(wù)器:Tomcat 5.5
數(shù)據(jù)庫(kù):MySQL 5.0.21
1.2 下載程序。Mondrian在http://mondrian.sourceforge.net 可以下載,最早他是用MS Analyze Service的教程中FoodMart數(shù)據(jù)庫(kù)作為demo的,那個(gè)是access的數(shù)據(jù)庫(kù)。還好現(xiàn)在他有了Platform-Independent的版本,我就下載了那個(gè)mondrian-2.1.1-derby.zip 解壓縮之后在lib目錄里面有一個(gè)mondrian-embedded.war,把這個(gè)直接放到tomcat的webapps目錄里面就能夠看到mondrian的demo了。不過(guò)后面的測(cè)試,我把這個(gè)war解開(kāi)之后放到webapps里面去,并且目錄把名字改短了點(diǎn)mondrian。啟動(dòng)tomcat,在瀏覽器輸入http://localhost/mondiran 看到了demo。需要說(shuō)明一下的是,mondrian的發(fā)布包含了Jpivot,用它來(lái)做展示層,所以不用再去單獨(dú)下載Jpivot了。
1.3 數(shù)據(jù)庫(kù)建表,在MySQL數(shù)據(jù)庫(kù)里面建立table,借用了kswaking的數(shù)據(jù)庫(kù)結(jié)構(gòu)
在這個(gè)tiny的系統(tǒng)中,數(shù)據(jù)庫(kù)有3個(gè)表tb_employee(職員表),tb_time(時(shí)間表),tb_salary(薪酬表)。表結(jié)構(gòu)如下:
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, --時(shí)間id
the_year char(4), --年
the_month char(2) --月
);
drop table tb_salary;
create table tb_salary
(
employee_id number, --職員id
time_id number, --時(shí)間id
salary number(19,4) --薪酬
);
當(dāng)然,為了使系統(tǒng)能夠運(yùn)行,還需要讀者向數(shù)據(jù)庫(kù)表中插入一些數(shù)據(jù)。
二. mondrian測(cè)試
需要說(shuō)明的是mondrian使用了MS一樣的MDX語(yǔ)言實(shí)現(xiàn)查詢(xún),這對(duì)于從MS Analyze Services入門(mén)的人真是一個(gè)好消息。
2.1 先編寫(xiě)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>
這個(gè)schema定義了一個(gè)cube,包含兩個(gè)Dimension和一個(gè)Measure。很容易看懂,就不解釋了。
文件路徑為webapps/mondrian/WEB-INF/queries/mondriantest.xml。
為了后面的測(cè)試方便,我把文件放到了queries目錄里面。
因?yàn)橛肕ySQL建表的時(shí)候都用小寫(xiě)的,所以schema里面的字段名也都用了小寫(xiě)(我一開(kāi)始也使用大寫(xiě)的,結(jié)果出錯(cuò),找不到字段),calvinxiu的文章說(shuō)如果是Oracle數(shù)據(jù)庫(kù),這里的字段要用大寫(xiě)。
2.2 編寫(xiě)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來(lái)連接數(shù)據(jù)庫(kù)的,其中要特別注意的是Catalog指名了schema的位置。
文件路徑webapps/mondrian/mondriantestmdx.jsp
2.3 測(cè)試
在瀏覽器輸入http://localhost/mondrian/mondriantestmdx.jsp 可以看到顯示的結(jié)果 get result,說(shuō)明一切正常。
到目前為止,我們只測(cè)試了Mondrian,它只負(fù)責(zé)數(shù)據(jù)的提取和組織,所以在畫(huà)面上沒(méi)有看到任何的數(shù)據(jù),下一篇文章將繼續(xù)研究數(shù)據(jù)的展現(xiàn) - Jpivot。