Search in sources :

Example 1 with ISQLExecutor

use of org.talend.cwm.db.connection.ISQLExecutor in project tdq-studio-se by Talend.

the class MatchAnalysisExecutor method execute.

/*
     * (non-Javadoc)
     * 
     * @see org.talend.dq.analysis.IAnalysisExecutor#execute(org.talend.dataquality.analysis.Analysis)
     */
public ReturnCode execute(Analysis analysis) {
    assert analysis != null;
    // --- preconditions
    ReturnCode rc = AnalysisExecutorHelper.check(analysis);
    if (!rc.isOk()) {
        AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
        return rc;
    }
    // --- creation time
    final long startime = AnalysisExecutorHelper.setExecutionDateInAnalysisResult(analysis);
    EList<Indicator> indicators = analysis.getResults().getIndicators();
    RecordMatchingIndicator recordMatchingIndicator = null;
    BlockKeyIndicator blockKeyIndicator = null;
    for (Indicator ind : indicators) {
        if (ind instanceof RecordMatchingIndicator) {
            recordMatchingIndicator = (RecordMatchingIndicator) ind;
        } else if (ind instanceof BlockKeyIndicator) {
            blockKeyIndicator = (BlockKeyIndicator) ind;
        }
    }
    if (recordMatchingIndicator == null || blockKeyIndicator == null) {
        rc.setOk(Boolean.FALSE);
        // $NON-NLS-1$
        rc.setMessage(Messages.getString("MatchAnalysisExecutor.noIndicators"));
        AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
        return rc;
    }
    List<ModelElement> anlayzedElements = analysis.getContext().getAnalysedElements();
    if (anlayzedElements == null || anlayzedElements.size() == 0) {
        rc.setOk(Boolean.FALSE);
        // $NON-NLS-1$
        rc.setMessage(Messages.getString("MatchAnalysisExecutor.EmptyAnalyzedElement"));
        AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
        return rc;
    }
    // TDQ-9664 msjian: check the store on disk path.
    Boolean isStoreOnDisk = TaggedValueHelper.getValueBoolean(SQLExecutor.STORE_ON_DISK_KEY, analysis);
    if (isStoreOnDisk) {
        String tempDataPath = TaggedValueHelper.getValueString(SQLExecutor.TEMP_DATA_DIR, analysis);
        File file = new File(tempDataPath);
        if (!file.exists() || !file.isDirectory()) {
            rc.setOk(Boolean.FALSE);
            // $NON-NLS-1$
            rc.setMessage(Messages.getString("MatchAnalysisExecutor.InvalidPath", file.getPath()));
            AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
            return rc;
        }
    }
    // TDQ-9664~
    Map<MetadataColumn, String> columnMap = getColumn2IndexMap(anlayzedElements);
    ISQLExecutor sqlExecutor = getSQLExectutor(analysis, recordMatchingIndicator, columnMap);
    if (sqlExecutor == null) {
        rc.setOk(Boolean.FALSE);
        // $NON-NLS-1$
        rc.setMessage(Messages.getString("MatchAnalysisExecutor.noSqlExecutor"));
        AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
        return rc;
    }
    if (getMonitor() != null) {
        getMonitor().worked(20);
    }
    // Set schema for match key.
    TypedReturnCode<MatchGroupResultConsumer> returnCode = new TypedReturnCode<MatchGroupResultConsumer>();
    MetadataColumn[] completeColumnSchema = AnalysisRecordGroupingUtils.getCompleteColumnSchema(columnMap);
    String[] colSchemaString = new String[completeColumnSchema.length];
    int idx = 0;
    for (MetadataColumn metadataCol : completeColumnSchema) {
        colSchemaString[idx++] = metadataCol.getName();
    }
    recordMatchingIndicator.setMatchRowSchema(colSchemaString);
    recordMatchingIndicator.reset();
    MatchGroupResultConsumer matchResultConsumer = createMatchGroupResultConsumer(recordMatchingIndicator);
    if (sqlExecutor.isStoreOnDisk()) {
        // need to execute the query
        try {
            sqlExecutor.executeQuery(analysis.getContext().getConnection(), analysis.getContext().getAnalysedElements());
        } catch (SQLException e) {
            log.error(e, e);
            rc.setOk(Boolean.FALSE);
            rc.setMessage(e.getMessage());
            AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
            return rc;
        }
        try {
            TypedReturnCode<Object> result = StoreOnDiskUtils.getDefault().executeWithStoreOnDisk(columnMap, recordMatchingIndicator, blockKeyIndicator, sqlExecutor.getStoreOnDiskHandler(), matchResultConsumer);
            if (result != null) {
                returnCode.setObject((MatchGroupResultConsumer) result.getObject());
                returnCode.setOk(result.isOk());
                returnCode.setMessage(result.getMessage());
            }
        } catch (Exception e) {
            log.error(e, e);
            returnCode.setMessage(e.getMessage());
            returnCode.setOk(false);
        }
    } else {
        // Added TDQ-9320 , use the result set iterator to replace the list of result in the memory.
        try {
            Iterator<Record> resultSetIterator = sqlExecutor.getResultSetIterator(analysis.getContext().getConnection(), anlayzedElements);
            BlockAndMatchManager bAndmManager = new BlockAndMatchManager(resultSetIterator, matchResultConsumer, columnMap, recordMatchingIndicator, blockKeyIndicator);
            bAndmManager.run();
        } catch (SQLException e) {
            log.error(e, e);
            rc.setOk(Boolean.FALSE);
            rc.setMessage(e.getMessage());
            AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
            return rc;
        } catch (BusinessException e) {
            log.error(e, e);
            rc.setOk(Boolean.FALSE);
            rc.setMessage(e.getMessage());
            AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
            return rc;
        }
    }
    if (!returnCode.isOk()) {
        rc.setOk(returnCode.isOk());
        rc.setMessage(returnCode.getMessage());
    }
    if (getMonitor() != null) {
        getMonitor().worked(20);
    }
    if (isLowMemory) {
        // $NON-NLS-1$
        rc.setMessage(Messages.getString("Evaluator.OutOfMomory", usedMemory));
    }
    // nodify the master page
    refreshTableWithMatchFullResult(analysis);
    // --- set metadata information of analysis
    AnalysisExecutorHelper.setExecutionInfoInAnalysisResult(analysis, rc.isOk(), rc.getMessage());
    // --- compute execution duration
    if (this.continueRun()) {
        long endtime = System.currentTimeMillis();
        final ExecutionInformations resultMetadata = analysis.getResults().getResultMetadata();
        resultMetadata.setExecutionDuration((int) (endtime - startime));
        resultMetadata.setOutThreshold(false);
    }
    if (getMonitor() != null) {
        getMonitor().worked(20);
    }
    return rc;
}
Also used : BlockKeyIndicator(org.talend.dataquality.indicators.columnset.BlockKeyIndicator) ISQLExecutor(org.talend.cwm.db.connection.ISQLExecutor) SQLException(java.sql.SQLException) ModelElement(orgomg.cwm.objectmodel.core.ModelElement) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) BusinessException(org.talend.commons.exception.BusinessException) Record(org.talend.dataquality.matchmerge.Record) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) ReturnCode(org.talend.utils.sugars.ReturnCode) RecordMatchingIndicator(org.talend.dataquality.indicators.columnset.RecordMatchingIndicator) RecordMatchingIndicator(org.talend.dataquality.indicators.columnset.RecordMatchingIndicator) Indicator(org.talend.dataquality.indicators.Indicator) BlockKeyIndicator(org.talend.dataquality.indicators.columnset.BlockKeyIndicator) SQLException(java.sql.SQLException) BusinessException(org.talend.commons.exception.BusinessException) ExecutionInformations(org.talend.dataquality.analysis.ExecutionInformations) MatchGroupResultConsumer(org.talend.dataquality.record.linkage.grouping.MatchGroupResultConsumer) TypedReturnCode(org.talend.utils.sugars.TypedReturnCode) BlockAndMatchManager(org.talend.dq.analysis.match.BlockAndMatchManager) File(java.io.File)

Example 2 with ISQLExecutor

use of org.talend.cwm.db.connection.ISQLExecutor in project tdq-studio-se by Talend.

the class MatchAnalysisExecutor method getSQLExectutor.

/**
 * getSQLExectutor .
 *
 * @param modelElement
 * @return
 */
private ISQLExecutor getSQLExectutor(Analysis analysis, RecordMatchingIndicator recordMatchingIndicator, Map<MetadataColumn, String> columnMap) {
    ModelElement modelElement = analysis.getContext().getAnalysedElements().get(0);
    ISQLExecutor sqlExecutor = null;
    if (modelElement instanceof TdColumn) {
        sqlExecutor = new DatabaseSQLExecutor();
    } else if (modelElement instanceof MetadataColumn) {
        sqlExecutor = new DelimitedFileSQLExecutor();
    }
    // Tune on store on disk option when needed.
    Boolean isStoreOnDisk = PluginChecker.isTDQLoaded() ? TaggedValueHelper.getValueBoolean(SQLExecutor.STORE_ON_DISK_KEY, analysis) : Boolean.FALSE;
    if (sqlExecutor != null && isStoreOnDisk) {
        sqlExecutor.setStoreOnDisk(Boolean.TRUE);
        sqlExecutor.initStoreOnDiskHandler(analysis, recordMatchingIndicator, columnMap);
    }
    return sqlExecutor;
}
Also used : ModelElement(orgomg.cwm.objectmodel.core.ModelElement) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) DatabaseSQLExecutor(org.talend.cwm.db.connection.DatabaseSQLExecutor) DelimitedFileSQLExecutor(org.talend.cwm.db.connection.DelimitedFileSQLExecutor) TdColumn(org.talend.cwm.relational.TdColumn) ISQLExecutor(org.talend.cwm.db.connection.ISQLExecutor)

Example 3 with ISQLExecutor

use of org.talend.cwm.db.connection.ISQLExecutor in project tdq-studio-se by Talend.

the class DataPreviewHandler method createPreviewData.

public List<Object[]> createPreviewData(ModelElement[] columns, int limit, boolean isRandomData) throws SQLException {
    // no columns be selected so that no data can be read
    if (columns == null || columns.length == 0) {
        return new ArrayList<Object[]>();
    }
    // use ModelElement instead of node to get the data source type directly.
    // get connection from column[0]
    boolean isDelimitedFile = false;
    ModelElement modelElement = columns[0];
    if (modelElement instanceof MetadataColumn && !(modelElement instanceof TdColumn)) {
        isDelimitedFile = true;
        connection = ConnectionHelper.getTdDataProvider((MetadataColumn) modelElement);
    } else if (modelElement instanceof TdColumn) {
        connection = ConnectionHelper.getTdDataProvider((TdColumn) modelElement);
    } else {
        // other case it is not support by now
        // $NON-NLS-1$
        log.warning(DefaultMessagesImpl.getString("DataPreviewHandler.UnSupportType"));
        return new ArrayList<Object[]>();
    }
    ISQLExecutor sqlExecutor = null;
    if (isDelimitedFile) {
        sqlExecutor = new DelimitedFileSQLExecutor();
    } else {
        // is database
        sqlExecutor = new DatabaseSQLExecutor();
    }
    // set limit
    sqlExecutor.setLimit(limit);
    sqlExecutor.setShowRandomData(isRandomData);
    return sqlExecutor.executeQuery(connection, Arrays.asList(columns), dataFilter);
}
Also used : ModelElement(orgomg.cwm.objectmodel.core.ModelElement) MetadataColumn(org.talend.core.model.metadata.builder.connection.MetadataColumn) DelimitedFileSQLExecutor(org.talend.cwm.db.connection.DelimitedFileSQLExecutor) DatabaseSQLExecutor(org.talend.cwm.db.connection.DatabaseSQLExecutor) TdColumn(org.talend.cwm.relational.TdColumn) ISQLExecutor(org.talend.cwm.db.connection.ISQLExecutor) ArrayList(java.util.ArrayList)

Aggregations

MetadataColumn (org.talend.core.model.metadata.builder.connection.MetadataColumn)3 ISQLExecutor (org.talend.cwm.db.connection.ISQLExecutor)3 ModelElement (orgomg.cwm.objectmodel.core.ModelElement)3 DatabaseSQLExecutor (org.talend.cwm.db.connection.DatabaseSQLExecutor)2 DelimitedFileSQLExecutor (org.talend.cwm.db.connection.DelimitedFileSQLExecutor)2 TdColumn (org.talend.cwm.relational.TdColumn)2 File (java.io.File)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 BusinessException (org.talend.commons.exception.BusinessException)1 ExecutionInformations (org.talend.dataquality.analysis.ExecutionInformations)1 Indicator (org.talend.dataquality.indicators.Indicator)1 BlockKeyIndicator (org.talend.dataquality.indicators.columnset.BlockKeyIndicator)1 RecordMatchingIndicator (org.talend.dataquality.indicators.columnset.RecordMatchingIndicator)1 Record (org.talend.dataquality.matchmerge.Record)1 MatchGroupResultConsumer (org.talend.dataquality.record.linkage.grouping.MatchGroupResultConsumer)1 BlockAndMatchManager (org.talend.dq.analysis.match.BlockAndMatchManager)1 ReturnCode (org.talend.utils.sugars.ReturnCode)1 TypedReturnCode (org.talend.utils.sugars.TypedReturnCode)1