com.datastax.data.exploration.service.impl.DataExhibitionServiceImpl Maven / Gradle / Ivy
package com.datastax.data.exploration.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.datastax.data.exploration.service.DataExhibitionService;
import com.datastax.data.exploration.common.DataOperator;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.datastax.data.exploration.util.CommonUtil.columnIndex;
import static com.datastax.data.exploration.util.CommonUtil.matchType;
/**数据展现
* @author songfu 2017/12/03
*/
@Service("dataExhibitionService")
public class DataExhibitionServiceImpl implements DataExhibitionService {
/**
* 数据展现
* @param id 数据集id
* @param column 数据集的属性名
* @param mode 操作方式
* @return 展示的数据集
*/
@Override
public JSONObject exhibitData(String id, String column, String mode) {
Map typesMap = DataOperator.readType(id);
String columns[] = DataOperator.readHeaders(id);
List listRow = DataOperator.readEntireData(id);
if (!column.equals("all")) {
int columnIndex = columnIndex(columns,column);
if (mode.equals("missing")) {
listRow = listRow
.parallelStream()
.filter(row -> row[columnIndex] == null || row[columnIndex].trim().isEmpty())
.collect(Collectors.toList());
}
if (mode.equals("noMissing")) {
listRow = listRow
.parallelStream()
.filter(row -> !(row[columnIndex] == null || row[columnIndex].trim().isEmpty()))
.collect(Collectors.toList());
}
if (mode.equals("error")) {
listRow = DataOperator.readErrorData(id);
listRow = listRow
.parallelStream()
.filter(row -> !(row[columnIndex] == null || row[columnIndex].trim().isEmpty()))
.filter(row -> !matchType(row[columnIndex], typesMap.get(column)))
.collect(Collectors.toList(
));
}
}
if ("all".equals(column)){
if (mode.equals("missing")) {
listRow = listRow
.parallelStream()
.filter(row -> {
boolean matchFlag = false;
String col;
for (String colName : columns) {
col = row[columnIndex(columns, colName)];
if (col == null || col.trim().isEmpty()) {
matchFlag = true;
break;
}
}
return matchFlag;
})
.collect(Collectors.toList());
}
if (mode.equals("noMissing")) {
listRow = listRow
.parallelStream()
.filter(row -> {
boolean matchFlag = true;
String col;
for (String colName : columns) {
col = row[columnIndex(columns, colName)];
if (col == null || col.trim().isEmpty()) {
matchFlag = false;
break;
}
}
return matchFlag;
})
.collect(Collectors.toList());
}
if (mode.equals("error")) {
listRow = DataOperator.readErrorData(id)
.parallelStream()
.filter(row -> {
boolean matchFlag = false;
String col;
for (String colName : columns) {
col = row[columnIndex(columns, colName)];
if (!(col == null || col.trim().isEmpty())) {
matchFlag = true;
break;
}
}
return matchFlag;
})
.collect(Collectors.toList());
}
}
List list = new ArrayList<>();
for (String colName : columns) {
List dataList = listRow
.parallelStream()
.map(row -> {
Object obj = row[columnIndex(columns,colName)];
if (obj == null) {
obj = "";
}
return obj;
})
.collect(Collectors.toList());
JSONObject jsonObject = new JSONObject();
jsonObject.put("name",colName);
jsonObject.put("type",typesMap.get(colName));
jsonObject.put("data",dataList);
list.add(jsonObject);
}
JSONObject json = new JSONObject();
json.put("id",id);
json.put("list",list);
return json;
}
}