com.github.mengxianun.elasticsearch.data.ElasticsearchSQLQuerySummary Maven / Gradle / Ivy
package com.github.mengxianun.elasticsearch.data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.github.mengxianun.core.Action;
import com.github.mengxianun.core.App;
import com.github.mengxianun.core.data.DefaultHeader;
import com.github.mengxianun.core.data.DefaultRow;
import com.github.mengxianun.core.data.Header;
import com.github.mengxianun.core.data.Row;
import com.github.mengxianun.core.data.summary.QuerySummary;
import com.github.mengxianun.core.item.ColumnItem;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class ElasticsearchSQLQuerySummary extends QuerySummary {
protected final String resultString;
protected final JsonObject resultObject;
public ElasticsearchSQLQuerySummary(String resultString) {
this(null, resultString);
}
public ElasticsearchSQLQuerySummary(Action action, String resultString) {
super(action, null);
this.resultString = resultString;
this.resultObject = App.gson().fromJson(resultString, JsonObject.class);
}
private Header createHeader() {
JsonArray columnsArray = resultObject.getAsJsonArray("columns");
List columnItems = new ArrayList<>();
columnsArray.forEach(c -> {
JsonObject columnObject = c.getAsJsonObject();
String column = columnObject.get("name").getAsString();
columnItems.add(new ColumnItem(column));
});
return new DefaultHeader(columnItems);
}
public JsonArray getResultRows() {
return resultObject.getAsJsonArray("rows");
}
@Override
public List toRows() {
List rows = new ArrayList<>();
List columnItems = action.getColumnItems();
Header rowHeader = columnItems.isEmpty() ? createHeader() : header;
JsonArray rowsArray = getResultRows();
rowsArray.forEach(e -> rows.add(new DefaultRow(rowHeader, App.gson().fromJson(e, Object[].class))));
return rows;
}
@Override
public List