Mondrian学习(2):什么是mondrian
? ? mondrian是一個(gè)開(kāi)放源碼的Rolap服務(wù)器,使用java技術(shù)開(kāi)發(fā)。它實(shí)現(xiàn)了xmla和jolap規(guī)范。并且支持由Microsoft,Hypeion等公司研究的多維查詢表達(dá)式MDX(類似于sql)。
? ? 到目前關(guān)于Mondrian的資料還是相對(duì)較少。多數(shù)就是對(duì)官網(wǎng)上的demo基礎(chǔ)的使用并沒(méi)有具體在項(xiàng)目中的具體集成和使用。
? ? Mondrian下載:https://sourceforge.net/projects/mondrian/?
? ?Mondrian API:https://mondrian.pentaho.com/documentation/architecture.php
? ?先來(lái)一張官方的圖:
整個(gè)Mondrian分為四層:
? ? 1.表現(xiàn)層(presentation layer):
從最上面可以看到mondrian的入口
? ? 1).HTTP(JPivot)得到結(jié)果,并且官方提供的war包就是就是基于此的,但是通過(guò)資料的查新這個(gè)現(xiàn)實(shí)層以及停止維護(hù)已經(jīng)很老并且并不是基于mvc模式開(kāi)發(fā),舍棄掉。
? ? 2).XML/HTTP也是官方提供的,舍棄。
? ? 3).JAVA通過(guò)API調(diào)用得到結(jié)果,這個(gè)正是我想要的,通過(guò)集成到j(luò)ava web中展示層則使用echarts等工具。
? ? 2. 計(jì)算層(dimmensinal layer):分析和校驗(yàn)MDX語(yǔ)句,并且將分析后的語(yǔ)句發(fā)送到聚集層進(jìn)行聚集
? ? 3.聚集層(star layer):聚集層是一個(gè)緩沖層對(duì)一些操作過(guò)的數(shù)據(jù)進(jìn)行緩存提供系統(tǒng)的性能。
? ? 4.存儲(chǔ)層:提供聚集的元數(shù)據(jù),可以在多臺(tái)機(jī)器上。
? ? 通過(guò)上面可以了解到我們接觸到比較多的是展示層和存儲(chǔ)層和計(jì)算層的MDX語(yǔ)句的編寫。
先通過(guò)Java做一個(gè)demo 吧。
先準(zhǔn)備環(huán)境:搭建存儲(chǔ)層
表結(jié)構(gòu):
需要的jar包
需要用到的聚集文件:
<Schema name="db"><!--創(chuàng)建一個(gè)數(shù)據(jù)立方體--><Cube name="Sales" visible="true" cache="true" enabled="true"><!--事實(shí)表 使用name指定表名--><Table name="sale"></Table><!--維度1 foreignKey 事實(shí)表中的外鍵id name維度名稱 --><Dimension visible="true" foreignKey="cusId" highCardinality="false" name="cusGender"><!--維度細(xì)分的層次 allMemberName 所有維度的名稱 primaryKey維度表的主鍵 hasAll是否包含所有維度--><Hierarchy visible="true" hasAll="true" allMemberName="allGender" primaryKey="cusId"><!----><Table name="customer"></Table><!--定義層次的水平,column 維度表的那一列 .... --><Level name="gender" visible="true" column="gender" type="String" uniqueMembers="false" levelType="Regular" hideMemberIf="Never"></Level></Hierarchy></Dimension><Dimension visible="true" foreignKey="proId" highCardinality="false" name="proType"><!--primaryKey 維度主鍵 primaryKeyTable維度主鍵對(duì)應(yīng)的表--><Hierarchy visible="true" hasAll="true" allMemberName="allPro" primaryKey="proId" primaryKeyTable="product"><!--兩張表外鍵關(guān)系--><Join leftKey="proTypeId" rightKey="proTypeId"><Table name="product"></Table><Table name="producttype"></Table></Join><!--具體的維度 --><Level name="proTypeId" visible="true" table="producttype" column="protypeid" nameColumn="protypename" type="String" uniqueMembers="true" levelType="Regular" hideMemberIf="Never"></Level><Level name="proId" visible="true" table="product" column="proId" nameColumn="proName" type="String" uniqueMembers="true" levelType="Regular" hideMemberIf="Never"></Level></Hierarchy></Dimension><!--事實(shí)表 aggregator 統(tǒng)計(jì)時(shí)使用的函數(shù) sum累加 formatString 格式化字符串,可以格式化類似Money、時(shí)間之類的格式--><Measure name="numb" column="number" datatype="Numeric" aggregator="sum"></Measure><Measure name="totalSale" formatString="¥#,##0.00" aggregator="sum"><!--sql 片段--><MeasureExpression><SQL dialect="generic"><![CDATA[(unitPrice*number)]]></SQL></MeasureExpression></Measure><CalculatedMember name="averPri" dimension="Measures"><Formula><![CDATA[[Measures].[totalSale] / [Measures].[numb]]]></Formula><CalculatedMemberProperty name="FORMAT_STRING" value="¥#,##0.00"></CalculatedMemberProperty></CalculatedMember></Cube> </Schema>Java demo文件? 為了方便就直接寫的main
package com.yanghs.test;import mondrian.olap.Result; import org.apache.log4j.PropertyConfigurator; import org.olap4j.*; import org.olap4j.metadata.Member;import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.InputStream; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException;/*** @author yanghs* @Description:* @date 2018/4/21 11:49*/ public class demo1 {public static void main(String[] args) throws FileNotFoundException {OlapConnection conn = null;try {InputStream is = new BufferedInputStream(new FileInputStream("G:\\mycode\\mondriandemo\\src\\main\\javacode\\com\\yanghs\\test\\log4j.properties"));PropertyConfigurator.configure( is);conn= getConnection(//URL協(xié)議"jdbc:mondrian:"//連接數(shù)據(jù)源的JDBC連接+ "Jdbc='jdbc:mysql://localhost:3306/testdb?user=root&password=123456&useUnicode=true&characterEncoding=utf8';"//數(shù)據(jù)模型文件+ "Catalog=G:\\mycode\\mondriandemo\\src\\main\\javacode\\com\\yanghs\\test\\qiuschema.xml;"//連接數(shù)據(jù)源用到的驅(qū)動(dòng)+ "JdbcDrivers=com.mysql.jdbc.Driver;");} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException e) {e.printStackTrace();}//查詢語(yǔ)句String mdx="SELECT {[Measures].[numb]} ON 0,{[proType].[proId].members} on 1 FROM [Sales]";CellSet cs= null;try {cs = getResultSet(mdx, conn);} catch (OlapException e) {e.printStackTrace();}/*遍歷結(jié)果*/if(cs.getAxes().size()>1){for (Position row : cs.getAxes().get(1)) {for (Position column : cs.getAxes().get(0)) {for (Member member : row.getMembers()) {System.out.println("rows:"+member.getUniqueName());}for (Member member : column.getMembers()) {System.out.println("columns:"+member.getUniqueName());}final Cell cell = cs.getCell(column, row);System.out.println("values:"+cell.getValue());System.out.println();}}}else{for(Position column:cs.getAxes().get(0)){for(Member member:column.getMembers()){System.out.println("columns:"+member.getUniqueName());}Cell cell=cs.getCell(column);System.out.print("values:"+cell.getValue());System.out.println();}}}/*** 得到連接* @param url* @return* @throws ClassNotFoundException* @throws SQLException*/public static OlapConnection getConnection(String url) throws ClassNotFoundException, SQLException {Class.forName("mondrian.olap4j.MondrianOlap4jDriver");Connection connection = DriverManager.getConnection(url);return connection.unwrap(OlapConnection.class);}/*** 得打查詢的結(jié)果* @param mdx* @param conn* @return* @throws OlapException*/public static CellSet getResultSet(String mdx, OlapConnection conn) throws OlapException {OlapStatement statement = conn.createStatement();CellSet cellSet = statement.executeOlapQuery(mdx);return cellSet;} }運(yùn)行結(jié)果
rows:[proType].[shiwu].[baozi] columns:[Measures].[numb] values:3.0rows:[proType].[shiwu].[mantou] columns:[Measures].[numb] values:3.0rows:[proType].[diannao].[hp] columns:[Measures].[numb] values:58.0rows:[proType].[diannao].[lenovo] columns:[Measures].[numb] values:77.0rows:[proType].[diannao].[asus] columns:[Measures].[numb] values:54.0rows:[proType].[jiaju].[zhuozi] columns:[Measures].[numb] values:36.0rows:[proType].[jiaju].[yizi] columns:[Measures].[numb] values:57.0通過(guò)日志文件打印了查詢sql語(yǔ)句,olap引擎把mdx語(yǔ)句翻譯為sql查詢數(shù)據(jù)。
通過(guò)上面mdx語(yǔ)句執(zhí)行是不是很像jdbc的編程,其實(shí)mondrian做的事情就是把mdx語(yǔ)句翻譯為sql語(yǔ)句,而mdx語(yǔ)句是專門用來(lái)做多維數(shù)據(jù)分析。
這個(gè)項(xiàng)目很小就不上傳了,建表語(yǔ)句和pom.xml 及我的maven下載不到的jar包。
下載:https://pan.baidu.com/s/1OiYEE1aaWYEiJhZr6elhqw
總結(jié)
以上是生活随笔為你收集整理的Mondrian学习(2):什么是mondrian的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【安全知识分享】操作类员工安全培训.pp
- 下一篇: 同步助手java_QQ同步助手Java版