Search in sources :

Example 1 with ESTableAlias

use of com.qlangtech.tis.datax.impl.ESTableAlias in project plugins by qlangtech.

the class TestElasticSearchSinkFactory method testCreateSinkFunction.

/**
 * 参考:ElasticsearchSinkTestBase
 *
 * @throws Exception
 */
@Test
public void testCreateSinkFunction() throws Exception {
    String tableName = "totalpayinfo";
    String colEntityId = "entity_id";
    String colNum = "num";
    String colId = "id";
    String colCreateTime = "create_time";
    IDataxProcessor dataxProcessor = mock("dataxProcessor", IDataxProcessor.class);
    IDataxReader dataxReader = mock("dataxReader", IDataxReader.class);
    List<ISelectedTab> selectedTabs = Lists.newArrayList();
    SelectedTab totalpayinfo = mock(tableName, SelectedTab.class);
    EasyMock.expect(totalpayinfo.getName()).andReturn(tableName);
    List<ISelectedTab.ColMeta> cols = Lists.newArrayList();
    ISelectedTab.ColMeta cm = new ISelectedTab.ColMeta();
    cm.setName(colEntityId);
    cm.setType(new DataType(Types.VARCHAR, 6));
    cols.add(cm);
    cm = new ISelectedTab.ColMeta();
    cm.setName(colNum);
    cm.setType(new DataType(Types.INTEGER));
    cols.add(cm);
    cm = new ISelectedTab.ColMeta();
    cm.setName(colId);
    cm.setType(new DataType(Types.VARCHAR, 32));
    cm.setPk(true);
    cols.add(cm);
    cm = new ISelectedTab.ColMeta();
    cm.setName(colCreateTime);
    cm.setType(new DataType(Types.BIGINT));
    cols.add(cm);
    EasyMock.expect(totalpayinfo.getCols()).andReturn(cols).anyTimes();
    selectedTabs.add(totalpayinfo);
    EasyMock.expect(dataxReader.getSelectedTabs()).andReturn(selectedTabs);
    EasyMock.expect(dataxProcessor.getReader(null)).andReturn(dataxReader);
    DataXElasticsearchWriter dataXWriter = mock("dataXWriter", DataXElasticsearchWriter.class);
    ESTableAlias esTableAlias = new ESTableAlias();
    dataXWriter.initialIndex(esTableAlias);
    EasyMock.expect(dataxProcessor.getWriter(null)).andReturn(dataXWriter);
    Map<String, IDataxProcessor.TableAlias> aliasMap = new HashMap<>();
    IDataxProcessor.TableAlias tab = new IDataxProcessor.TableAlias(tableName);
    aliasMap.put(tableName, tab);
    EasyMock.expect(dataxProcessor.getTabAlias()).andReturn(aliasMap);
    this.replay();
    ElasticSearchSinkFactory clickHouseSinkFactory = new ElasticSearchSinkFactory();
    Map<IDataxProcessor.TableAlias, SinkFunction<DTO>> sinkFuncs = clickHouseSinkFactory.createSinkFunction(dataxProcessor);
    Assert.assertTrue("sinkFuncs must > 0", sinkFuncs.size() > 0);
    // StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    // env.setParallelism(1);
    DTO d = new DTO();
    d.setTableName(tableName);
    d.setEventType(DTO.EventType.ADD);
    Map<String, Object> after = Maps.newHashMap();
    after.put(colEntityId, "334556");
    after.put(colNum, "5");
    after.put(colId, "123dsf124325253dsf123");
    after.put(colCreateTime, "20211113115959");
    d.setAfter(after);
    Assert.assertEquals(1, sinkFuncs.size());
    for (Map.Entry<IDataxProcessor.TableAlias, SinkFunction<DTO>> entry : sinkFuncs.entrySet()) {
        // env.fromElements(new DTO[]{d}).addSink(entry.getValue()).name("clickhouse");
        runElasticSearchSinkTest("elasticsearch-sink-test-json-index", entry.getValue());
        break;
    }
    // env.execute("testJob");
    Thread.sleep(5000);
    this.verifyAll();
    Client client = getClient();
}
Also used : HashMap(java.util.HashMap) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) DataXElasticsearchWriter(com.qlangtech.tis.plugin.datax.DataXElasticsearchWriter) IDataxReader(com.qlangtech.tis.datax.IDataxReader) SinkFunction(org.apache.flink.streaming.api.functions.sink.SinkFunction) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) SelectedTab(com.qlangtech.tis.plugin.datax.SelectedTab) DataType(com.qlangtech.tis.plugin.ds.DataType) Client(org.elasticsearch.client.Client) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) ESTableAlias(com.qlangtech.tis.datax.impl.ESTableAlias) ESTableAlias(com.qlangtech.tis.datax.impl.ESTableAlias) HashMap(java.util.HashMap) Map(java.util.Map) DTO(com.qlangtech.tis.realtime.transfer.DTO) Test(org.junit.Test)

Example 2 with ESTableAlias

use of com.qlangtech.tis.datax.impl.ESTableAlias in project plugins by qlangtech.

the class ElasticSearchSinkFactory method createSinkFunction.

@Override
public Map<IDataxProcessor.TableAlias, SinkFunction<DTO>> createSinkFunction(IDataxProcessor dataxProcessor) {
    DataXElasticsearchWriter dataXWriter = (DataXElasticsearchWriter) dataxProcessor.getWriter(null);
    Objects.requireNonNull(dataXWriter, "dataXWriter can not be null");
    IHttpToken token = dataXWriter.getToken();
    ESTableAlias esSchema = null;
    for (Map.Entry<String, IDataxProcessor.TableAlias> e : dataxProcessor.getTabAlias().entrySet()) {
        IDataxProcessor.TableAlias value = e.getValue();
        if (!(value instanceof ESTableAlias)) {
            throw new IllegalStateException("value must be type of 'ESTableAlias',but now is :" + value.getClass());
        }
        esSchema = (ESTableAlias) value;
        break;
    }
    Objects.requireNonNull(esSchema, "esSchema can not be null");
    List<ISelectedTab.ColMeta> cols = esSchema.getSourceCols();
    if (CollectionUtils.isEmpty(cols)) {
        throw new IllegalStateException("cols can not be null");
    }
    Optional<ISelectedTab.ColMeta> firstPK = cols.stream().filter((c) -> c.isPk()).findFirst();
    if (!firstPK.isPresent()) {
        throw new IllegalStateException("has not set PK col");
    }
    /**
     ******************************************************
     * 初始化索引Schema
     ******************************************************
     */
    dataXWriter.initialIndex(esSchema);
    // JSONArray schemaCols = esSchema.getSchemaCols();
    // ESClient esClient = new ESClient();
    // esClient.createClient(token.getEndpoint(),
    // token.getAccessKeyId(),
    // token.getAccessKeySecret(),
    // false,
    // 300000,
    // false,
    // false);
    // try {
    // esClient.createIndex(dataXWriter.getIndexName()
    // , dataXWriter.type
    // , esClient.genMappings(schemaCols, dataXWriter.type, (columnList) -> {
    // }), dataXWriter.settings, false);
    // } catch (Exception e) {
    // throw new RuntimeException(e);
    // } finally {
    // try {
    // esClient.closeJestClient();
    // } catch (Throwable e) {
    // 
    // }
    // }
    // if (!) {
    // throw new IllegalStateException("create index or mapping failed indexName:" + dataXWriter.getIndexName());
    // }
    // Map<String, String> config = new HashMap<>();
    // config.put("cluster.name", "my-cluster-name");
    // // This instructs the sink to emit after every element, otherwise they would be buffered
    // config.put("bulk.flush.max.actions", "1");
    List<HttpHost> transportAddresses = new ArrayList<>();
    transportAddresses.add(HttpHost.create(token.getEndpoint()));
    ElasticsearchSink.Builder<DTO> sinkBuilder = new ElasticsearchSink.Builder<>(transportAddresses, new DefaultElasticsearchSinkFunction(cols.stream().map((c) -> c.getName()).collect(Collectors.toSet()), firstPK.get().getName(), dataXWriter.getIndexName()));
    if (this.bulkFlushMaxActions != null) {
        sinkBuilder.setBulkFlushMaxActions(this.bulkFlushMaxActions);
    }
    if (this.bulkFlushMaxSizeMb != null) {
        sinkBuilder.setBulkFlushMaxSizeMb(bulkFlushMaxSizeMb);
    }
    if (this.bulkFlushIntervalMs != null) {
        sinkBuilder.setBulkFlushInterval(this.bulkFlushIntervalMs);
    }
    // new RestClientBuilder.HttpClientConfigCallback() {
    // @Override
    // public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
    // return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    // }
    // }
    sinkBuilder.setFailureHandler(new DefaultActionRequestFailureHandler());
    if (StringUtils.isNotEmpty(token.getAccessKeyId()) || StringUtils.isNotEmpty(token.getAccessKeySecret())) {
        // 如果用户设置了accessKey 或者accessSecret
        sinkBuilder.setRestClientFactory(new TISElasticRestClientFactory(token.getAccessKeyId(), token.getAccessKeySecret()));
    // sinkBuilder.setRestClientFactory(new RestClientFactory() {
    // @Override
    // public void configureRestClientBuilder(RestClientBuilder restClientBuilder) {
    // final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    // credentialsProvider.setCredentials(AuthScope.ANY,
    // new UsernamePasswordCredentials(token.getAccessKeyId(), token.getAccessKeySecret()));
    // restClientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
    // @Override
    // public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
    // return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
    // }
    // });
    // }
    // });
    }
    IDataxProcessor.TableAlias tableMapper = new IDataxProcessor.TableAlias();
    tableMapper.setTo(dataXWriter.getIndexName());
    IDataxReader reader = dataxProcessor.getReader(null);
    for (ISelectedTab selectedTab : reader.getSelectedTabs()) {
        tableMapper.setFrom(selectedTab.getName());
    }
    return Collections.singletonMap(tableMapper, sinkBuilder.build());
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) ActionRequestFailureHandler(org.apache.flink.streaming.connectors.elasticsearch.ActionRequestFailureHandler) java.util(java.util) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) RequestIndexer(org.apache.flink.streaming.connectors.elasticsearch.RequestIndexer) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) LoggerFactory(org.slf4j.LoggerFactory) FormField(com.qlangtech.tis.plugin.annotation.FormField) DTO(com.qlangtech.tis.realtime.transfer.DTO) ElasticsearchSinkFunction(org.apache.flink.streaming.connectors.elasticsearch.ElasticsearchSinkFunction) IndexRequest(org.elasticsearch.action.index.IndexRequest) CollectionUtils(org.apache.commons.collections.CollectionUtils) IDataXPluginMeta(com.qlangtech.tis.datax.IDataXPluginMeta) IDataxReader(com.qlangtech.tis.datax.IDataxReader) ESTableAlias(com.qlangtech.tis.datax.impl.ESTableAlias) Public(org.apache.flink.annotation.Public) Requests(org.elasticsearch.client.Requests) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) FormFieldType(com.qlangtech.tis.plugin.annotation.FormFieldType) Validator(com.qlangtech.tis.plugin.annotation.Validator) TISSinkFactory(com.qlangtech.tis.plugin.incr.TISSinkFactory) TISExtension(com.qlangtech.tis.extension.TISExtension) ActionRequest(org.elasticsearch.action.ActionRequest) ElasticsearchSink(org.apache.flink.streaming.connectors.elasticsearch7.ElasticsearchSink) Logger(org.slf4j.Logger) SinkFunction(org.apache.flink.streaming.api.functions.sink.SinkFunction) IHttpToken(com.qlangtech.tis.config.aliyun.IHttpToken) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) DataXElasticsearchWriter(com.qlangtech.tis.plugin.datax.DataXElasticsearchWriter) HttpHost(com.qlangtech.org.apache.http.HttpHost) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) DataXElasticsearchWriter(com.qlangtech.tis.plugin.datax.DataXElasticsearchWriter) IDataxReader(com.qlangtech.tis.datax.IDataxReader) HttpHost(com.qlangtech.org.apache.http.HttpHost) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) ESTableAlias(com.qlangtech.tis.datax.impl.ESTableAlias) IHttpToken(com.qlangtech.tis.config.aliyun.IHttpToken) ESTableAlias(com.qlangtech.tis.datax.impl.ESTableAlias) ElasticsearchSink(org.apache.flink.streaming.connectors.elasticsearch7.ElasticsearchSink) DTO(com.qlangtech.tis.realtime.transfer.DTO)

Example 3 with ESTableAlias

use of com.qlangtech.tis.datax.impl.ESTableAlias in project plugins by qlangtech.

the class DataXElasticsearchWriter method projectionFromExpertModel.

@Override
public ISchema projectionFromExpertModel(IDataxProcessor.TableAlias tableAlias, Consumer<byte[]> schemaContentConsumer) {
    ESTableAlias esTable = (ESTableAlias) tableAlias;
    schemaContentConsumer.accept(esTable.getSchemaByteContent());
    JSONObject body = new JSONObject();
    body.put("content", esTable.getSchemaContent());
    return this.projectionFromExpertModel(body);
}
Also used : ESTableAlias(com.qlangtech.tis.datax.impl.ESTableAlias) JSONObject(com.alibaba.fastjson.JSONObject)

Example 4 with ESTableAlias

use of com.qlangtech.tis.datax.impl.ESTableAlias in project plugins by qlangtech.

the class DataXElasticsearchWriter method initialIndex.

/**
 * 当增量开始执行前,先需要初始化一下索引实例
 *
 * @param esSchema
 */
public void initialIndex(ESTableAlias esSchema) {
    if (esSchema == null) {
        throw new IllegalArgumentException("param esSchema can not be null");
    }
    IHttpToken token = this.getToken();
    /**
     ******************************************************
     * 初始化索引Schema
     ******************************************************
     */
    JSONArray schemaCols = esSchema.getSchemaCols();
    ESClient esClient = new ESClient(ESInitialization.create(token.getEndpoint(), token.getAccessKeyId(), token.getAccessKeySecret(), false, 300000, false, false));
    try {
        esClient.createIndex(this.getIndexName(), this.type, esClient.genMappings(schemaCols, this.type, (columnList) -> {
        }), this.settings, false);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        try {
            esClient.closeJestClient();
        } catch (Throwable e) {
        }
    }
}
Also used : ISchemaField(com.qlangtech.tis.solrdao.ISchemaField) StringUtils(org.apache.commons.lang.StringUtils) ISchema(com.qlangtech.tis.solrdao.ISchema) ParamsConfig(com.qlangtech.tis.config.ParamsConfig) ISelectedTab(com.qlangtech.tis.plugin.ds.ISelectedTab) IDataxContext(com.qlangtech.tis.datax.IDataxContext) FormField(com.qlangtech.tis.plugin.annotation.FormField) Context(com.alibaba.citrus.turbine.Context) ESInitialization(com.alibaba.datax.plugin.writer.elasticsearchwriter.ESInitialization) IFieldErrorHandler(com.qlangtech.tis.runtime.module.misc.IFieldErrorHandler) JSONArray(com.alibaba.fastjson.JSONArray) SchemaMetaContent(com.qlangtech.tis.solrdao.SchemaMetaContent) VisualType(com.qlangtech.tis.runtime.module.misc.VisualType) ESTableAlias(com.qlangtech.tis.datax.impl.ESTableAlias) Map(java.util.Map) IDataxProcessor(com.qlangtech.tis.datax.IDataxProcessor) FormFieldType(com.qlangtech.tis.plugin.annotation.FormFieldType) Validator(com.qlangtech.tis.plugin.annotation.Validator) ESFieldType(com.alibaba.datax.plugin.writer.elasticsearchwriter.ESFieldType) TISExtension(com.qlangtech.tis.extension.TISExtension) ESClient(com.alibaba.datax.plugin.writer.elasticsearchwriter.ESClient) ImmutableMap(com.google.common.collect.ImmutableMap) IHttpToken(com.qlangtech.tis.config.aliyun.IHttpToken) DataxWriter(com.qlangtech.tis.datax.impl.DataxWriter) Maps(com.google.common.collect.Maps) Objects(java.util.Objects) Consumer(java.util.function.Consumer) DataXReaderColType(com.qlangtech.tis.plugin.ds.DataXReaderColType) ISearchEngineTypeTransfer(com.qlangtech.tis.datax.ISearchEngineTypeTransfer) JSON(com.alibaba.fastjson.JSON) IOUtils(com.qlangtech.tis.extension.impl.IOUtils) Public(com.qlangtech.tis.annotation.Public) Optional(java.util.Optional) JSONObject(com.alibaba.fastjson.JSONObject) StringEscapeUtils(org.apache.commons.lang.StringEscapeUtils) IHttpToken(com.qlangtech.tis.config.aliyun.IHttpToken) ESClient(com.alibaba.datax.plugin.writer.elasticsearchwriter.ESClient) JSONArray(com.alibaba.fastjson.JSONArray)

Example 5 with ESTableAlias

use of com.qlangtech.tis.datax.impl.ESTableAlias in project plugins by qlangtech.

the class TestDataXElasticsearchWriter method testTemplateGenerate.

public void testTemplateGenerate() throws Exception {
    DataxReader dataxReader = EasyMock.createMock("dataxReader", DataxReader.class);
    // List<ISelectedTab> selectedTabs = createSelectedTabs(dataxReader);
    // EasyMock.expect(dataxReader.getSelectedTabs()).andReturn(selectedTabs);
    final TestAliyunToken token = new TestAliyunToken("xxxxxxxxxxx", "accessKeykkkkkkkkkkkkkk");
    DataXElasticsearchWriter dataXWriter = new DataXElasticsearchWriter() {

        @Override
        public IHttpToken getToken() {
            return token;
        }

        @Override
        public String getTemplate() {
            return DataXElasticsearchWriter.getDftTemplate();
        }

        @Override
        public Class<?> getOwnerClass() {
            return DataXElasticsearchWriter.class;
        }
    };
    EasyMock.replay(dataxReader);
    dataXWriter.endpoint = "aliyun-bj-endpoint";
    dataXWriter.alias = "application2";
    dataXWriter.index = "application";
    dataXWriter.type = "specific_type";
    dataXWriter.cleanup = true;
    dataXWriter.batchSize = 9999;
    dataXWriter.trySize = 22;
    dataXWriter.timeout = 6666;
    dataXWriter.discovery = true;
    dataXWriter.compression = true;
    dataXWriter.multiThread = true;
    dataXWriter.ignoreParseError = true;
    dataXWriter.aliasMode = "append";
    dataXWriter.settings = "{\"index\" :{\"number_of_shards\": 1, \"number_of_replicas\": 0}}";
    dataXWriter.splitter = ",";
    dataXWriter.dynamic = true;
    ESTableAlias tableMap = new ESTableAlias();
    String esSchema = IOUtils.loadResourceFromClasspath(DataXElasticsearchWriter.class, "es-schema-content.json");
    tableMap.setSchemaContent(esSchema);
    WriterTemplate.valiateCfgGenerate("es-datax-writer-assert.json", dataXWriter, tableMap);
    token.accessKeyId = null;
    token.sccessKeySecret = null;
    WriterTemplate.valiateCfgGenerate("es-datax-writer-assert-without-option.json", dataXWriter, tableMap);
    EasyMock.verify(dataxReader);
}
Also used : ESTableAlias(com.qlangtech.tis.datax.impl.ESTableAlias) DataxReader(com.qlangtech.tis.datax.impl.DataxReader)

Aggregations

ESTableAlias (com.qlangtech.tis.datax.impl.ESTableAlias)5 IDataxProcessor (com.qlangtech.tis.datax.IDataxProcessor)3 ISelectedTab (com.qlangtech.tis.plugin.ds.ISelectedTab)3 JSONObject (com.alibaba.fastjson.JSONObject)2 IHttpToken (com.qlangtech.tis.config.aliyun.IHttpToken)2 IDataxReader (com.qlangtech.tis.datax.IDataxReader)2 TISExtension (com.qlangtech.tis.extension.TISExtension)2 FormField (com.qlangtech.tis.plugin.annotation.FormField)2 FormFieldType (com.qlangtech.tis.plugin.annotation.FormFieldType)2 Validator (com.qlangtech.tis.plugin.annotation.Validator)2 DataXElasticsearchWriter (com.qlangtech.tis.plugin.datax.DataXElasticsearchWriter)2 DTO (com.qlangtech.tis.realtime.transfer.DTO)2 Map (java.util.Map)2 StringUtils (org.apache.commons.lang.StringUtils)2 Context (com.alibaba.citrus.turbine.Context)1 ESClient (com.alibaba.datax.plugin.writer.elasticsearchwriter.ESClient)1 ESFieldType (com.alibaba.datax.plugin.writer.elasticsearchwriter.ESFieldType)1 ESInitialization (com.alibaba.datax.plugin.writer.elasticsearchwriter.ESInitialization)1 JSON (com.alibaba.fastjson.JSON)1 JSONArray (com.alibaba.fastjson.JSONArray)1