|
| 1 | +package com.alibaba.otter.canal.client.adapter.es8x; |
| 2 | + |
| 3 | +import com.alibaba.otter.canal.client.adapter.es.core.ESAdapter; |
| 4 | +import com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig; |
| 5 | +import com.alibaba.otter.canal.client.adapter.es8x.etl.ESEtlService; |
| 6 | +import com.alibaba.otter.canal.client.adapter.es8x.support.ES8xTemplate; |
| 7 | +import com.alibaba.otter.canal.client.adapter.es8x.support.ESConnection; |
| 8 | +import com.alibaba.otter.canal.client.adapter.support.DatasourceConfig; |
| 9 | +import com.alibaba.otter.canal.client.adapter.support.EtlResult; |
| 10 | +import com.alibaba.otter.canal.client.adapter.support.OuterAdapterConfig; |
| 11 | +import com.alibaba.otter.canal.client.adapter.support.SPI; |
| 12 | +import org.elasticsearch.action.search.SearchResponse; |
| 13 | + |
| 14 | +import javax.sql.DataSource; |
| 15 | +import java.util.LinkedHashMap; |
| 16 | +import java.util.List; |
| 17 | +import java.util.Map; |
| 18 | +import java.util.Properties; |
| 19 | + |
| 20 | +/** |
| 21 | + * ES 8.x 外部适配器 |
| 22 | + * |
| 23 | + * @author ymz 2013-02-23 |
| 24 | + * @version 1.0.0 |
| 25 | + */ |
| 26 | +@SPI("es8") |
| 27 | +public class ES8xAdapter extends ESAdapter { |
| 28 | + |
| 29 | + private ESConnection esConnection; |
| 30 | + |
| 31 | + public ESConnection getEsConnection() { |
| 32 | + return esConnection; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void init(OuterAdapterConfig configuration, Properties envProperties) { |
| 37 | + try { |
| 38 | + Map<String, String> properties = configuration.getProperties(); |
| 39 | + |
| 40 | + String[] hostArray = configuration.getHosts().split(","); |
| 41 | + esConnection = new ESConnection(hostArray, properties); |
| 42 | + |
| 43 | + this.esTemplate = new ES8xTemplate(esConnection); |
| 44 | + |
| 45 | + envProperties.put("es.version", "es8"); |
| 46 | + super.init(configuration, envProperties); |
| 47 | + } catch (Throwable e) { |
| 48 | + throw new RuntimeException(e); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public Map<String, Object> count(String task) { |
| 54 | + ESSyncConfig config = esSyncConfig.get(task); |
| 55 | + ESSyncConfig.ESMapping mapping = config.getEsMapping(); |
| 56 | + SearchResponse response = this.esConnection.new ESSearchRequest(mapping.get_index()).size(0).getResponse(); |
| 57 | + |
| 58 | + long rowCount = response.getHits().getTotalHits().value; |
| 59 | + Map<String, Object> res = new LinkedHashMap<>(); |
| 60 | + res.put("esIndex", mapping.get_index()); |
| 61 | + res.put("count", rowCount); |
| 62 | + return res; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public EtlResult etl(String task, List<String> params) { |
| 67 | + EtlResult etlResult = new EtlResult(); |
| 68 | + ESSyncConfig config = esSyncConfig.get(task); |
| 69 | + if (config != null) { |
| 70 | + DataSource dataSource = DatasourceConfig.DATA_SOURCES.get(config.getDataSourceKey()); |
| 71 | + ESEtlService esEtlService = new ESEtlService(esConnection, config); |
| 72 | + if (dataSource != null) { |
| 73 | + return esEtlService.importData(params); |
| 74 | + } else { |
| 75 | + etlResult.setSucceeded(false); |
| 76 | + etlResult.setErrorMessage("DataSource not found"); |
| 77 | + return etlResult; |
| 78 | + } |
| 79 | + } else { |
| 80 | + StringBuilder resultMsg = new StringBuilder(); |
| 81 | + boolean resSuccess = true; |
| 82 | + for (ESSyncConfig configTmp : esSyncConfig.values()) { |
| 83 | + // 取所有的destination为task的配置 |
| 84 | + if (configTmp.getDestination().equals(task)) { |
| 85 | + ESEtlService esEtlService = new ESEtlService(esConnection, configTmp); |
| 86 | + EtlResult etlRes = esEtlService.importData(params); |
| 87 | + if (!etlRes.getSucceeded()) { |
| 88 | + resSuccess = false; |
| 89 | + resultMsg.append(etlRes.getErrorMessage()).append("\n"); |
| 90 | + } else { |
| 91 | + resultMsg.append(etlRes.getResultMessage()).append("\n"); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + if (resultMsg.length() > 0) { |
| 96 | + etlResult.setSucceeded(resSuccess); |
| 97 | + if (resSuccess) { |
| 98 | + etlResult.setResultMessage(resultMsg.toString()); |
| 99 | + } else { |
| 100 | + etlResult.setErrorMessage(resultMsg.toString()); |
| 101 | + } |
| 102 | + return etlResult; |
| 103 | + } |
| 104 | + } |
| 105 | + etlResult.setSucceeded(false); |
| 106 | + etlResult.setErrorMessage("Task not found"); |
| 107 | + return etlResult; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void destroy() { |
| 112 | + super.destroy(); |
| 113 | + if (esConnection != null) { |
| 114 | + esConnection.close(); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments