Search in sources :

Example 6 with ISourceDTO

use of com.dtstack.dtcenter.loader.dto.source.ISourceDTO in project Taier by DTStack.

the class DatasourceService method getTableColumnIncludePart.

/**
 * 查询表所属字段 可以选择是否需要分区字段
 * @param source
 * @param tableName
 * @param part 是否需要分区字段
 * @return
 * @throws Exception
 */
private List<JSONObject> getTableColumnIncludePart(BatchDataSource source, String tableName, Boolean part, String schema) {
    try {
        if (source == null) {
            throw new RdosDefineException(ErrorCode.CAN_NOT_FIND_DATA_SOURCE);
        }
        if (part == null) {
            part = false;
        }
        JSONObject dataJson = JSONObject.parseObject(source.getDataJson());
        Map<String, Object> kerberosConfig = fillKerberosConfig(source.getId());
        IClient iClient = ClientCache.getClient(source.getType());
        SqlQueryDTO sqlQueryDTO = SqlQueryDTO.builder().tableName(tableName).schema(schema).filterPartitionColumns(part).build();
        ISourceDTO iSourceDTO = SourceDTOType.getSourceDTO(dataJson, source.getType(), kerberosConfig, Maps.newHashMap());
        List<ColumnMetaDTO> columnMetaData = iClient.getColumnMetaData(iSourceDTO, sqlQueryDTO);
        List<JSONObject> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(columnMetaData)) {
            for (ColumnMetaDTO columnMetaDTO : columnMetaData) {
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(columnMetaDTO));
                jsonObject.put("isPart", columnMetaDTO.getPart());
                list.add(jsonObject);
            }
        }
        return list;
    } catch (DtCenterDefException e) {
        throw e;
    } catch (Exception e) {
        throw new RdosDefineException(ErrorCode.GET_COLUMN_ERROR, e);
    }
}
Also used : RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) ArrayList(java.util.ArrayList) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) IClient(com.dtstack.dtcenter.loader.client.IClient) SftpException(com.jcraft.jsch.SftpException) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) ColumnMetaDTO(com.dtstack.dtcenter.loader.dto.ColumnMetaDTO) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) SqlQueryDTO(com.dtstack.dtcenter.loader.dto.SqlQueryDTO) ISourceDTO(com.dtstack.dtcenter.loader.dto.source.ISourceDTO)

Example 7 with ISourceDTO

use of com.dtstack.dtcenter.loader.dto.source.ISourceDTO in project Taier by DTStack.

the class BatchTaskService method getTableColumnIncludePart.

/**
 * 查询表所属字段 可以选择是否需要分区字段
 * @param source
 * @param tableName
 * @param part 是否需要分区字段
 * @return
 * @throws Exception
 */
private List<JSONObject> getTableColumnIncludePart(BatchDataSource source, String tableName, Boolean part, String schema) {
    try {
        if (source == null) {
            throw new RdosDefineException(ErrorCode.CAN_NOT_FIND_DATA_SOURCE);
        }
        if (part == null) {
            part = false;
        }
        JSONObject dataJson = JSONObject.parseObject(source.getDataJson());
        Map<String, Object> kerberosConfig = fillKerberosConfig(source.getId());
        IClient iClient = ClientCache.getClient(source.getType());
        SqlQueryDTO sqlQueryDTO = SqlQueryDTO.builder().tableName(tableName).schema(schema).filterPartitionColumns(part).build();
        ISourceDTO iSourceDTO = SourceDTOType.getSourceDTO(dataJson, source.getType(), kerberosConfig, Maps.newHashMap());
        List<ColumnMetaDTO> columnMetaData = iClient.getColumnMetaData(iSourceDTO, sqlQueryDTO);
        List<JSONObject> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(columnMetaData)) {
            for (ColumnMetaDTO columnMetaDTO : columnMetaData) {
                JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(columnMetaDTO));
                jsonObject.put("isPart", columnMetaDTO.getPart());
                list.add(jsonObject);
            }
        }
        return list;
    } catch (DtCenterDefException e) {
        throw e;
    } catch (Exception e) {
        throw new RdosDefineException(ErrorCode.GET_COLUMN_ERROR, e);
    }
}
Also used : RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) ArrayList(java.util.ArrayList) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) IClient(com.dtstack.dtcenter.loader.client.IClient) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) ColumnMetaDTO(com.dtstack.dtcenter.loader.dto.ColumnMetaDTO) JSONObject(com.alibaba.fastjson.JSONObject) JSONObject(com.alibaba.fastjson.JSONObject) SqlQueryDTO(com.dtstack.dtcenter.loader.dto.SqlQueryDTO) ISourceDTO(com.dtstack.dtcenter.loader.dto.source.ISourceDTO)

Example 8 with ISourceDTO

use of com.dtstack.dtcenter.loader.dto.source.ISourceDTO in project Taier by DTStack.

the class DatasourceService method preview.

/**
 * 数据同步-获得预览数据,默认展示3条
 *
 * @param sourceId  数据源id
 * @param tableName 表名
 * @return
 * @author toutian
 */
public JSONObject preview(Long sourceId, String tableName, String schema) {
    BatchDataSource source = getOne(sourceId);
    StringBuffer newTableName = new StringBuffer();
    if (DataSourceType.SQLServer.getVal().equals(source.getType()) && StringUtils.isNotBlank(tableName)) {
        if (tableName.indexOf("[") == -1) {
            final String[] tableNames = tableName.split("\\.");
            for (final String name : tableNames) {
                newTableName.append("[").append(name).append("]").append(".");
            }
            tableName = newTableName.substring(0, newTableName.length() - 1);
        }
    }
    String dataJson = source.getDataJson();
    JSONObject json = JSON.parseObject(dataJson);
    // 获取字段信息
    List<String> columnList = new ArrayList<String>();
    // 获取数据
    List<List<String>> dataList = new ArrayList<List<String>>();
    try {
        Map<String, Object> kerberosConfig = fillKerberosConfig(source.getId());
        List<JSONObject> columnJson = getTableColumn(source, tableName, schema);
        if (CollectionUtils.isNotEmpty(columnJson)) {
            for (JSONObject columnMetaDTO : columnJson) {
                columnList.add(columnMetaDTO.getString("key"));
            }
        }
        IClient iClient = ClientCache.getClient(source.getType());
        ISourceDTO iSourceDTO = SourceDTOType.getSourceDTO(json, source.getType(), kerberosConfig, Maps.newHashMap());
        SqlQueryDTO sqlQueryDTO = SqlQueryDTO.builder().schema(schema).tableName(tableName).previewNum(3).build();
        dataList = iClient.getPreview(iSourceDTO, sqlQueryDTO);
        if (DataSourceType.getRDBMS().contains(source.getType())) {
            // 因为会把字段名也会返回 所以要去除第一行
            dataList = dataList.subList(1, dataList.size());
        }
    } catch (Exception e) {
        LOGGER.error("datasource preview end with error.", e);
        throw new RdosDefineException(String.format("%s获取预览数据失败", source.getDataName()), e);
    }
    JSONObject preview = new JSONObject(2);
    preview.put("columnList", columnList);
    preview.put("dataList", dataList);
    return preview;
}
Also used : BatchDataSource(com.dtstack.taier.dao.domain.BatchDataSource) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) ArrayList(java.util.ArrayList) IClient(com.dtstack.dtcenter.loader.client.IClient) SftpException(com.jcraft.jsch.SftpException) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) IOException(java.io.IOException) PubSvcDefineException(com.dtstack.taier.common.exception.PubSvcDefineException) DtCenterDefException(com.dtstack.taier.common.exception.DtCenterDefException) JSONObject(com.alibaba.fastjson.JSONObject) ArrayList(java.util.ArrayList) List(java.util.List) JSONObject(com.alibaba.fastjson.JSONObject) SqlQueryDTO(com.dtstack.dtcenter.loader.dto.SqlQueryDTO) ISourceDTO(com.dtstack.dtcenter.loader.dto.source.ISourceDTO)

Example 9 with ISourceDTO

use of com.dtstack.dtcenter.loader.dto.source.ISourceDTO in project Taier by DTStack.

the class DatasourceService method checkConnectionWithConf.

/**
 * 数据源连通性测试
 * @param source
 * @param confMap
 * @param localKerberosPath
 * @return
 */
public Boolean checkConnectionWithConf(DataSourceVO source, Map<String, Object> confMap, String localKerberosPath) {
    DataSourceTypeEnum typeEnum = DataSourceTypeEnum.typeVersionOf(source.getDataType(), source.getDataVersion());
    if (MapUtils.isEmpty(confMap) && source.getId() > 0L) {
        confMap = fillKerberosConfig(source.getId());
        localKerberosPath = kerberosService.getLocalKerberosPath(source.getId());
    }
    if (DataSourceTypeEnum.ADB_PostgreSQL == typeEnum) {
        typeEnum = DataSourceTypeEnum.PostgreSQL;
    }
    // 替换相对绝对路径
    Map<String, Object> tempConfMap = null;
    if (MapUtils.isNotEmpty(confMap)) {
        tempConfMap = Maps.newHashMap(confMap);
        IKerberos kerberos = ClientCache.getKerberos(typeEnum.getVal());
        kerberos.prepareKerberosForConnect(tempConfMap, localKerberosPath);
    }
    // 测试连通性
    ISourceDTO sourceDTO = SourceDTOType.getSourceDTO(source.getDataJson(), typeEnum.getVal(), tempConfMap, Maps.newHashMap());
    return ClientCache.getClient(typeEnum.getVal()).testCon(sourceDTO);
}
Also used : DataSourceTypeEnum(com.dtstack.taier.common.enums.DataSourceTypeEnum) JSONObject(com.alibaba.fastjson.JSONObject) IKerberos(com.dtstack.dtcenter.loader.client.IKerberos) ISourceDTO(com.dtstack.dtcenter.loader.dto.source.ISourceDTO)

Example 10 with ISourceDTO

use of com.dtstack.dtcenter.loader.dto.source.ISourceDTO in project Taier by DTStack.

the class DatasourceService method getTableInfo.

/**
 * 获取表信息
 *
 * @param sourceType
 * @param table
 * @param dataJson
 * @param kerberosConfig
 * @return
 */
private com.dtstack.dtcenter.loader.dto.Table getTableInfo(Integer sourceType, String table, JSONObject dataJson, Map<String, Object> kerberosConfig) {
    IClient client = ClientCache.getClient(sourceType);
    ISourceDTO sourceDTO = SourceDTOType.getSourceDTO(dataJson, sourceType, kerberosConfig, Maps.newHashMap());
    com.dtstack.dtcenter.loader.dto.Table tableInfo = client.getTable(sourceDTO, SqlQueryDTO.builder().tableName(table).build());
    return tableInfo;
}
Also used : IClient(com.dtstack.dtcenter.loader.client.IClient) ISourceDTO(com.dtstack.dtcenter.loader.dto.source.ISourceDTO)

Aggregations

ISourceDTO (com.dtstack.dtcenter.loader.dto.source.ISourceDTO)23 IClient (com.dtstack.dtcenter.loader.client.IClient)20 JSONObject (com.alibaba.fastjson.JSONObject)11 RdosDefineException (com.dtstack.taier.common.exception.RdosDefineException)7 ArrayList (java.util.ArrayList)7 ColumnMetaDTO (com.dtstack.dtcenter.loader.dto.ColumnMetaDTO)6 SqlQueryDTO (com.dtstack.dtcenter.loader.dto.SqlQueryDTO)6 DtCenterDefException (com.dtstack.taier.common.exception.DtCenterDefException)6 BatchDataSource (com.dtstack.taier.dao.domain.BatchDataSource)6 IOException (java.io.IOException)6 PubSvcDefineException (com.dtstack.taier.common.exception.PubSvcDefineException)5 SftpException (com.jcraft.jsch.SftpException)5 Map (java.util.Map)4 DataSourceType (com.dtstack.dtcenter.loader.source.DataSourceType)3 JdbcInfo (com.dtstack.taier.common.engine.JdbcInfo)3 List (java.util.List)3 Connection (java.sql.Connection)2 IKerberos (com.dtstack.dtcenter.loader.client.IKerberos)1 ITable (com.dtstack.dtcenter.loader.client.ITable)1 Table (com.dtstack.dtcenter.loader.dto.Table)1