用 Mondrian 建立 cube 示例
1 准备数据
分析一个访问日志的事实表,有三个维度,站点、 IP 地址、日期。事实表记录的
数据源为 mysql 数据库
1.1 创建数据库
DROP DATABASE IF EXISTS `accessinfo`;
CREATE DATABASE `accessinfo` /*!40100 DEFAULT CHARACTER SET latin1 */;
USE `accessinfo`;
1.2 创建维度表
DROP TABLE IF EXISTS `dim_ip`;
CREATE TABLE `dim_ip` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`dip` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
INSERT INTO `dim_ip` VALUES (1,'61.183.248.218');
INSERT INTO `dim_ip` VALUES (2,'61.144.207.115');
DROP TABLE IF EXISTS `dim_site`;
CREATE TABLE `dim_site` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`dSiteID` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1;
INSERT INTO `dim_site` VALUES (1,542);
INSERT INTO `dim_site` VALUES (2,548);
INSERT INTO `dim_site` VALUES (3,543);
INSERT INTO `dim_site` VALUES (4,552);
INSERT INTO `dim_site` VALUES (5,551);
INSERT INTO `dim_site` VALUES (6,549);
DROP TABLE IF EXISTS `dim_time`;
CREATE TABLE `dim_time` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`signinTime` varchar(10) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1;
INSERT INTO `dim_time` VALUES (1,'2008-11-15');
INSERT INTO `dim_time` VALUES (2,'2008-11-16');
INSERT INTO `dim_time` VALUES (3,'2008-11-17');
INSERT INTO `dim_time` VALUES (4,'2008-11-18');
INSERT INTO `dim_time` VALUES (5,'2008-11-19');
1.3 创建事实表
DROP TABLE IF EXISTS `fact_logs`;
CREATE TABLE `fact_logs` (
`fID` varchar(20) NOT NULL DEFAULT '',
`fSiteID` varchar(20) DEFAULT NULL,
`fTime` varchar(10) DEFAULT NULL,
`fIP` varchar(20) DEFAULT NULL,
`fCount` int(11) DEFAULT NULL,
PRIMARY KEY (`fID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `fact_logs` VALUES ('1','1','4','1',87);
INSERT INTO `fact_logs` VALUES ('10','2','5','2',14);
INSERT INTO `fact_logs` VALUES ('2','1','4','2',128);
INSERT INTO `fact_logs` VALUES ('3','3','4','1',5);
INSERT INTO `fact_logs` VALUES ('4','4','4','2',4);
INSERT INTO `fact_logs` VALUES ('5','5','4','2',5);
INSERT INTO `fact_logs` VALUES ('6','6','4','2',3);
INSERT INTO `fact_logs` VALUES ('7','2','4','2',4);
INSERT INTO `fact_logs` VALUES ('8','5','5','2',15);
INSERT INTO `fact_logs` VALUES ('9','6','5','2',13);
2 定义模型
利用的是 pentaho 的工具: psw-ce-3.2.2.14148\schema-workbench
( 1 ) Option->connection 配置好数据源
( 2 )创建模型文件 AccessInfo.xml
( 3 )新建 schema ,随便给个名字
( 4 )右击 add cube
( 5 )右击 add table
( 6 )右击 add measure
( 7 )右击 schema 然后 add dimension
双击这个 dimension ,设置所有层次的别名:
右击这个层次, add table ,并选择它对应的维度表:
右击层次添加至少一个 level :
同样的方法添加另外两个维度。
( 8 )在 cube 中引用以上维度
右击 cube , add dimension usage :
注意添加完引用后,需要再回到 dimension 处,修改 hierarchy 的 primaryKey :
3 添加数据源
在 datasource.xml 文件中添加如下内容:
<DataSource>
<DataSourceName>Provider=Mondrian;DataSource=AccessInfo;</DataSourceName>
<DataSourceDescription>AccessInfo Data Warehouse</DataSourceDescription>
<URL>http://localhost:8080/mondrian/xmla</URL>
<DataSourceInfo>Jdbc=jdbc:mysql://192.168.23.128/accessinfo;JdbcUser=root;JdbcPassword=root;JdbcDrivers=com.mysql.jdbc.Driver;</DataSourceInfo>
<ProviderName>Mondrian</ProviderName>
<ProviderType>MDP</ProviderType>
<AuthenticationMode>Unauthenticated</AuthenticationMode>
<Catalogs>
<Catalog name="AccessInformation">
<Definition>/WEB-INF/queries/AccessInfo.xml</Definition>
</Catalog>
</Catalogs>
</DataSource>
4 添加查询页面
修改 query 文件夹下的 xmla.jsp 页面内容:
<jp:xmlaQuery id="query01"
uri="http://localhost:8080/mondrian/xmla"
dataSource="Provider=Mondrian;DataSource=AccessInfo;"
catalog="AccessInformation">
select NON EMPTY {[Measures].[Amount]} ON COLUMNS, NON EMPTY{([Access Time].[All Time], [Website Num].[All Site]) } ON ROWS from [Access Analysis] where [User IP].[All IP].[61.144.207.115]
</jp:xmlaQuery>
注意:红色字体中的括号不能少,另外 where 条件(即对应的切片过滤条件 slicer )也不能少。
分析查询的结果: