Saikuソースコードを変更してデフォルトのクエリーを実現する1日のデータ(10)
7836 ワード
Saikuは、ソースコードを変更することで、デフォルトのクエリーの前日のデータを実現します.
saikuがローカルでコンパイルする方法の前にチュートリアルの説明があり、次は元のコードを変更しました(ネットから学んだチュートリアルで、現在は最新の日のデータ情報しか取得していません)
参考ブログアドレス:https://blog.csdn.net/zstao168/article/details/52818569
1.主に変更されたクラス情報:saiku-webエンジニアリングにおけるQuery 2 Resource.JAvaでは、具体的なクラスパスは、/saiku-web/src/main/java/org/saiku/web/rest/resources/Query 2 Resource.java
このクラスのpublic QueryResult execute(ThinQuery tq)メソッドにtq=thisを追加する.restrictQueryByDate(tq);(executeメソッドはこのクラスの185-220行程度)
Query 2 ResourceでJAvaクラスにThinQuery restrictQueryByDate(ThinQuery tq)メソッドを追加します.内容は以下の通りです.
ps:hie.getLevels().get(「SaikuUseDate」)で得られたThinLevelオブジェクトにはsetSelection(selection)メソッドはありません.ThinLevelにsetメソッドを手動で追加する必要があります.
もう一つ注意したいのは、これらのコードを追加した後、saikuのschameファイルのcubeにはSaikuUseDate日付フィールドが必要です.
最後にプロジェクト全体をパッケージ化してコンパイルし、新しいsaiku-serverを起動し、いくつかのデータ情報を構成すると、最近のデータ情報を取得できます.
saikuがローカルでコンパイルする方法の前にチュートリアルの説明があり、次は元のコードを変更しました(ネットから学んだチュートリアルで、現在は最新の日のデータ情報しか取得していません)
参考ブログアドレス:https://blog.csdn.net/zstao168/article/details/52818569
1.主に変更されたクラス情報:saiku-webエンジニアリングにおけるQuery 2 Resource.JAvaでは、具体的なクラスパスは、/saiku-web/src/main/java/org/saiku/web/rest/resources/Query 2 Resource.java
このクラスのpublic QueryResult execute(ThinQuery tq)メソッドにtq=thisを追加する.restrictQueryByDate(tq);(executeメソッドはこのクラスの185-220行程度)
/**
*
* Execute a Saiku Query
* @summary Execute Query
* @param tq Thin Query model
* @return A query result set.
*/
@POST
@Consumes({"application/json" })
@Path("/execute")
public QueryResult execute(ThinQuery tq) {
//add code to limit query date
tq = this.restrictQueryByDate(tq);
try {
if (thinQueryService.isMdxDrillthrough(tq)) {
Long start = (new Date()).getTime();
ResultSet rs = thinQueryService.drillthrough(tq);
QueryResult rsc = RestUtil.convert(rs);
rsc.setQuery(tq);
Long runtime = (new Date()).getTime()- start;
rsc.setRuntime(runtime.intValue());
return rsc;
}
QueryResult qr = RestUtil.convert(thinQueryService.execute(tq));
ThinQuery tqAfter = thinQueryService.getContext(tq.getName()).getOlapQuery();
qr.setQuery(tqAfter);
return qr;
}
catch (Exception e) {
log.error("Cannot execute query (" + tq + ")",e);
String error = ExceptionUtils.getRootCauseMessage(e);
return new QueryResult(error);
}
}
Query 2 ResourceでJAvaクラスにThinQuery restrictQueryByDate(ThinQuery tq)メソッドを追加します.内容は以下の通りです.
// add code for query limit
private ThinQuery restrictQueryByDate(ThinQuery tq) {
ThinQueryModel queryModel = tq.getQueryModel();
Map axesMap = queryModel.getAxes();
NamedList namedList = new NamedListImpl();
ThinAxis filterAxis = axesMap.get(AxisLocation.FILTER);
List filterHie = filterAxis.getHierarchies();
namedList = this.resetThinHierachy(filterHie);
// Row set queryModel
if(namedList.size() > 0) {
ThinAxis newFilterAxis = new ThinAxis(
AxisLocation.FILTER,
namedList,
filterAxis.isNonEmpty(),
filterAxis.getAggregators()
);
axesMap.put(AxisLocation.FILTER,newFilterAxis);
}
// Row set queryModel
if(namedList.size() == 0) {
ThinAxis rowAxis = axesMap.get(AxisLocation.ROWS);
List rowHie = rowAxis.getHierarchies();
namedList = this.resetThinHierachy(rowHie);
if(namedList.size() > 0) {
ThinAxis newRowsAxis = new ThinAxis(
AxisLocation.ROWS,
namedList,
rowAxis.isNonEmpty(),
rowAxis.getAggregators()
);
axesMap.put(AxisLocation.ROWS,newRowsAxis);
}
}
//if columns contained date member
//if contained and have not set the limit ,then add limit to one day ,if do not hava then add this column and limit to one day;
if(namedList.size() == 0) {
// namedList.clear();
ThinAxis colAxis = axesMap.get(AxisLocation.COLUMNS);
List colHie = colAxis.getHierarchies();
namedList = this.resetThinHierachy(colHie);
if(namedList.size() == 0) {
//if list is empty,then column don't hava date ,then add colHie to list and enfore to add date member;
namedList.addAll(colHie);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String yesterday = format.format(new Date(new Date().getTime() - 24 * 60 * 60 * 1000));
String newDateMdx = "[SaikuUseDate].[SaikuUseDate].["+yesterday+"]";
ThinMember thinMember = new ThinMember(yesterday,newDateMdx,yesterday);
ThinHierarchy thinHie = new ThinHierarchy();
thinHie.setName("[SaikuUseDate].[SaikuUseDate]");
List thinMemberList = new ArrayList();
thinMemberList.add(thinMember);
ThinSelection selection = new ThinSelection();
selection.setMembers(thinMemberList);
selection.setType(ThinSelection.Type.INCLUSION);
ThinLevel thinLevel = new ThinLevel(yesterday,yesterday,selection,null);
// thinLevel.setSelection(selection);
Map mapLevel = new LinkedHashMap();
mapLevel.put("SaikuUseDate", thinLevel);
thinHie.setLevels(mapLevel);
namedList.add(thinHie);
ThinAxis newColAxis = new ThinAxis(
AxisLocation.COLUMNS,
namedList,
colAxis.isNonEmpty(),
colAxis.getAggregators()
);
axesMap.put(AxisLocation.COLUMNS,newColAxis);
}
}
return tq;
}
private NamedList resetThinHierachy(List hieList) {
NamedList namedList = new NamedListImpl();
boolean flag = false;
for(ThinHierarchy hie : hieList) {
if(hie.getName().equals("[SaikuUseDate].[SaikuUseDate]")) {
if(hie.getLevels().get("SaikuUseDate").getSelection() == null) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String yesterday = format.format(new Date(new Date().getTime() - 24 * 60 * 60 * 1000));
String newDateMdx = "[SaikuUseDate].[SaikuUseDate].["+yesterday+"]";
ThinMember thinMember = new ThinMember(null,newDateMdx,yesterday);
List thinMemberList = new ArrayList();
thinMemberList.add(thinMember);
ThinSelection selection = new ThinSelection();
selection.setMembers(thinMemberList);
hie.getLevels().get("SaikuUseDate").setSelection(selection);
}
flag = true;
}
namedList.add(hie);
}
if(flag) return namedList;
namedList.clear();
return namedList;
}
ps:hie.getLevels().get(「SaikuUseDate」)で得られたThinLevelオブジェクトにはsetSelection(selection)メソッドはありません.ThinLevelにsetメソッドを手動で追加する必要があります.
もう一つ注意したいのは、これらのコードを追加した後、saikuのschameファイルのcubeにはSaikuUseDate日付フィールドが必要です.
最後にプロジェクト全体をパッケージ化してコンパイルし、新しいsaiku-serverを起動し、いくつかのデータ情報を構成すると、最近のデータ情報を取得できます.