Search in sources :

Example 1 with BasicDataSourceFactory

use of com.qlangtech.tis.plugin.ds.BasicDataSourceFactory in project plugins by qlangtech.

the class InitWriterTable method process.

/**
 * 初始化表RDBMS的表,如果表不存在就创建表
 *
 * @param
 * @throws Exception
 */
public static void process(String dataXName, String tableName, List<String> jdbcUrls) throws Exception {
    if (StringUtils.isEmpty(dataXName)) {
        throw new IllegalArgumentException("param dataXName can not be null");
    }
    BasicDataXRdbmsWriter<BasicDataSourceFactory> dataXWriter = (BasicDataXRdbmsWriter<BasicDataSourceFactory>) DataxWriter.load(null, dataXName);
    Objects.requireNonNull(dataXWriter, "dataXWriter can not be null,dataXName:" + dataXName);
    boolean autoCreateTable = dataXWriter.autoCreateTable;
    if (autoCreateTable) {
        DataxProcessor processor = DataxProcessor.load(null, dataXName);
        File createDDL = new File(processor.getDataxCreateDDLDir(null), tableName + IDataxProcessor.DATAX_CREATE_DDL_FILE_NAME_SUFFIX);
        if (!createDDL.exists()) {
            throw new IllegalStateException("create table script is not exist:" + createDDL.getAbsolutePath());
        }
        BasicDataSourceFactory dsFactory = dataXWriter.getDataSourceFactory();
        String createScript = FileUtils.readFileToString(createDDL, TisUTF8.get());
        for (String jdbcUrl : jdbcUrls) {
            try (Connection conn = dsFactory.getConnection(jdbcUrl)) {
                List<String> tabs = Lists.newArrayList();
                dsFactory.refectTableInDB(tabs, conn);
                if (!tabs.contains(tableName)) {
                    // 表不存在
                    try (Statement statement = conn.createStatement()) {
                        logger.info("create table:{}\n   script:{}", tableName, createScript);
                        statement.execute(createScript);
                    }
                } else {
                    logger.info("table:{} already exist ,skip the create table step", tableName);
                }
            }
        }
    }
}
Also used : DataxProcessor(com.qlangtech.tis.datax.impl.DataxProcessor) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) BasicDataSourceFactory(com.qlangtech.tis.plugin.ds.BasicDataSourceFactory) Statement(java.sql.Statement) Connection(java.sql.Connection) File(java.io.File)

Example 2 with BasicDataSourceFactory

use of com.qlangtech.tis.plugin.ds.BasicDataSourceFactory in project plugins by qlangtech.

the class FlinkCDCMysqlSourceFunction method start.

@Override
public JobExecutionResult start(TargetResName dataxName, IDataxReader dataSource, List<ISelectedTab> tabs, IDataxProcessor dataXProcessor) throws MQConsumeException {
    try {
        // TabColIndexer colIndexer = new TabColIndexer(tabs);
        // TISDeserializationSchema deserializationSchema
        // = new TISDeserializationSchema(new MySQLSourceValConvert(colIndexer));
        TISDeserializationSchema deserializationSchema = new TISDeserializationSchema();
        BasicDataXRdbmsReader rdbmsReader = (BasicDataXRdbmsReader) dataSource;
        BasicDataSourceFactory dsFactory = (BasicDataSourceFactory) rdbmsReader.getDataSourceFactory();
        SourceChannel sourceChannel = new SourceChannel(SourceChannel.getSourceFunction(dsFactory, tabs, (dbHost, dbs, tbs, debeziumProperties) -> {
            DateTimeConverter.setDatetimeConverters(MySqlDateTimeConverter.class.getName(), debeziumProperties);
            String[] databases = dbs.toArray(new String[dbs.size()]);
            return Collections.singletonList(new ReaderSource(dbHost + ":" + dsFactory.port + ":" + dbs.stream().collect(Collectors.joining("_")), MySqlSource.<DTO>builder().hostname(dbHost).port(dsFactory.port).databaseList(// monitor all tables under inventory database
            databases).tableList(tbs.toArray(new String[tbs.size()])).serverTimeZone(BasicDataSourceFactory.DEFAULT_SERVER_TIME_ZONE.getId()).username(dsFactory.getUserName()).password(dsFactory.getPassword()).startupOptions(sourceFactory.getStartupOptions()).debeziumProperties(debeziumProperties).deserializer(// converts SourceRecord to JSON String
            deserializationSchema).build()));
        }));
        for (ISelectedTab tab : tabs) {
            sourceChannel.addFocusTab(tab.getName());
        }
        return (JobExecutionResult) getConsumerHandle().consume(dataxName, sourceChannel, dataXProcessor);
    } catch (Exception e) {
        throw new MQConsumeException(e.getMessage(), e);
    }
}
Also used : MQConsumeException(com.qlangtech.tis.async.message.client.consumer.MQConsumeException) DateTimeConverter(com.qlangtech.plugins.incr.flink.cdc.valconvert.DateTimeConverter) IConsumerHandle(com.qlangtech.tis.async.message.client.consumer.IConsumerHandle) TargetResName(com.qlangtech.tis.coredefine.module.action.TargetResName) BasicDataXRdbmsReader(com.qlangtech.tis.plugin.datax.common.BasicDataXRdbmsReader) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) DTO(com.qlangtech.tis.realtime.transfer.DTO) TISDeserializationSchema(com.qlangtech.plugins.incr.flink.cdc.TISDeserializationSchema) Collectors(java.util.stream.Collectors) BasicDataSourceFactory(com.qlangtech.tis.plugin.ds.BasicDataSourceFactory) SourceChannel(com.qlangtech.plugins.incr.flink.cdc.SourceChannel) ReaderSource(com.qlangtech.tis.realtime.ReaderSource) JobExecutionResult(org.apache.flink.api.common.JobExecutionResult) List(java.util.List) IDataxReader(com.qlangtech.tis.datax.IDataxReader) IAsyncMsgDeserialize(com.qlangtech.tis.async.message.client.consumer.IAsyncMsgDeserialize) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) IMQListener(com.qlangtech.tis.async.message.client.consumer.IMQListener) MySqlSource(com.ververica.cdc.connectors.mysql.MySqlSource) Collections(java.util.Collections) JobExecutionResult(org.apache.flink.api.common.JobExecutionResult) TISDeserializationSchema(com.qlangtech.plugins.incr.flink.cdc.TISDeserializationSchema) BasicDataSourceFactory(com.qlangtech.tis.plugin.ds.BasicDataSourceFactory) MQConsumeException(com.qlangtech.tis.async.message.client.consumer.MQConsumeException) SourceChannel(com.qlangtech.plugins.incr.flink.cdc.SourceChannel) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) BasicDataXRdbmsReader(com.qlangtech.tis.plugin.datax.common.BasicDataXRdbmsReader) ReaderSource(com.qlangtech.tis.realtime.ReaderSource) DTO(com.qlangtech.tis.realtime.transfer.DTO) MQConsumeException(com.qlangtech.tis.async.message.client.consumer.MQConsumeException)

Example 3 with BasicDataSourceFactory

use of com.qlangtech.tis.plugin.ds.BasicDataSourceFactory in project plugins by qlangtech.

the class TestTISFlinkCDCOracleSourceFunction method createMySqlDataSourceFactory.

protected BasicDataSourceFactory createMySqlDataSourceFactory(TargetResName dataxName) {
    Descriptor mySqlV5DataSourceFactory = TIS.get().getDescriptor("OracleDataSourceFactory");
    Assert.assertNotNull(mySqlV5DataSourceFactory);
    Descriptor.FormData formData = new Descriptor.FormData();
    formData.addProp("name", "oracle");
    formData.addProp("dbName", oracleContainer.getSid());
    formData.addProp("nodeDesc", oracleContainer.getHost());
    // formData.addProp("password", OracleTestUtils.ORACLE_PWD);
    // formData.addProp("userName", OracleTestUtils.ORACLE_USER);
    formData.addProp("password", oracleContainer.getPassword());
    formData.addProp("userName", oracleContainer.getUsername());
    formData.addProp("port", String.valueOf(oracleContainer.getOraclePort()));
    formData.addProp("allAuthorized", "true");
    formData.addProp("asServiceName", "false");
    Descriptor.ParseDescribable<BasicDataSourceFactory> parseDescribable = mySqlV5DataSourceFactory.newInstance(dataxName.getName(), formData);
    Assert.assertNotNull(parseDescribable.instance);
    return parseDescribable.instance;
}
Also used : BasicDataSourceFactory(com.qlangtech.tis.plugin.ds.BasicDataSourceFactory) Descriptor(com.qlangtech.tis.extension.Descriptor)

Example 4 with BasicDataSourceFactory

use of com.qlangtech.tis.plugin.ds.BasicDataSourceFactory in project plugins by qlangtech.

the class TestFlinkCDCPostgreSQLSourceFunction method createPGDataSourceFactory.

protected BasicDataSourceFactory createPGDataSourceFactory(TargetResName dataxName) {
    Descriptor pgDataSourceFactory = TIS.get().getDescriptor("PGDataSourceFactory");
    Assert.assertNotNull(pgDataSourceFactory);
    Descriptor.FormData formData = new Descriptor.FormData();
    formData.addProp("name", "pg");
    formData.addProp("dbName", POSTGERS_CONTAINER.getDatabaseName());
    formData.addProp("nodeDesc", POSTGERS_CONTAINER.getContainerIpAddress());
    formData.addProp("password", POSTGERS_CONTAINER.getPassword());
    formData.addProp("userName", POSTGERS_CONTAINER.getUsername());
    formData.addProp("port", String.valueOf(POSTGERS_CONTAINER.getMappedPort(PostgreSQLContainer.POSTGRESQL_PORT)));
    formData.addProp("tabSchema", schemaName);
    formData.addProp("encode", "utf8");
    // formData.addProp("useCompression", "true");
    Descriptor.ParseDescribable<BasicDataSourceFactory> parseDescribable = pgDataSourceFactory.newInstance(dataxName.getName(), formData);
    Assert.assertNotNull(parseDescribable.instance);
    return parseDescribable.instance;
}
Also used : BasicDataSourceFactory(com.qlangtech.tis.plugin.ds.BasicDataSourceFactory) Descriptor(com.qlangtech.tis.extension.Descriptor)

Example 5 with BasicDataSourceFactory

use of com.qlangtech.tis.plugin.ds.BasicDataSourceFactory in project plugins by qlangtech.

the class SourceChannel method getSourceFunction.

// https://ververica.github.io/flink-cdc-connectors/master/
public static List<ReaderSource> getSourceFunction(BasicDataSourceFactory dsFactory, Function<DBTable, String> tabnameCreator, List<ISelectedTab> tabs, ReaderSourceCreator sourceFunctionCreator) {
    try {
        DBConfig dbConfig = dsFactory.getDbConfig();
        List<ReaderSource> sourceFuncs = Lists.newArrayList();
        Map<String, List<String>> ip2dbs = Maps.newHashMap();
        Map<String, List<ISelectedTab>> db2tabs = Maps.newHashMap();
        dbConfig.vistDbName((config, ip, dbName) -> {
            List<String> dbs = ip2dbs.get(ip);
            if (dbs == null) {
                dbs = Lists.newArrayList();
                ip2dbs.put(ip, dbs);
            }
            dbs.add(dbName);
            if (db2tabs.get(dbName) == null) {
                db2tabs.put(dbName, tabs);
            }
            return false;
        });
        for (Map.Entry<String, List<String>> /**
         *dbs
         */
        entry : ip2dbs.entrySet()) {
            // Set<String> tbs = entry.getValue().stream().flatMap(
            // (dbName) -> db2tabs.get(dbName).stream().map((tab) -> dbName + "." + tab.getName())).collect(Collectors.toSet());
            Set<String> tbs = entry.getValue().stream().flatMap((dbName) -> db2tabs.get(dbName).stream().map((tab) -> {
                // return (dsSchemaSupport ? ((BasicDataSourceFactory.ISchemaSupported) dsFactory).getDBSchema() : dbName) + "." + tab.getName();
                return tabnameCreator.apply(new DBTable(dbName, tab));
            })).collect(Collectors.toSet());
            Properties debeziumProperties = new Properties();
            // do not use lock
            debeziumProperties.put("snapshot.locking.mode", "none");
            String dbHost = entry.getKey();
            List<String> dbs = entry.getValue();
            sourceFuncs.addAll(sourceFunctionCreator.create(dbHost, dbs, tbs, debeziumProperties));
        }
        return sourceFuncs;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : DBConfig(com.qlangtech.tis.plugin.ds.DBConfig) StringUtils(org.apache.commons.lang.StringUtils) Properties(java.util.Properties) Lists(org.apache.commons.compress.utils.Lists) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) Set(java.util.Set) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AsyncMsg(com.qlangtech.tis.async.message.client.consumer.AsyncMsg) BasicDataSourceFactory(com.qlangtech.tis.plugin.ds.BasicDataSourceFactory) ReaderSource(com.qlangtech.tis.realtime.ReaderSource) List(java.util.List) Map(java.util.Map) Properties(java.util.Properties) IOException(java.io.IOException) DBConfig(com.qlangtech.tis.plugin.ds.DBConfig) List(java.util.List) ReaderSource(com.qlangtech.tis.realtime.ReaderSource) Map(java.util.Map)

Aggregations

BasicDataSourceFactory (com.qlangtech.tis.plugin.ds.BasicDataSourceFactory)9 ISelectedTab (com.qlangtech.tis.plugin.ds.ISelectedTab)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 IMQListener (com.qlangtech.tis.async.message.client.consumer.IMQListener)4 MQConsumeException (com.qlangtech.tis.async.message.client.consumer.MQConsumeException)4 TargetResName (com.qlangtech.tis.coredefine.module.action.TargetResName)4 IDataxProcessor (com.qlangtech.tis.datax.IDataxProcessor)4 Descriptor (com.qlangtech.tis.extension.Descriptor)4 BasicDataXRdbmsReader (com.qlangtech.tis.plugin.datax.common.BasicDataXRdbmsReader)4 ReaderSource (com.qlangtech.tis.realtime.ReaderSource)4 JobExecutionResult (org.apache.flink.api.common.JobExecutionResult)4 SourceChannel (com.qlangtech.plugins.incr.flink.cdc.SourceChannel)3 TISDeserializationSchema (com.qlangtech.plugins.incr.flink.cdc.TISDeserializationSchema)3 IAsyncMsgDeserialize (com.qlangtech.tis.async.message.client.consumer.IAsyncMsgDeserialize)3 IConsumerHandle (com.qlangtech.tis.async.message.client.consumer.IConsumerHandle)3 IDataxReader (com.qlangtech.tis.datax.IDataxReader)3 DTO (com.qlangtech.tis.realtime.transfer.DTO)3 StringUtils (org.apache.commons.lang.StringUtils)3 DateTimeConverter (com.qlangtech.plugins.incr.flink.cdc.valconvert.DateTimeConverter)2