Search in sources :

Example 1 with CsTableInfoDetail

use of com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail in project Qualitis by WeBankFinTech.

the class RuleQueryServiceImpl method getColumnsByTableName.

@Override
public List<ColumnInfoDetail> getColumnsByTableName(String clusterName, Long datasourceId, String dbName, String tableName, String userName) throws UnExpectedRequestException, MetaDataAcquireFailedException {
    List<ColumnInfoDetail> result = null;
    User user = userDao.findByUsername(userName);
    if (user == null) {
        throw new UnExpectedRequestException(String.format("{&FAILED_TO_FIND_USER} %s", userName));
    }
    try {
        if (StringUtils.isBlank(dbName)) {
            List<Rule> rules = ruleDataSourceDao.findRuleByDataSource(clusterName, dbName, tableName, "%", userName);
            if (rules == null || rules.isEmpty()) {
                LOGGER.info("No rules for this datasource!");
                return null;
            }
            LOGGER.info("Rules related with context service table: [{}] are: {}", tableName, rules.toArray());
            String csId = rules.get(0).getCsId();
            String nodeName = rules.get(0).getName();
            GetUserTableByCsIdRequest getUserTableByCsIdRequest = new GetUserTableByCsIdRequest();
            getUserTableByCsIdRequest.setClusterName(clusterName);
            getUserTableByCsIdRequest.setLoginUser(userName);
            getUserTableByCsIdRequest.setNodeName(nodeName);
            getUserTableByCsIdRequest.setCsId(csId);
            DataInfo<CsTableInfoDetail> csTableInfoDetails = metaDataClient.getTableByCsId(getUserTableByCsIdRequest);
            if (csTableInfoDetails.getTotalCount() == 0) {
                LOGGER.info("Cannot find context service table with existed rules!");
                return null;
            }
            for (CsTableInfoDetail csTableInfoDetail : csTableInfoDetails.getContent()) {
                if (csTableInfoDetail.getTableName().equals(tableName)) {
                    GetUserColumnByCsRequest getUserColumnByCsRequest = new GetUserColumnByCsRequest();
                    getUserColumnByCsRequest.setClusterName(clusterName);
                    getUserColumnByCsRequest.setContextKey(csTableInfoDetail.getContextKey());
                    getUserColumnByCsRequest.setCsId(csId);
                    getUserColumnByCsRequest.setLoginUser(userName);
                    result = metaDataClient.getColumnByCsId(getUserColumnByCsRequest).getContent();
                    setRuleCount(result, user.getId(), clusterName, dbName, tableName);
                    break;
                } else {
                    continue;
                }
            }
        } else {
            if (datasourceId == null) {
                result = metaDataClient.getColumnInfo(clusterName, dbName, tableName, userName);
            } else {
                result = metaDataClient.getColumnsByDataSource(clusterName, userName, datasourceId, dbName, tableName).getContent();
            }
            setRuleCount(result, user.getId(), clusterName, dbName, tableName);
        }
        LOGGER.info("Datasource table number of columns: {}", result == null ? 0 : result.size());
    } catch (MetaDataAcquireFailedException e) {
        LOGGER.error("Datasource colums info {&DOES_NOT_EXIST}. Exception: {}", e);
        throw new MetaDataAcquireFailedException("Datasource colums info {&DOES_NOT_EXIST}. Exception: " + e.getMessage());
    } catch (UnExpectedRequestException e) {
        LOGGER.error("{&QUERY_PARAM_HAS_ERROR}. Exception: {}", e);
        throw new UnExpectedRequestException("{&QUERY_PARAM_HAS_ERROR}. Exception: " + e.getMessage());
    } catch (Exception e) {
        LOGGER.error("exception: {}", e);
    }
    return result;
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) User(com.webank.wedatasphere.qualitis.entity.User) ProjectUser(com.webank.wedatasphere.qualitis.project.entity.ProjectUser) ColumnInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.column.ColumnInfoDetail) GetUserTableByCsIdRequest(com.webank.wedatasphere.qualitis.metadata.request.GetUserTableByCsIdRequest) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) MetaDataAcquireFailedException(com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) CsTableInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail) MetaDataAcquireFailedException(com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) RuleQueryRule(com.webank.wedatasphere.qualitis.query.response.RuleQueryRule) GetUserColumnByCsRequest(com.webank.wedatasphere.qualitis.metadata.request.GetUserColumnByCsRequest)

Example 2 with CsTableInfoDetail

use of com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail in project Qualitis by WeBankFinTech.

the class OuterExecutionServiceImpl method checkDatasourceInContextService.

private void checkDatasourceInContextService(RuleDataSource ruleDataSource, Map<String, String> mappingCol, String clusterName, String userName, String nodeName, String csId) throws UnExpectedRequestException, MetaDataAcquireFailedException {
    GetUserTableByCsIdRequest getUserTableByCsIdRequest = new GetUserTableByCsIdRequest();
    getUserTableByCsIdRequest.setClusterName(StringUtils.isNotBlank(clusterName) ? clusterName : ruleDataSource.getClusterName());
    getUserTableByCsIdRequest.setLoginUser(userName);
    getUserTableByCsIdRequest.setCsId(csId);
    if (StringUtils.isBlank(nodeName)) {
        getUserTableByCsIdRequest.setNodeName(RuleConstraintEnum.DEFAULT_NODENAME.getValue());
    } else {
        getUserTableByCsIdRequest.setNodeName(nodeName);
    }
    DataInfo<CsTableInfoDetail> csTableInfoDetails = metaDataClient.getTableByCsId(getUserTableByCsIdRequest);
    if (csTableInfoDetails.getTotalCount() == 0) {
        LOGGER.info("Cannot find context service table with existed rules!");
        throw new UnExpectedRequestException("Table[" + ruleDataSource.getTableName() + "] {&RULE_DATASOURCE_BE_MOVED}");
    }
    for (CsTableInfoDetail csTableInfoDetail : csTableInfoDetails.getContent()) {
        if (csTableInfoDetail.getTableName().equals(ruleDataSource.getTableName())) {
            GetUserColumnByCsRequest getUserColumnByCsRequest = new GetUserColumnByCsRequest();
            getUserColumnByCsRequest.setClusterName(StringUtils.isNotBlank(clusterName) ? clusterName : ruleDataSource.getClusterName());
            getUserColumnByCsRequest.setContextKey(csTableInfoDetail.getContextKey());
            getUserColumnByCsRequest.setLoginUser(userName);
            getUserColumnByCsRequest.setCsId(csId);
            List<ColumnInfoDetail> cols = metaDataClient.getColumnByCsId(getUserColumnByCsRequest).getContent();
            if (ruleDataSource.getRule().getRuleType().equals(RuleTypeEnum.CUSTOM_RULE.getCode())) {
                continue;
            }
            if (!metaDataClient.fieldExist(ruleDataSource.getColName(), cols, mappingCol)) {
                throw new UnExpectedRequestException("Table[" + ruleDataSource.getTableName() + ":" + ruleDataSource.getColName() + "] {&RULE_DATASOURCE_BE_MOVED}");
            }
        }
    }
}
Also used : CsTableInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) GetUserColumnByCsRequest(com.webank.wedatasphere.qualitis.metadata.request.GetUserColumnByCsRequest) ColumnInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.column.ColumnInfoDetail) GetUserTableByCsIdRequest(com.webank.wedatasphere.qualitis.metadata.request.GetUserTableByCsIdRequest)

Example 3 with CsTableInfoDetail

use of com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail in project Qualitis by WeBankFinTech.

the class RuleDataSourceServiceImpl method saveCustomColumn.

private List<DataSourceColumnRequest> saveCustomColumn(String clusterName, String db, String table, String loginUser, String csId, String nodeName, String funcContent, boolean fps, boolean cs) throws UnExpectedRequestException, MetaDataAcquireFailedException {
    List<DataSourceColumnRequest> dataSourceColumnRequests = new ArrayList<>();
    List<ColumnInfoDetail> cols = new ArrayList<>();
    if (cs) {
        GetUserTableByCsIdRequest getUserTableByCsIdRequest = new GetUserTableByCsIdRequest();
        getUserTableByCsIdRequest.setClusterName(clusterName);
        getUserTableByCsIdRequest.setLoginUser(loginUser);
        getUserTableByCsIdRequest.setCsId(csId);
        getUserTableByCsIdRequest.setNodeName(nodeName);
        DataInfo<CsTableInfoDetail> csTableInfoDetails = metaDataClient.getTableByCsId(getUserTableByCsIdRequest);
        if (csTableInfoDetails.getTotalCount() == 0 && !fps) {
            LOGGER.info("Cannot find context service table with existed rules!");
            throw new UnExpectedRequestException("Table in sql {&DOES_NOT_EXIST}");
        }
        for (CsTableInfoDetail csTableInfoDetail : csTableInfoDetails.getContent()) {
            if (csTableInfoDetail.getTableName().equals(table)) {
                GetUserColumnByCsRequest getUserColumnByCsRequest = new GetUserColumnByCsRequest();
                getUserColumnByCsRequest.setClusterName(clusterName);
                getUserColumnByCsRequest.setContextKey(csTableInfoDetail.getContextKey());
                getUserColumnByCsRequest.setCsId(csId);
                getUserColumnByCsRequest.setLoginUser(loginUser);
                cols = metaDataClient.getColumnByCsId(getUserColumnByCsRequest).getContent();
                if (CollectionUtils.isEmpty(cols) && !fps) {
                    throw new UnExpectedRequestException("Table in sql {&DOES_NOT_EXIST}");
                }
            }
        }
    } else {
        cols = metaDataClient.getColumnInfo(clusterName, db, table, loginUser);
        if (CollectionUtils.isEmpty(cols) && !fps) {
            throw new UnExpectedRequestException("Table in sql {&DOES_NOT_EXIST}");
        }
    }
    // Save column info in use.
    for (ColumnInfoDetail detail : cols) {
        if (funcContent != null && funcContent.contains(detail.getFieldName())) {
            dataSourceColumnRequests.add(new DataSourceColumnRequest(detail.getFieldName(), detail.getDataType()));
        }
    }
    return dataSourceColumnRequests;
}
Also used : DataSourceColumnRequest(com.webank.wedatasphere.qualitis.rule.request.DataSourceColumnRequest) CsTableInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ArrayList(java.util.ArrayList) ColumnInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.column.ColumnInfoDetail) GetUserColumnByCsRequest(com.webank.wedatasphere.qualitis.metadata.request.GetUserColumnByCsRequest) GetUserTableByCsIdRequest(com.webank.wedatasphere.qualitis.metadata.request.GetUserTableByCsIdRequest)

Example 4 with CsTableInfoDetail

use of com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail in project Qualitis by WeBankFinTech.

the class MetaDataClientImpl method getTableByCsId.

@Override
public DataInfo<CsTableInfoDetail> getTableByCsId(GetUserTableByCsIdRequest request) throws MetaDataAcquireFailedException, UnExpectedRequestException {
    DataInfo<CsTableInfoDetail> result = new DataInfo<>();
    List<CsTableInfoDetail> csTableInfoDetailList = new ArrayList<>();
    try {
        LOGGER.info("Start to get tables with context service ID and node name by restful API. csId: {}, nodeName: {}", request.getCsId(), request.getNodeName());
        ClusterInfo clusterInfo = checkClusterNameExists(request.getClusterName());
        String authUser = request.getLoginUser();
        // send request
        String url;
        if (clusterInfo.getClusterType().endsWith(LINKIS_ONE_VERSION)) {
            url = getPath(clusterInfo.getLinkisAddress()).path(QUERY_WORKFLOW_TABLE_PATH).toString();
        } else {
            url = getPath(clusterInfo.getLinkisAddress()).path(QUERY_CS_TABLE_PATH).toString();
        }
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.add("Token-User", authUser);
        headers.add("Token-Code", clusterInfo.getLinkisToken());
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("contextID", request.getCsId());
            jsonObject.put("nodeName", request.getNodeName());
        } catch (JSONException e) {
            LOGGER.error(e.getMessage(), e);
            throw new UnExpectedRequestException("Failed to construct http body json with context ID and node name", 500);
        }
        HttpEntity<Object> entity = new HttpEntity<>(jsonObject.toString(), headers);
        LOGGER.info("Start to get table with context service ID and node name by restful API. url: {}, method: {}, body: {}", url, javax.ws.rs.HttpMethod.POST, entity);
        Map<String, Object> response = restTemplate.exchange(url, HttpMethod.POST, entity, Map.class).getBody();
        if (!checkResponse(response)) {
            String message = (String) response.get("message");
            LOGGER.error("Error! Can not get meta data from linkis, message: " + message);
            throw new MetaDataAcquireFailedException("Error! Can not get meta data from linkis, exception: " + message);
        }
        LOGGER.info("Finished to get table with context service ID and node name by restful API. response: {}", response);
        Map<String, Object> data = (Map<String, Object>) response.get("data");
        List<Map<String, Object>> tables = (List<Map<String, Object>>) data.get("tables");
        if (tables == null || tables.size() == 0) {
            return result;
        }
        LOGGER.info("Successfully to get tables with context service ID and node name by restful API. csId: {}, nodeName: {}, tables: {}", request.getCsId(), request.getNodeName(), tables);
        for (Map<String, Object> table : tables) {
            CsTableInfoDetail csTableInfoDetail = new CsTableInfoDetail();
            csTableInfoDetail.setTableName(table.get("tableName").toString());
            csTableInfoDetail.setContextKey(table.get("contextKey").toString());
            csTableInfoDetailList.add(csTableInfoDetail);
        }
        result.setContent(csTableInfoDetailList);
        result.setTotalCount(tables.size());
    } catch (RestClientException e) {
        LOGGER.error(e.getMessage(), e);
        throw new MetaDataAcquireFailedException("Error! Can not get tables by context service ID and node name", 500);
    }
    return result;
}
Also used : DataInfo(com.webank.wedatasphere.qualitis.metadata.response.DataInfo) HttpHeaders(org.springframework.http.HttpHeaders) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) CsTableInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail) ClusterInfo(com.webank.wedatasphere.qualitis.entity.ClusterInfo) JSONObject(org.json.JSONObject) MetaDataAcquireFailedException(com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException) RestClientException(org.springframework.web.client.RestClientException) JSONObject(org.json.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)4 CsTableInfoDetail (com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail)4 GetUserColumnByCsRequest (com.webank.wedatasphere.qualitis.metadata.request.GetUserColumnByCsRequest)3 GetUserTableByCsIdRequest (com.webank.wedatasphere.qualitis.metadata.request.GetUserTableByCsIdRequest)3 ColumnInfoDetail (com.webank.wedatasphere.qualitis.metadata.response.column.ColumnInfoDetail)3 MetaDataAcquireFailedException (com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException)2 ArrayList (java.util.ArrayList)2 ClusterInfo (com.webank.wedatasphere.qualitis.entity.ClusterInfo)1 User (com.webank.wedatasphere.qualitis.entity.User)1 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)1 DataInfo (com.webank.wedatasphere.qualitis.metadata.response.DataInfo)1 ProjectUser (com.webank.wedatasphere.qualitis.project.entity.ProjectUser)1 RuleQueryRule (com.webank.wedatasphere.qualitis.query.response.RuleQueryRule)1 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)1 DataSourceColumnRequest (com.webank.wedatasphere.qualitis.rule.request.DataSourceColumnRequest)1 List (java.util.List)1 Map (java.util.Map)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 HttpEntity (org.springframework.http.HttpEntity)1