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();
}
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());
}
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);
}
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) {
}
}
}
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);
}
Aggregations