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 {
private final String resultString;
public ElasticsearchSQLQuerySummary(String resultString) {
this(null, resultString);
}
public ElasticsearchSQLQuerySummary(Action action, String resultString) {
super(action, null);
this.resultString = resultString;
}
private Header createHeader(JsonObject resultObject) {
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);
}
@Override
public List toRows() {
List rows = new ArrayList<>();
JsonObject jsonObject = App.gson().fromJson(resultString, JsonObject.class);
List columnItems = action.getColumnItems();
Header rowHeader = columnItems.isEmpty() ? createHeader(jsonObject) : header;
JsonArray rowsArray = jsonObject.getAsJsonArray("rows");
rowsArray.forEach(e -> rows.add(new DefaultRow(rowHeader, App.gson().fromJson(e, Object[].class))));
return rows;
}
@Override
public List