前期准备工作:
已经安装好eclipse+MySQL环境,下载了Mondrian的带源码的压缩包。
具体步骤:
1.新建一个Java工程;
2.将Mondrian压缩包解压,并找到lib文件下的mondrian.war文件,也对其解压;
3.你会发现在.war解压文件下的WEB-INF/lib文件夹下有很多jar包(我的目录路径是/home/hadoop/Desktop/mondrian-3.5.0/lib/mondrian.war_FILES/WEB-INF/lib),将其全部
通过build path加进我们的工程中;将mysql的jdbc驱动jar包也加进来(mysql-connector-java-5.0.8-bin.jar);
4.将Mondrian解压包里demo文件夹下的FoodMart.xml文件copy到Java工程的根目录下;
5.在src下新建一个class类(com.neu.qiu.mondrian.ParseResult),代码如下:
package com.neu.qiu.mondrian;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import mondrian.olap.Connection;
import mondrian.olap.DriverManager;
import mondrian.olap.Query;
import mondrian.olap.Result;
public class ParseResult {
/**
* @param args
*/
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
Connection connection = DriverManager.getConnection(
"Provider=mondrian;"+
"Jdbc=jdbc:mysql://localhost:3306/foodmart; JdbcUser=hive;"+
"JdbcPassword=123456;"+
"Catalog=file:///home/hadoop/workspace/mondrian/FoodMart.xml;"+
"JdbcDriver=com.mysql.jdbc.Driver", null);
Query query = connection.parseQuery(
"select {[Measures].[Unit Sales],[Measures].[Store Cost],[Measures].[Store Sales]} on columns,"
+"{([Promotion Media].[All Media],[Product].[All Products])} on rows "
+"from Sales "
+"where ([Time].[1997])");
@SuppressWarnings("deprecation")
Result result = connection.execute(query);
PrintWriter pw = new PrintWriter(System.out);
result.print(pw);
pw.flush();
}
}
运行后,结果为:
log4j:WARN No appenders could be found for logger (mondrian.olap.MondrianProperties).
log4j:WARN Please initialize the log4j system properly.
Axis #0:
{[Time].[1997]}
Axis #1:
{[Measures].[Unit Sales]}
{[Measures].[Store Cost]}
{[Measures].[Store Sales]}
Axis #2:
{[Promotion Media].[All Media], [Product].[All Products]}
Row #0: 266,773
Row #0: 225,627.23
Row #0: 565,238.13
前两行警告可以忽视。
一个小例子测试完毕。