一下鏈接為 SqlServer mdx 查詢的數據圖片 (主要是将数据集放在JavaBean,for (Position row : cellSet.getAxes().get(1)) 得到行 for (Position row : cellSet.getAxes().get(0)) 得到列 final Cell cell = cellSet.getCell(column, row); values.add(cell.getFormattedValue() == "" ? " " : cell.getFormattedValue()); // 这样是按照API来操作的 The
)
package com.msi.mdxDemo;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.joda.time.DateTime; import org.olap4j.Cell; import org.olap4j.CellSet; import org.olap4j.OlapConnection; import org.olap4j.OlapException; import org.olap4j.OlapStatement; import org.olap4j.Position;
public class MyMdxDemo {
public OlapConnection getOlapConnection() {
OlapConnection olapConnection = null;
try {
Class.forName("org.olap4j.driver.xmla.XmlaOlap4jDriver");
Connection connection = DriverManager
.getConnection("jdbc:xmla:Server=http://172.17.0.37/OLAPService/msmdpump.dll;Catalog=B_SY_FPYWX");
olapConnection = connection.unwrap(OlapConnection.class);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return olapConnection;
}
@SuppressWarnings("static-access")
public List<FpyBean> getFpyTb2(String lobNo, String day, int timeId) {
String day1 = "";
String day2 = "";
int timeId1 = timeId + 1;
List<FpyBean> list = new ArrayList<FpyBean>();
FpyBean bean = null;
try {
DateTime dt = new DateTime();
day2 = dt.parse(day).toString("yyyy-MM-dd");
day1 = dt.parse(day).plusDays(-1).toString("yyyy-MM-dd");// 往前推一天
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String mdxSql = "";
if (lobNo.equals("SPD-MB")) {
lobNo = "DT-MB";
mdxSql = "SELECT({[DimTime].[日期].&["
+ day1
+ "T00:00:00]*{[DimTimeSection].[TimeSection].["
+ timeId1
+ "]:[DimTimeSection].[TimeSection].[12]}+[DimTime].[日期].&["
+ day2
+ "T00:00:00]*{[DimTimeSection].[TimeSection].[1]:[DimTimeSection].[TimeSection].["
+ timeId + "]}})";
mdxSql += " ON COLUMNS,non empty({[DimWorkOrder].[LobByProduct].&["
+ lobNo
+ "]}*{[DimLinepage].[線別].children}*{[DimStation].[站別].&[6],[DimStation].[站別].&[91]}*{[Measures].[生產數量],[Measures].[FAIL數量],[Measures].[生產良率]}) ON ROWS FROM [B_SY_FPYWX]";
} else if (lobNo.equals("SPD-BB") || lobNo.equals("EPS-BB")
|| lobNo.equals("IPS-BB")) {
if (lobNo.equals("SPD-BB")) {
lobNo = "DT-BB";
}
mdxSql = "SELECT({[DimTime].[日期].&["
+ day1
+ "T00:00:00]*{[DimTimeSection].[TimeSection].["
+ timeId1
+ "]:[DimTimeSection].[TimeSection].[12]}+[DimTime].[日期].&["
+ day2
+ "T00:00:00]*{[DimTimeSection].[TimeSection].[1]:[DimTimeSection].[TimeSection].["
+ timeId + "]}})";
mdxSql += " ON COLUMNS,non empty({[DimWorkOrder].[LobByProduct].&["
+ lobNo
+ "]}*{[DimLinepage].[線別].children}*{[DimStation].[站別].&[8],[DimStation].[站別].&[9],[DimStation].[站別].&[11]}*{[Measures].[生產數量],[Measures].[FAIL數量],[Measures].[生產良率]}) ON ROWS FROM [B_SY_FPYWX]";
} else if (lobNo.equals("VGA")) {
mdxSql = "SELECT({[DimTime].[日期].&["
+ day1
+ "T00:00:00]*{[DimTimeSection].[TimeSection].["
+ timeId1
+ "]:[DimTimeSection].[TimeSection].[12]}+[DimTime].[日期].&["
+ day2
+ "T00:00:00]*{[DimTimeSection].[TimeSection].[1]:[DimTimeSection].[TimeSection].["
+ timeId + "]}})";
mdxSql += " ON COLUMNS,non empty({[DimWorkOrder].[LobByProduct].&["
+ lobNo
+ "]}*{[DimLinepage].[線別].children}*{[DimStation].[站別].&[6],[DimStation].[站別].&[42],[DimStation].[站別].&[94],[DimStation].[站別].&[96],[DimStation].[站別].&[60]}*{[Measures].[生產數量],[Measures].[FAIL數量],[Measures].[生產良率]}) ON ROWS FROM [B_SY_FPYWX]";
} else if (lobNo.equals("VE ASSY")) {
lobNo = "VE";
mdxSql = "SELECT({[DimTime].[日期].&["
+ day1
+ "T00:00:00]*{[DimTimeSection].[TimeSection].["
+ timeId1
+ "]:[DimTimeSection].[TimeSection].[12]}+[DimTime].[日期].&["
+ day2
+ "T00:00:00]*{[DimTimeSection].[TimeSection].[1]:[DimTimeSection].[TimeSection].["
+ timeId + "]}})";
mdxSql += " ON COLUMNS,non empty({[DimWorkOrder].[LobByProduct].&["
+ lobNo
+ "]}*{[DimStation].[制程別].&[ASSY]}*{[DimLinepage].[線別].children}*{[DimStation].[站別].&[8],[DimStation].[站別].&[9],[DimStation].[站別].&[366]}*{[Measures].[生產數量],[Measures].[FAIL數量],[Measures].[生產良率]}) ON ROWS FROM [B_SY_FPYWX]";
} else if (lobNo.equals("VE PCBA")) {
lobNo = "VE";
mdxSql = "SELECT({[DimTime].[日期].&["
+ day1
+ "T00:00:00]*{[DimTimeSection].[TimeSection].["
+ timeId1
+ "]:[DimTimeSection].[TimeSection].[12]}+[DimTime].[日期].&["
+ day2
+ "T00:00:00]*{[DimTimeSection].[TimeSection].[1]:[DimTimeSection].[TimeSection].["
+ timeId + "]}})";
mdxSql += " ON COLUMNS,non empty({[DimWorkOrder].[LobByProduct].&["
+ lobNo
+ "]}*{[DimStation].[制程別].&[PCBA]}*{[DimLinepage].[線別].children}*{[DimStation].[站別].&[6]}*{[Measures].[生產數量],[Measures].[FAIL數量],[Measures].[生產良率]}) ON ROWS FROM [B_SY_FPYWX]";
} else if (lobNo.equals("ROBOT ASSY")) {
lobNo = "ROBOT";
mdxSql = "SELECT({[DimTime].[日期].&["
+ day1
+ "T00:00:00]*{[DimTimeSection].[TimeSection].["
+ timeId1
+ "]:[DimTimeSection].[TimeSection].[12]}+[DimTime].[日期].&["
+ day2
+ "T00:00:00]*{[DimTimeSection].[TimeSection].[1]:[DimTimeSection].[TimeSection].["
+ timeId + "]}})";
mdxSql += " ON COLUMNS,non empty({[DimWorkOrder].[LobByProduct].&["
+ lobNo
+ "]}*{[DimStation].[制程別].&[ASSY]}*{[DimLinepage].[線別].children}*{[DimStation].[站別].&[8],[DimStation].[站別].&[9],[DimStation].[站別].&[50],[DimStation].[站別].&[20]}*{[Measures].[生產數量],[Measures].[FAIL數量],[Measures].[生產良率]}) ON ROWS FROM [B_SY_FPYWX]";
} else if (lobNo.equals("ROBOT PCBA")) {
lobNo = "ROBOT";
mdxSql = "SELECT({[DimTime].[日期].&["
+ day1
+ "T00:00:00]*{[DimTimeSection].[TimeSection].["
+ timeId1
+ "]:[DimTimeSection].[TimeSection].[12]}+[DimTime].[日期].&["
+ day2
+ "T00:00:00]*{[DimTimeSection].[TimeSection].[1]:[DimTimeSection].[TimeSection].["
+ timeId + "]}})";
mdxSql += " ON COLUMNS,non empty({[DimWorkOrder].[LobByProduct].&["
+ lobNo
+ "]}*{[DimStation].[制程別].&[PCBA]}*{[DimLinepage].[線別].children}*{[DimStation].[站別].&[6]}*{[Measures].[生產數量],[Measures].[FAIL數量],[Measures].[生產良率]}) ON ROWS FROM [B_SY_FPYWX]";
} else {
if (lobNo.equals("SPD-SC")) {
lobNo = "Other Card";
}
mdxSql = "SELECT({[DimTime].[日期].&["
+ day1
+ "T00:00:00]*{[DimTimeSection].[TimeSection].["
+ timeId1
+ "]:[DimTimeSection].[TimeSection].[12]}+[DimTime].[日期].&["
+ day2
+ "T00:00:00]*{[DimTimeSection].[TimeSection].[1]:[DimTimeSection].[TimeSection].["
+ timeId + "]}})";
mdxSql += " ON COLUMNS,non empty({[DimWorkOrder].[LobByProduct].&["
+ lobNo
+ "]}*{[DimLinepage].[線別].children}*{[DimStation].[站別].&[6]}*{[Measures].[生產數量],[Measures].[FAIL數量],[Measures].[生產良率]}) ON ROWS FROM [B_SY_FPYWX]";
}
List<String> days = null; // new ArrayList<String>();
List<Integer> timeIds = null; // new ArrayList<Integer>();
List<String> values = null; // new ArrayList<String>();
OlapConnection olapConnection = null;
OlapStatement olapStatement = null;
CellSet cellSet = null;
try {
olapConnection = getOlapConnection();
olapStatement = olapConnection.createStatement();
cellSet = olapStatement.executeOlapQuery(mdxSql);
if (lobNo.equals("VE") || lobNo.equals("ROBOT")) {
for (Position row : cellSet.getAxes().get(1)) {
bean = new FpyBean();
bean.setLobNo(row.getMembers().get(0).getName() + " "
+ row.getMembers().get(1).getName());
bean.setLineNo(row.getMembers().get(2).getName());
bean.setStation(row.getMembers().get(3).getName());
bean.setItem(row.getMembers().get(4).getName());
days = new ArrayList<String>();
timeIds = new ArrayList<Integer>();
values = new ArrayList<String>();
for (Position column : cellSet.getAxes().get(0)) {
days.add(column.getMembers().get(0).getName());
timeIds.add(Integer.parseInt(column.getMembers().get(1)
.getName()));
final Cell cell = cellSet.getCell(column, row);
values.add(cell.getFormattedValue() == "" ? " " : cell
.getFormattedValue());
}
bean.setDay(days);
bean.setTimeId(timeIds);
bean.setValues(values);
list.add(bean);
}
} else {
// 当做二维数据进行操作
for (Position row : cellSet.getAxes().get(1)) { // 行操作
bean = new FpyBean();
bean.setLobNo(row.getMembers().get(0).getName());// 行的第一個單元格
bean.setLineNo(row.getMembers().get(1).getName());// 行的第二個單元格
bean.setStation(row.getMembers().get(2).getName());// 行的第三個單元格
bean.setItem(row.getMembers().get(3).getName());// 行的第四個單元格
// System.out.println(" LobNo()=" + bean.getLobNo() +
// " LineNo()=" + bean.getLineNo() + " Station()="+
// bean.getStation() + " Item()=" + bean.getItem());
days = new ArrayList<String>();
timeIds = new ArrayList<Integer>();
values = new ArrayList<String>();
for (Position column : cellSet.getAxes().get(0)) { // 以列开始
// System.out.println(column.getMembers().get(0).getName()+" "+column.getMembers().get(1).getName());
days.add(column.getMembers().get(0).getName());
timeIds.add(Integer.parseInt(column.getMembers().get(1)
.getName()));
final Cell cell = cellSet.getCell(column, row);
values.add(cell.getFormattedValue() == "" ? " " : cell
.getFormattedValue()); // 这样是按照API来操作的 The
// formatted value is
// never null. In
// particular, when the
// cell contains the MDX
// NULL value,
// getValue() will
// return the Java null
// value but this method
// will return the empty
// string "".
}
bean.setDay(days);
bean.setTimeId(timeIds);
bean.setValues(values);
list.add(bean);
}
}
} catch (OlapException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
List<FpyBean> list = new MyMdxDemo().getFpyTb2("ROBOT ASSY",
"2014-01-21", 1);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getLobNo() + " "
+ list.get(i).getLineNo() + " " + list.get(i).getStation()
+ " " + list.get(i).getItem() + " "
+ list.get(i).getDay() + " " + list.get(i).getTimeId()
+ " " + list.get(i).getValues() + " ");
}
// 循环出Days List
if (list.get(0).getDay() != null || list.get(0).getDay().size() > 0) {
List<String> days = list.get(0).getDay();
for (int i = 0; i < days.size(); i++) {
System.err.print(days.get(i) + " ");
}
}
//循環出來mdx的結果的方法 ,主要是根據數據的類型,比如說FAIL數量 /生產數量/生產良率
//一般會有時間這個维度,将时间维度的长度作为数据遍历的长度
for (int i = 0; i < list.size(); i++) {
if (list.get(i).getItem().equals("FAIL數量")) {
for (int j = 0; j < list.get(i).getDay() .size() ; j++) {
System.out.print(list.get(i).getValues().get(j)+" ");
}
}
System.out.println(" ");
}
}
}