Search in sources :

Example 11 with DataInfo

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

the class MetaDataClientImpl method getColumnByCsId.

@Override
public DataInfo<ColumnInfoDetail> getColumnByCsId(GetUserColumnByCsRequest request) throws MetaDataAcquireFailedException, UnExpectedRequestException {
    DataInfo<ColumnInfoDetail> result = new DataInfo<>();
    List<ColumnInfoDetail> list = new ArrayList<>();
    try {
        LOGGER.info("Start to get columns with context service ID and table's context key. csId: {}, contextKey: {}", request.getCsId(), request.getContextKey());
        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_COLUMN_PATH).toString();
        } else {
            url = getPath(clusterInfo.getLinkisAddress()).path(QUERY_CS_COLUMN_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("contextKey", request.getContextKey());
        } catch (JSONException e) {
            LOGGER.error("Failed to construct http body json, exception is : {}", e);
        }
        HttpEntity<Object> entity = new HttpEntity<>(jsonObject.toString(), headers);
        LOGGER.info("Start to get column with context service ID and table's context key 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 column with context service ID and table's context key by restful API. response: {}", response);
        Map<String, Object> data = (Map<String, Object>) response.get("data");
        List<Map<String, Object>> columns = (List<Map<String, Object>>) data.get("columns");
        if (columns == null || columns.size() == 0) {
            return result;
        }
        LOGGER.info("Successfully to get columns with context service ID and table's context key by restful API. csId: {}, contextKey: {}", request.getCsId(), request.getContextKey());
        for (Map<String, Object> column : columns) {
            ColumnInfoDetail columnInfoDetail = new ColumnInfoDetail();
            columnInfoDetail.setFieldName(column.get("columnName").toString());
            columnInfoDetail.setDataType(column.get("columnType").toString());
            columnInfoDetail.setColumnComment(column.get("columnComment") == null ? "" : column.get("columnComment").toString());
            columnInfoDetail.setPartitionField((Boolean) column.get("partitioned"));
            list.add(columnInfoDetail);
        }
        result.setTotalCount(columns.size());
        result.setContent(list);
    } catch (RestClientException e) {
        LOGGER.error(e.getMessage(), e);
        throw new MetaDataAcquireFailedException("Error! Can not get column by context service ID", 500);
    }
    return result;
}
Also used : DataInfo(com.webank.wedatasphere.qualitis.metadata.response.DataInfo) HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) ColumnInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.column.ColumnInfoDetail) 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

DataInfo (com.webank.wedatasphere.qualitis.metadata.response.DataInfo)11 ArrayList (java.util.ArrayList)10 ClusterInfo (com.webank.wedatasphere.qualitis.entity.ClusterInfo)7 MetaDataAcquireFailedException (com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException)7 Map (java.util.Map)6 JSONObject (org.json.JSONObject)6 HttpEntity (org.springframework.http.HttpEntity)6 HttpHeaders (org.springframework.http.HttpHeaders)6 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)5 ColumnInfoDetail (com.webank.wedatasphere.qualitis.metadata.response.column.ColumnInfoDetail)5 List (java.util.List)5 JSONException (org.json.JSONException)4 RestClientException (org.springframework.web.client.RestClientException)4 ClusterInfoDetail (com.webank.wedatasphere.qualitis.metadata.response.cluster.ClusterInfoDetail)3 CsTableInfoDetail (com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail)3 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)3 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)3 LinkisConfig (com.webank.wedatasphere.qualitis.config.LinkisConfig)2 LinkisResponseKeyEnum (com.webank.wedatasphere.qualitis.constant.LinkisResponseKeyEnum)2 SpecCharEnum (com.webank.wedatasphere.qualitis.constant.SpecCharEnum)2