Search in sources :

Example 1 with BasicDorisStarRocksWriter

use of com.qlangtech.tis.plugin.datax.BasicDorisStarRocksWriter in project plugins by qlangtech.

the class StarRocksSinkFactory method createSinkFunction.

@Override
public Map<IDataxProcessor.TableAlias, SinkFunction<DTO>> createSinkFunction(IDataxProcessor dataxProcessor) {
    Map<IDataxProcessor.TableAlias, SinkFunction<DTO>> sinkFuncs = Maps.newHashMap();
    IDataxProcessor.TableAlias tableName = null;
    // Map<String, IDataxProcessor.TableAlias> tabAlias = dataxProcessor.getTabAlias();
    BasicDorisStarRocksWriter dataXWriter = (BasicDorisStarRocksWriter) dataxProcessor.getWriter(null);
    Objects.requireNonNull(dataXWriter, "dataXWriter can not be null");
    BasicDorisStarRocksWriter.Separator separator = dataXWriter.getSeparator();
    IDataxReader reader = dataxProcessor.getReader(null);
    List<ISelectedTab> tabs = reader.getSelectedTabs();
    DorisSourceFactory dsFactory = dataXWriter.getDataSourceFactory();
    DBConfig dbConfig = dsFactory.getDbConfig();
    Map<String, IDataxProcessor.TableAlias> selectedTabs = dataxProcessor.getTabAlias();
    if (MapUtils.isEmpty(selectedTabs)) {
        throw new IllegalStateException("selectedTabs can not be empty");
    }
    for (Map.Entry<String, IDataxProcessor.TableAlias> tabAliasEntry : selectedTabs.entrySet()) {
        tableName = tabAliasEntry.getValue();
        Objects.requireNonNull(tableName, "tableName can not be null");
        if (StringUtils.isEmpty(tableName.getFrom())) {
            throw new IllegalStateException("tableName.getFrom() can not be empty");
        }
        AtomicReference<SinkFunction<DTO>> sinkFuncRef = new AtomicReference<>();
        final IDataxProcessor.TableAlias tabName = tableName;
        AtomicReference<Object[]> exceptionLoader = new AtomicReference<>();
        final String targetTabName = tableName.getTo();
        dbConfig.vistDbURL(false, (dbName, jdbcUrl) -> {
            try {
                Optional<ISelectedTab> selectedTab = tabs.stream().filter((tab) -> StringUtils.equals(tabName.getFrom(), tab.getName())).findFirst();
                if (!selectedTab.isPresent()) {
                    throw new IllegalStateException("target table:" + tabName.getFrom() + " can not find matched table in:[" + tabs.stream().map((t) -> t.getName()).collect(Collectors.joining(",")) + "]");
                }
                /**
                 * 需要先初始化表starrocks目标库中的表
                 */
                dataXWriter.initWriterTable(targetTabName, Collections.singletonList(jdbcUrl));
                sinkFuncRef.set(createSinkFunction(dbName, targetTabName, selectedTab.get(), jdbcUrl, dsFactory, separator));
            } catch (Throwable e) {
                exceptionLoader.set(new Object[] { jdbcUrl, e });
            }
        });
        if (exceptionLoader.get() != null) {
            Object[] error = exceptionLoader.get();
            throw new RuntimeException((String) error[0], (Throwable) error[1]);
        }
        Objects.requireNonNull(sinkFuncRef.get(), "sinkFunc can not be null");
        sinkFuncs.put(tableName, sinkFuncRef.get());
    }
    if (sinkFuncs.size() < 1) {
        throw new IllegalStateException("size of sinkFuncs can not be small than 1");
    }
    return sinkFuncs;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) AtomicDataType(org.apache.flink.table.types.AtomicDataType) java.util(java.util) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) Option(com.qlangtech.tis.manage.common.Option) FormField(com.qlangtech.tis.plugin.annotation.FormField) DTO(com.qlangtech.tis.realtime.transfer.DTO) StarRocksSinkOptions(com.starrocks.connector.flink.table.StarRocksSinkOptions) AtomicReference(java.util.concurrent.atomic.AtomicReference) Context(com.alibaba.citrus.turbine.Context) IFieldErrorHandler(com.qlangtech.tis.runtime.module.misc.IFieldErrorHandler) Lists(com.google.common.collect.Lists) StarRocksSinkOP(com.starrocks.connector.flink.row.StarRocksSinkOP) TextElement(org.apache.flink.configuration.description.TextElement) IDataXPluginMeta(com.qlangtech.tis.datax.IDataXPluginMeta) IDataxReader(com.qlangtech.tis.datax.IDataxReader) org.apache.flink.table.types.logical(org.apache.flink.table.types.logical) Public(org.apache.flink.annotation.Public) ConfigOption(org.apache.flink.configuration.ConfigOption) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) FormFieldType(com.qlangtech.tis.plugin.annotation.FormFieldType) DorisSourceFactory(com.qlangtech.tis.plugin.ds.doris.DorisSourceFactory) Validator(com.qlangtech.tis.plugin.annotation.Validator) TISSinkFactory(com.qlangtech.tis.plugin.incr.TISSinkFactory) StarRocksSink(com.starrocks.connector.flink.StarRocksSink) DBConfig(com.qlangtech.tis.plugin.ds.DBConfig) UnderlineUtils(com.qlangtech.tis.realtime.transfer.UnderlineUtils) TISExtension(com.qlangtech.tis.extension.TISExtension) SinkFunction(org.apache.flink.streaming.api.functions.sink.SinkFunction) MapUtils(org.apache.commons.collections.MapUtils) BasicDorisStarRocksWriter(com.qlangtech.tis.plugin.datax.BasicDorisStarRocksWriter) DataTypes(org.apache.flink.table.api.DataTypes) TableSchema(org.apache.flink.table.api.TableSchema) Field(java.lang.reflect.Field) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) DataType(com.qlangtech.tis.plugin.ds.DataType) StarRocksSinkSemantic(com.starrocks.connector.flink.table.StarRocksSinkSemantic) BlockElement(org.apache.flink.configuration.description.BlockElement) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) BasicDorisStarRocksWriter(com.qlangtech.tis.plugin.datax.BasicDorisStarRocksWriter) IDataxReader(com.qlangtech.tis.datax.IDataxReader) SinkFunction(org.apache.flink.streaming.api.functions.sink.SinkFunction) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) AtomicReference(java.util.concurrent.atomic.AtomicReference) DBConfig(com.qlangtech.tis.plugin.ds.DBConfig) DorisSourceFactory(com.qlangtech.tis.plugin.ds.doris.DorisSourceFactory)

Aggregations

Context (com.alibaba.citrus.turbine.Context)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 IDataXPluginMeta (com.qlangtech.tis.datax.IDataXPluginMeta)1 IDataxProcessor (com.qlangtech.tis.datax.IDataxProcessor)1 IDataxReader (com.qlangtech.tis.datax.IDataxReader)1 TISExtension (com.qlangtech.tis.extension.TISExtension)1 Option (com.qlangtech.tis.manage.common.Option)1 FormField (com.qlangtech.tis.plugin.annotation.FormField)1 FormFieldType (com.qlangtech.tis.plugin.annotation.FormFieldType)1 Validator (com.qlangtech.tis.plugin.annotation.Validator)1 BasicDorisStarRocksWriter (com.qlangtech.tis.plugin.datax.BasicDorisStarRocksWriter)1 DBConfig (com.qlangtech.tis.plugin.ds.DBConfig)1 DataType (com.qlangtech.tis.plugin.ds.DataType)1 ISelectedTab (com.qlangtech.tis.plugin.ds.ISelectedTab)1 DorisSourceFactory (com.qlangtech.tis.plugin.ds.doris.DorisSourceFactory)1 TISSinkFactory (com.qlangtech.tis.plugin.incr.TISSinkFactory)1 DTO (com.qlangtech.tis.realtime.transfer.DTO)1 UnderlineUtils (com.qlangtech.tis.realtime.transfer.UnderlineUtils)1 IFieldErrorHandler (com.qlangtech.tis.runtime.module.misc.IFieldErrorHandler)1