use of com.webank.wedatasphere.qualitis.response.ApplicationDatabaseResponse in project Qualitis by WeBankFinTech.
the class ApplicationServiceImpl method uploadDataSourceAnalysisResult.
@Override
public GeneralResponse<?> uploadDataSourceAnalysisResult(UploadResultRequest request) throws UnExpectedRequestException, IOException {
// Login user permission.
Long userId = HttpUtils.getUserId(httpServletRequest);
User user = userDao.findById(userId);
if (user == null) {
throw new UnExpectedRequestException("User {&DOES_NOT_EXIST}");
}
UploadResultRequest.checkRequest(request);
List<ApplicationClusterResponse> responses = (List<ApplicationClusterResponse>) getDataSource(new PageRequest(0, Integer.MAX_VALUE)).getData();
List<String> tables = responses.stream().filter(cluster -> cluster.getClusterName().equals(request.getClusterName())).map(ApplicationClusterResponse::getDatabase).flatMap(database -> database.stream()).filter(databaseResponse -> databaseResponse.getDatabaseName().equals(request.getDatabaseName())).map(ApplicationDatabaseResponse::getTable).flatMap(table -> table.stream()).distinct().collect(Collectors.toList());
if (StringUtils.isNotBlank(request.getTableName())) {
tables.clear();
tables.add(request.getTableName());
}
LOGGER.info("Start to write excel");
StringBuffer fileName = new StringBuffer();
fileName.append(linkisConfig.getUploadTmpPath()).append(File.separator).append(user.getUserName()).append("_").append(request.getClusterName()).append("_").append(request.getDatabaseName()).append("_").append(UUID.randomUUID().toString()).append(ExcelTypeEnum.XLSX.getValue());
File tmpFile = new File(fileName.toString());
writeExcelFile(tmpFile, tables, request);
// Upload to HDFS
ClusterInfo clusterInfo = clusterInfoDao.findByClusterName(request.getClusterName());
if (clusterInfo == null) {
throw new UnExpectedRequestException("Cluster info " + "[" + request.getClusterName() + "]" + "{&DOES_NOT_EXIST}");
}
// send request to get dbs
String url = UriBuilder.fromUri(clusterInfo.getLinkisAddress()).path(linkisConfig.getPrefix()).path(linkisConfig.getUpload()).toString();
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setContentType(ContentType.MULTIPART_FORM_DATA);
multipartEntityBuilder.setCharset(Charset.forName("UTF-8"));
if (clusterInfo.getClusterType().endsWith(LINKIS_ONE_VERSION)) {
multipartEntityBuilder.addTextBody("path", linkisConfig.getUploadPrefix() + LINKIS_ONE_UPLOAD_PREFIX + request.getHdfsPath());
} else {
multipartEntityBuilder.addTextBody("path", linkisConfig.getUploadPrefix() + request.getHdfsPath());
}
multipartEntityBuilder.addBinaryBody("file", tmpFile);
httppost.addHeader("Token-User", user.getUserName());
httppost.addHeader("Token-Code", clusterInfo.getLinkisToken());
httppost.setEntity(multipartEntityBuilder.build());
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (IOException e) {
throw new UnExpectedRequestException("{&FAILED_TO_CALL_UPLOAD_API}");
}
int code = response.getStatusLine().getStatusCode();
response.close();
if (code != HttpStatus.SC_OK) {
throw new UnExpectedRequestException("{&FAILED_TO_CALL_UPLOAD_API}");
}
boolean tmpDeleteFlag = tmpFile.delete();
LOGGER.info("Delete tmp excel: " + tmpDeleteFlag);
return new GeneralResponse<>(code + "", "{&SUCCESS_TO_UPLOAD_ANALYSIS_EXCEL}", code);
}
use of com.webank.wedatasphere.qualitis.response.ApplicationDatabaseResponse in project Qualitis by WeBankFinTech.
the class ApplicationServiceImpl method putIntoDatabase.
private void putIntoDatabase(Map<String, String> taskDataSourceMap, ApplicationClusterResponse applicationClusterResponse) {
String database = taskDataSourceMap.get("db_name");
if (StringUtils.isEmpty(database)) {
return;
}
if (applicationClusterResponse.getMap().containsKey(database)) {
ApplicationDatabaseResponse response = applicationClusterResponse.getMap().get(database);
putIntoTable(taskDataSourceMap, response);
} else {
ApplicationDatabaseResponse response = new ApplicationDatabaseResponse(database);
response.setDatabaseName(database);
putIntoTable(taskDataSourceMap, response);
applicationClusterResponse.getMap().put(database, response);
applicationClusterResponse.getDatabase().add(response);
}
}
Aggregations