com.datastax.data.exploration.service.impl.PersistentDataServiceImpl Maven / Gradle / Ivy
package com.datastax.data.exploration.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.datastax.data.exploration.common.DataOperator;
import com.datastax.data.exploration.service.PersistentDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
/**数据持久化
* @author songfu 2017/12/01
*/
@Service("persistentDataService")
public class PersistentDataServiceImpl implements PersistentDataService {
private static final Logger logger = LoggerFactory.getLogger(PersistentDataServiceImpl.class);
/**
* 类型设定
* @param jsonObject 设定的类型名称和数据类型
*/
@Override
public void setDataType(String idUrl,JSONObject jsonObject) {
List listTypes = new ArrayList<>();
List listColumns = new ArrayList<>();
JSONArray jsonArrayData = JSONArray.parseArray(jsonObject.getString("list"));
for (Object jsonObjectCol : jsonArrayData) {
listColumns.add(JSONObject.parseObject(jsonObjectCol.toString()).getString("name"));
listTypes.add(JSONObject.parseObject(jsonObjectCol.toString()).getString("type"));
}
DataOperator.writeType(idUrl, listColumns.toArray(new String[listColumns.size()]),listTypes);
}
/**
* 类型设定时数据展示,100页
* @param id 数据集id
* @return 100页的展示数据
*/
@Override
public JSONObject typeShow(String id) {
List listRow = DataOperator.readEntireData(id);
String columns[] = DataOperator.readHeaders(id);
Map typesMap = DataOperator.readType(id);
if (listRow.size() > 100) {
listRow = listRow.subList(0,100);
}
List list = new ArrayList<>();
for (String column : columns) {
List dataList = listRow
.parallelStream()
.map(row -> row[columnIndex(columns,column)])
.collect(Collectors.toList());
JSONObject jsonObject = new JSONObject();
jsonObject.put("name",column);
jsonObject.put("type",typesMap.get(column));
jsonObject.put("data",dataList);
list.add(jsonObject);
}
JSONObject json = new JSONObject();
json.put("id",id);
json.put("list",list);
return json;
}
/**
* 过滤错误的数据集,并保存错误数据集和准确的数据集
* @param id 数据集id
*/
@Override
public void filterErrorData(String id) {
DataOperator.writeExactData(id,DataOperator.filterData(id).getValue0());
DataOperator.writeErrorData(id,DataOperator.filterData(id).getValue1());
}
}