You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
328 lines
10 KiB
328 lines
10 KiB
5 years ago
|
/*
|
||
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||
|
* contributor license agreements. See the NOTICE file distributed with
|
||
|
* this work for additional information regarding copyright ownership.
|
||
|
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||
|
* (the "License"); you may not use this file except in compliance with
|
||
|
* the License. You may obtain a copy of the License at
|
||
|
*
|
||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
*
|
||
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
* See the License for the specific language governing permissions and
|
||
|
* limitations under the License.
|
||
|
*/
|
||
5 years ago
|
package org.apache.dolphinscheduler.api.service;
|
||
5 years ago
|
|
||
5 years ago
|
import org.apache.dolphinscheduler.api.enums.Status;
|
||
|
import org.apache.dolphinscheduler.api.utils.Constants;
|
||
|
import org.apache.dolphinscheduler.api.utils.PageInfo;
|
||
|
import org.apache.dolphinscheduler.api.utils.Result;
|
||
|
import org.apache.dolphinscheduler.common.enums.UdfType;
|
||
|
import org.apache.dolphinscheduler.common.utils.PropertyUtils;
|
||
|
import org.apache.dolphinscheduler.dao.entity.Resource;
|
||
|
import org.apache.dolphinscheduler.dao.entity.UdfFunc;
|
||
|
import org.apache.dolphinscheduler.dao.entity.User;
|
||
|
import org.apache.dolphinscheduler.dao.mapper.ResourceMapper;
|
||
|
import org.apache.dolphinscheduler.dao.mapper.UDFUserMapper;
|
||
|
import org.apache.dolphinscheduler.dao.mapper.UdfFuncMapper;
|
||
5 years ago
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||
5 years ago
|
import org.apache.commons.lang.StringUtils;
|
||
|
import org.slf4j.Logger;
|
||
|
import org.slf4j.LoggerFactory;
|
||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
import org.springframework.stereotype.Service;
|
||
|
import org.springframework.transaction.annotation.Transactional;
|
||
|
|
||
|
import java.util.Date;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
* udf function service
|
||
|
*/
|
||
|
@Service
|
||
|
public class UdfFuncService extends BaseService{
|
||
|
|
||
|
private static final Logger logger = LoggerFactory.getLogger(UdfFuncService.class);
|
||
|
|
||
|
@Autowired
|
||
|
private ResourceMapper resourceMapper;
|
||
|
|
||
|
@Autowired
|
||
|
private UdfFuncMapper udfFuncMapper;
|
||
|
|
||
|
@Autowired
|
||
|
private UDFUserMapper udfUserMapper;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* create udf function
|
||
|
*
|
||
|
* @param loginUser
|
||
|
* @param funcName
|
||
|
* @param argTypes
|
||
|
* @param database
|
||
|
* @param desc
|
||
|
* @param type
|
||
|
* @param resourceId
|
||
|
* @return
|
||
|
*/
|
||
|
public Result createUdfFunction(User loginUser,
|
||
|
String funcName,
|
||
|
String className,
|
||
|
String argTypes,
|
||
|
String database,
|
||
|
String desc,
|
||
|
UdfType type,
|
||
|
int resourceId) {
|
||
|
Result result = new Result();
|
||
|
|
||
5 years ago
|
// if resource upload startup
|
||
|
if (!PropertyUtils.getResUploadStartupState()){
|
||
|
logger.error("resource upload startup state: {}", PropertyUtils.getResUploadStartupState());
|
||
5 years ago
|
putMsg(result, Status.HDFS_NOT_STARTUP);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
// verify udf func name exist
|
||
5 years ago
|
if (checkUdfFuncNameExists(funcName)) {
|
||
5 years ago
|
logger.error("udf func {} has exist, can't recreate", funcName);
|
||
|
putMsg(result, Status.UDF_FUNCTION_EXISTS);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
5 years ago
|
Resource resource = resourceMapper.selectById(resourceId);
|
||
5 years ago
|
if (resource == null) {
|
||
|
logger.error("resourceId {} is not exist", resourceId);
|
||
|
putMsg(result, Status.RESOURCE_NOT_EXIST);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
//save data
|
||
|
UdfFunc udf = new UdfFunc();
|
||
|
Date now = new Date();
|
||
|
udf.setUserId(loginUser.getId());
|
||
|
udf.setFuncName(funcName);
|
||
|
udf.setClassName(className);
|
||
|
if (StringUtils.isNotEmpty(argTypes)) {
|
||
|
udf.setArgTypes(argTypes);
|
||
|
}
|
||
|
if (StringUtils.isNotEmpty(argTypes)) {
|
||
|
udf.setDatabase(database);
|
||
|
}
|
||
|
udf.setDesc(desc);
|
||
|
udf.setResourceId(resourceId);
|
||
|
udf.setResourceName(resource.getAlias());
|
||
|
udf.setType(type);
|
||
|
|
||
|
udf.setCreateTime(now);
|
||
|
udf.setUpdateTime(now);
|
||
|
|
||
|
udfFuncMapper.insert(udf);
|
||
|
putMsg(result, Status.SUCCESS);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
5 years ago
|
/**
|
||
|
*
|
||
|
* @param name
|
||
|
* @return
|
||
|
*/
|
||
|
private boolean checkUdfFuncNameExists(String name){
|
||
|
List<UdfFunc> resource = udfFuncMapper.queryUdfByIdStr(null, name);
|
||
|
if(resource != null && resource.size() > 0){
|
||
|
return true;
|
||
|
}
|
||
|
return false;
|
||
|
}
|
||
|
|
||
5 years ago
|
|
||
|
/**
|
||
|
* query udf function
|
||
|
*/
|
||
|
public Map<String, Object> queryUdfFuncDetail(int id) {
|
||
|
|
||
|
Map<String, Object> result = new HashMap<>(5);
|
||
5 years ago
|
UdfFunc udfFunc = udfFuncMapper.selectById(id);
|
||
5 years ago
|
if (udfFunc == null) {
|
||
|
putMsg(result, Status.RESOURCE_NOT_EXIST);
|
||
|
return result;
|
||
|
}
|
||
|
result.put(Constants.DATA_LIST, udfFunc);
|
||
|
putMsg(result, Status.SUCCESS);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* updateProcessInstance udf function
|
||
|
*
|
||
|
* @param funcName
|
||
|
* @param argTypes
|
||
|
* @param database
|
||
|
* @param desc
|
||
|
* @param type
|
||
|
* @param resourceId
|
||
|
* @return
|
||
|
*/
|
||
|
public Map<String, Object> updateUdfFunc(int udfFuncId,
|
||
|
String funcName,
|
||
|
String className,
|
||
|
String argTypes,
|
||
|
String database,
|
||
|
String desc,
|
||
|
UdfType type,
|
||
|
int resourceId) {
|
||
|
Map<String, Object> result = new HashMap<>();
|
||
|
// verify udfFunc is exist
|
||
5 years ago
|
UdfFunc udf = udfFuncMapper.selectById(udfFuncId);
|
||
5 years ago
|
|
||
5 years ago
|
// if resource upload startup
|
||
|
if (!PropertyUtils.getResUploadStartupState()){
|
||
|
logger.error("resource upload startup state: {}", PropertyUtils.getResUploadStartupState());
|
||
5 years ago
|
putMsg(result, Status.HDFS_NOT_STARTUP);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
if (udf == null) {
|
||
|
result.put(Constants.STATUS, Status.UDF_FUNCTION_NOT_EXIST);
|
||
|
result.put(Constants.MSG, Status.UDF_FUNCTION_NOT_EXIST.getMsg());
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
// verify udfFuncName is exist
|
||
|
if (!funcName.equals(udf.getFuncName())) {
|
||
5 years ago
|
if (checkUdfFuncNameExists(funcName)) {
|
||
5 years ago
|
logger.error("UdfFunc {} has exist, can't create again.", funcName);
|
||
|
result.put(Constants.STATUS, Status.UDF_FUNCTION_EXISTS);
|
||
|
result.put(Constants.MSG, Status.UDF_FUNCTION_EXISTS.getMsg());
|
||
|
return result;
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
Resource resource = resourceMapper.selectById(resourceId);
|
||
5 years ago
|
if (resource == null) {
|
||
|
logger.error("resourceId {} is not exist", resourceId);
|
||
|
result.put(Constants.STATUS, Status.RESOURCE_NOT_EXIST);
|
||
|
result.put(Constants.MSG, Status.RESOURCE_NOT_EXIST.getMsg());
|
||
|
return result;
|
||
|
}
|
||
|
Date now = new Date();
|
||
|
udf.setFuncName(funcName);
|
||
|
udf.setClassName(className);
|
||
|
if (StringUtils.isNotEmpty(argTypes)) {
|
||
|
udf.setArgTypes(argTypes);
|
||
|
}
|
||
|
if (StringUtils.isNotEmpty(argTypes)) {
|
||
|
udf.setDatabase(database);
|
||
|
}
|
||
|
udf.setDesc(desc);
|
||
|
udf.setResourceId(resourceId);
|
||
|
udf.setResourceName(resource.getAlias());
|
||
|
udf.setType(type);
|
||
|
|
||
|
|
||
|
udf.setCreateTime(now);
|
||
|
udf.setUpdateTime(now);
|
||
|
|
||
5 years ago
|
udfFuncMapper.updateById(udf);
|
||
5 years ago
|
putMsg(result, Status.SUCCESS);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* query udf function list paging
|
||
|
*
|
||
|
* @param loginUser
|
||
|
* @param searchVal
|
||
|
* @param pageNo
|
||
|
* @param pageSize
|
||
|
* @return
|
||
|
*/
|
||
|
public Map<String, Object> queryUdfFuncListPaging(User loginUser, String searchVal, Integer pageNo, Integer pageSize) {
|
||
|
Map<String, Object> result = new HashMap<>(5);
|
||
|
|
||
|
|
||
|
PageInfo pageInfo = new PageInfo<Resource>(pageNo, pageSize);
|
||
5 years ago
|
IPage<UdfFunc> udfFuncList = getUdfFuncsPage(loginUser, searchVal, pageSize, pageNo);
|
||
|
pageInfo.setTotalCount((int)udfFuncList.getTotal());
|
||
|
pageInfo.setLists(udfFuncList.getRecords());
|
||
5 years ago
|
result.put(Constants.DATA_LIST, pageInfo);
|
||
|
putMsg(result, Status.SUCCESS);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* get udf functions
|
||
|
*
|
||
|
* @param loginUser
|
||
|
* @param searchVal
|
||
|
* @param pageSize
|
||
5 years ago
|
* @param pageNo
|
||
5 years ago
|
* @return
|
||
|
*/
|
||
5 years ago
|
private IPage<UdfFunc> getUdfFuncsPage(User loginUser, String searchVal, Integer pageSize, int pageNo) {
|
||
5 years ago
|
|
||
5 years ago
|
int userId = loginUser.getId();
|
||
5 years ago
|
if (isAdmin(loginUser)) {
|
||
5 years ago
|
userId = 0;
|
||
5 years ago
|
}
|
||
5 years ago
|
Page<UdfFunc> page = new Page(pageNo, pageSize);
|
||
|
return udfFuncMapper.queryUdfFuncPaging(page, userId, searchVal);
|
||
5 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* query data resource by type
|
||
|
*
|
||
|
* @param loginUser
|
||
|
* @param type
|
||
|
* @return
|
||
|
*/
|
||
|
public Map<String, Object> queryResourceList(User loginUser, Integer type) {
|
||
|
Map<String, Object> result = new HashMap<>(5);
|
||
|
List<UdfFunc> udfFuncList = udfFuncMapper.getUdfFuncByType(loginUser.getId(), type);
|
||
|
|
||
|
result.put(Constants.DATA_LIST, udfFuncList);
|
||
|
putMsg(result, Status.SUCCESS);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* delete udf function
|
||
|
*
|
||
|
* @param id
|
||
|
*/
|
||
|
@Transactional(value = "TransactionManager", rollbackFor = Exception.class)
|
||
|
public Result delete(int id) {
|
||
|
Result result = new Result();
|
||
|
|
||
5 years ago
|
udfFuncMapper.deleteById(id);
|
||
5 years ago
|
udfUserMapper.deleteByUdfFuncId(id);
|
||
|
putMsg(result, Status.SUCCESS);
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* verify udf function by name
|
||
|
*
|
||
|
* @param name
|
||
|
* @return
|
||
|
*/
|
||
|
public Result verifyUdfFuncByName(String name) {
|
||
|
Result result = new Result();
|
||
5 years ago
|
if (checkUdfFuncNameExists(name)) {
|
||
5 years ago
|
logger.error("UDF function name:{} has exist, can't create again.", name);
|
||
|
putMsg(result, Status.UDF_FUNCTION_EXISTS);
|
||
|
} else {
|
||
|
putMsg(result, Status.SUCCESS);
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
}
|