Search in sources :

Example 1 with TypedProperties

use of org.talend.utils.properties.TypedProperties in project tdq-studio-se by Talend.

the class DBViewerMain method main.

public static void main(java.lang.String[] args) {
    System.out.println("--- Database Viewer ---");
    try {
        TypedProperties connectionParams = PropertiesLoader.getProperties(DBViewerMain.class, "db.properties");
        assertNotNull("No properties found", connectionParams);
        String driverClassName = connectionParams.getProperty("driver");
        String dbUrl = connectionParams.getProperty("url");
        Connection con = ConnectionUtils.createConnection(dbUrl, driverClassName, connectionParams);
        DatabaseMetaData dbmd = con.getMetaData();
        System.out.println("Driver Name: " + dbmd.getDriverName());
        System.out.println("Database Product: " + dbmd.getDatabaseProductName());
        System.out.println("SQL Keywords Supported:");
        StringTokenizer st = new StringTokenizer(dbmd.getSQLKeywords(), ",");
        while (st.hasMoreTokens()) System.out.println(" " + st.nextToken());
        // Get a ResultSet that contains all of the tables in this database
        // We specify a table_type of "TABLE" to prevent seeing system tables,
        // views and so forth
        String[] tableTypes = { "TABLE" };
        ResultSet allTables = dbmd.getTables(null, null, null, tableTypes);
        while (allTables.next()) {
            String tableName = allTables.getString("TABLE_NAME");
            System.out.println("Table Name: " + tableName);
            System.out.println("Table Type: " + allTables.getString("TABLE_TYPE"));
            System.out.println("Indexes: ");
            // Get a list of all the indexes for this table
            ResultSet indexList = dbmd.getIndexInfo(null, null, tableName, false, false);
            while (indexList.next()) {
                System.out.println(" Index Name: " + indexList.getString("INDEX_NAME"));
                System.out.println(" Column Name:" + indexList.getString("COLUMN_NAME"));
            }
            indexList.close();
        }
        allTables.close();
        con.close();
    } catch (ClassNotFoundException e) {
        System.out.println("Unable to load database driver class");
    } catch (SQLException e) {
        System.out.println("SQL Exception: " + e.getMessage());
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        log.error(e, e);
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        log.error(e, e);
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) TypedProperties(org.talend.utils.properties.TypedProperties) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 2 with TypedProperties

use of org.talend.utils.properties.TypedProperties in project tdq-studio-se by Talend.

the class IndicatorEvaluationMain method main.

/**
 * DOC scorreia Comment method "main".
 *
 * @param args
 */
public static void main(String[] args) {
    TypedProperties connectionParams = PropertiesLoader.getProperties(IndicatorEvaluator.class, "db.properties");
    String driverClassName = connectionParams.getProperty("driver");
    String dbUrl = connectionParams.getProperty("url");
    try {
        TimeTracer tt = new TimeTracer("Indicator evaluation", null);
        tt.start();
        // create connection
        Connection connection = ConnectionUtils.createConnection(dbUrl, driverClassName, connectionParams);
        String database = "test";
        String tableName = "my_test";
        // --- columns to analyze
        String[] columnsArray = new String[] { // 0
        "my_int", // 1
        "my_double", // 2
        "my_text", // 4
        "my_date", // 3
        "my_string", // 5
        "my_int_null" };
        List<String> columns = Arrays.asList(columnsArray);
        // store in file
        File file = new File("out/columnTest_0.1.ana");
        EMFUtil util = new EMFUtil();
        Resource resource = util.getResourceSet().createResource(URI.createFileURI(file.getAbsolutePath()));
        rContents = resource.getContents();
        evaluator.setConnection(connection);
        // --- create indicators
        RowCountIndicator rowCountIndicator = IndicatorsFactory.eINSTANCE.createRowCountIndicator();
        NullCountIndicator nullCountIndicator = IndicatorsFactory.eINSTANCE.createNullCountIndicator();
        DistinctCountIndicator distinctCountIndicator = IndicatorsFactory.eINSTANCE.createDistinctCountIndicator();
        DistinctCountIndicator distinctCountIndicator2 = IndicatorsFactory.eINSTANCE.createDistinctCountIndicator();
        UniqueCountIndicator uniqueCountIndicator = IndicatorsFactory.eINSTANCE.createUniqueCountIndicator();
        DuplicateCountIndicator duplicateCountIndicator = IndicatorsFactory.eINSTANCE.createDuplicateCountIndicator();
        BlankCountIndicator blankCountIndicator = IndicatorsFactory.eINSTANCE.createBlankCountIndicator();
        MinLengthIndicator minLengthIndicator = IndicatorsFactory.eINSTANCE.createMinLengthIndicator();
        MaxLengthIndicator maxLengthIndicator = IndicatorsFactory.eINSTANCE.createMaxLengthIndicator();
        AverageLengthIndicator averageLengthIndicator = IndicatorsFactory.eINSTANCE.createAverageLengthIndicator();
        AverageLengthIndicator averageLengthIndicator2 = IndicatorsFactory.eINSTANCE.createAverageLengthIndicator();
        ModeIndicator modeIndicator = IndicatorsFactory.eINSTANCE.createModeIndicator();
        FrequencyIndicator textFrequencyIndicator = IndicatorsFactory.eINSTANCE.createFrequencyIndicator();
        // store in freq indic
        // textFrequencyIndicator.setDistinctCountIndicator(distinctCountIndicator);
        // textFrequencyIndicator.setDistinctCountIndicator(distinctCountIndicator2);
        // textFrequencyIndicator.setUniqueCountIndicator(uniqueCountIndicator);
        // textFrequencyIndicator.setDuplicateCountIndicator(duplicateCountIndicator);
        // textFrequencyIndicator.setModeIndicator(modeIndicator);
        MeanIndicator doubleMeanIndicator = IndicatorsFactory.eINSTANCE.createMeanIndicator();
        MeanIndicator integerMeanIndicator = IndicatorsFactory.eINSTANCE.createMeanIndicator();
        MedianIndicator medianIndicator = IndicatorsFactory.eINSTANCE.createMedianIndicator();
        SumIndicator integerSumIndicator = IndicatorsFactory.eINSTANCE.createSumIndicator();
        addIndicator(columnsArray[0], medianIndicator);
        addIndicator(columnsArray[1], doubleMeanIndicator);
        addIndicator(columnsArray[2], blankCountIndicator);
        addIndicator(columnsArray[5], nullCountIndicator);
        // addIndicator(columnsArray[2], textFrequencyIndicator);
        // addIndicator(columnsArray[2], distinctCountIndicator); // probably not useful?
        // addIndicator(columnsArray[2], uniqueCountIndicator); // probably not useful?
        // addIndicator(columnsArray[2], duplicateCountIndicator); // probably not useful?
        // addIndicator(columnsArray[2], modeIndicator); // probably not useful?
        addIndicator(columnsArray[3], rowCountIndicator);
        addIndicator(columnsArray[5], integerSumIndicator);
        addIndicator(columnsArray[5], integerMeanIndicator);
        addIndicator(columnsArray[2], averageLengthIndicator);
        addIndicator(columnsArray[3], averageLengthIndicator2);
        addIndicator(columnsArray[3], minLengthIndicator);
        addIndicator(columnsArray[3], maxLengthIndicator);
        // build query on columns
        // TODO scorreia add filter somewhere here...
        String selectCols = sqlSelectColumns(database, tableName, columns);
        // --- create a description of the column set
        QueryExpression queryExpression = DatatypesFactory.eINSTANCE.createQueryExpression();
        queryExpression.setBody(selectCols);
        // TODO scorreia externalize this as a constant
        queryExpression.setLanguage("SQL");
        tt.start("compute");
        evaluator.setFetchSize(10000);
        evaluator.evaluateIndicators(selectCols, true);
        tt.end("compute");
        // Print indicators the median
        System.out.println("Median=" + medianIndicator.getMedian());
        System.out.println("# Unique values= " + textFrequencyIndicator.getUniqueValueCount());
        System.out.println("# Distinct values= " + textFrequencyIndicator.getDistinctValueCount());
        for (String col : columns) {
            printIndicators(evaluator.getIndicators(col));
        }
        tt.start("save");
        util.save();
        tt.end("saved in " + file.getAbsolutePath());
        tt.end();
        CwmResource cwmR = (CwmResource) resource;
        String id = cwmR.getID(medianIndicator);
        System.out.println("ecore util.getId= " + EcoreUtil.getID(medianIndicator));
        System.out.println("uuId= " + id);
    // test reload this file
    // LoadSerialData.main(args);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        log.error(e, e);
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        log.error(e, e);
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        log.error(e, e);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        log.error(e, e);
    }
}
Also used : SumIndicator(org.talend.dataquality.indicators.SumIndicator) SQLException(java.sql.SQLException) BlankCountIndicator(org.talend.dataquality.indicators.BlankCountIndicator) MedianIndicator(org.talend.dataquality.indicators.MedianIndicator) UniqueCountIndicator(org.talend.dataquality.indicators.UniqueCountIndicator) ModeIndicator(org.talend.dataquality.indicators.ModeIndicator) NullCountIndicator(org.talend.dataquality.indicators.NullCountIndicator) FrequencyIndicator(org.talend.dataquality.indicators.FrequencyIndicator) DistinctCountIndicator(org.talend.dataquality.indicators.DistinctCountIndicator) RowCountIndicator(org.talend.dataquality.indicators.RowCountIndicator) QueryExpression(orgomg.cwm.foundation.datatypes.QueryExpression) MeanIndicator(org.talend.dataquality.indicators.MeanIndicator) CwmResource(org.talend.model.emf.CwmResource) DuplicateCountIndicator(org.talend.dataquality.indicators.DuplicateCountIndicator) MaxLengthIndicator(org.talend.dataquality.indicators.MaxLengthIndicator) Connection(java.sql.Connection) CwmResource(org.talend.model.emf.CwmResource) Resource(org.eclipse.emf.ecore.resource.Resource) MinLengthIndicator(org.talend.dataquality.indicators.MinLengthIndicator) TypedProperties(org.talend.utils.properties.TypedProperties) TimeTracer(org.talend.utils.time.TimeTracer) AverageLengthIndicator(org.talend.dataquality.indicators.AverageLengthIndicator) EMFUtil(org.talend.commons.emf.EMFUtil) File(java.io.File)

Example 3 with TypedProperties

use of org.talend.utils.properties.TypedProperties in project tdq-studio-se by Talend.

the class MultiColAnalysisCreationTest method getDataManager.

/**
 * DOC scorreia Comment method "getDataManager".
 *
 * @return
 */
public Connection getDataManager() {
    // $NON-NLS-1$
    TypedProperties connectionParams = PropertiesLoader.getProperties(AllDataProfilerCoreTests.class, "db.properties");
    // $NON-NLS-1$
    String driverClassName = connectionParams.getProperty("driver");
    // $NON-NLS-1$
    String dbUrl = connectionParams.getProperty("url");
    // $NON-NLS-1$
    String sqlTypeName = connectionParams.getProperty("sqlTypeName");
    DBConnectionParameter params = new DBConnectionParameter();
    // $NON-NLS-1$
    params.setName("My connection");
    params.setDriverClassName(driverClassName);
    params.setJdbcUrl(dbUrl);
    params.setSqlTypeName(sqlTypeName);
    params.setParameters(connectionParams);
    params.getParameters();
    // create connection
    ConnectionUtils.setTimeout(false);
    MetadataFillFactory instance = MetadataFillFactory.getDBInstance();
    IMetadataConnection metaConnection = instance.fillUIParams(ParameterUtil.toMap(params));
    ReturnCode rc = null;
    try {
        rc = instance.checkConnection(metaConnection);
    } catch (java.lang.RuntimeException e) {
        Assert.fail("connect to " + dbUrl + "failed," + e.getMessage());
    }
    Connection dataProvider = null;
    if (rc.isOk()) {
        dataProvider = instance.fillUIConnParams(metaConnection, null);
        dataProvider.setName(DATA_PROVIDER_NAME);
        // because the DI side code is changed, modify the following code.
        metaConnection.setCurrentConnection(dataProvider);
        try {
            ProjectNodeHelper.fillCatalogAndSchemas(metaConnection, (DatabaseConnection) dataProvider);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    Assert.assertNotNull(dataProvider);
    return dataProvider;
}
Also used : ReturnCode(org.talend.utils.sugars.ReturnCode) SQLException(java.sql.SQLException) DatabaseConnection(org.talend.core.model.metadata.builder.connection.DatabaseConnection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) Connection(org.talend.core.model.metadata.builder.connection.Connection) IMetadataConnection(org.talend.core.model.metadata.IMetadataConnection) TypedProperties(org.talend.utils.properties.TypedProperties) DBConnectionParameter(org.talend.dq.analysis.parameters.DBConnectionParameter) MetadataFillFactory(org.talend.metadata.managment.model.MetadataFillFactory)

Aggregations

SQLException (java.sql.SQLException)3 TypedProperties (org.talend.utils.properties.TypedProperties)3 Connection (java.sql.Connection)2 File (java.io.File)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 StringTokenizer (java.util.StringTokenizer)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 EMFUtil (org.talend.commons.emf.EMFUtil)1 IMetadataConnection (org.talend.core.model.metadata.IMetadataConnection)1 Connection (org.talend.core.model.metadata.builder.connection.Connection)1 DatabaseConnection (org.talend.core.model.metadata.builder.connection.DatabaseConnection)1 AverageLengthIndicator (org.talend.dataquality.indicators.AverageLengthIndicator)1 BlankCountIndicator (org.talend.dataquality.indicators.BlankCountIndicator)1 DistinctCountIndicator (org.talend.dataquality.indicators.DistinctCountIndicator)1 DuplicateCountIndicator (org.talend.dataquality.indicators.DuplicateCountIndicator)1 FrequencyIndicator (org.talend.dataquality.indicators.FrequencyIndicator)1 MaxLengthIndicator (org.talend.dataquality.indicators.MaxLengthIndicator)1 MeanIndicator (org.talend.dataquality.indicators.MeanIndicator)1 MedianIndicator (org.talend.dataquality.indicators.MedianIndicator)1