|
21 | 21 | package com.dtstack.flink.sql.sink.elasticsearch; |
22 | 22 |
|
23 | 23 | import com.dtstack.flink.sql.table.AbstractTargetTableInfo; |
| 24 | +import com.google.common.base.Preconditions; |
24 | 25 | import org.apache.flink.api.common.typeinfo.TypeInformation; |
25 | 26 | import org.apache.flink.api.java.tuple.Tuple2; |
26 | 27 | import org.apache.flink.api.java.typeutils.RowTypeInfo; |
@@ -68,7 +69,7 @@ public class ElasticsearchSink implements RetractStreamTableSink<Row>, IStreamSi |
68 | 69 |
|
69 | 70 | private String type = ""; |
70 | 71 |
|
71 | | - private List<Integer> idIndexList; |
| 72 | + private List<String> idFiledNames; |
72 | 73 |
|
73 | 74 | protected String[] fieldNames; |
74 | 75 |
|
@@ -113,6 +114,10 @@ public TypeInformation<?>[] getFieldTypes() { |
113 | 114 |
|
114 | 115 | private RichSinkFunction createEsSinkFunction(){ |
115 | 116 |
|
| 117 | + // check whether id fields is exists in columns |
| 118 | + List<String> filedNamesLists = Arrays.asList(fieldNames); |
| 119 | + Preconditions.checkState(filedNamesLists.containsAll(idFiledNames), "elasticsearch5 type of id %s is should be exists in columns %s.", idFiledNames, filedNamesLists); |
| 120 | + CustomerSinkFunc customerSinkFunc = new CustomerSinkFunc(index, type, filedNamesLists, Arrays.asList(columnTypes), idFiledNames); |
116 | 121 |
|
117 | 122 | Map<String, String> userConfig = new HashMap<>(); |
118 | 123 | userConfig.put("cluster.name", clusterName); |
@@ -142,8 +147,6 @@ private RichSinkFunction createEsSinkFunction(){ |
142 | 147 | userConfig.put("xpack.security.user", authPassword); |
143 | 148 | } |
144 | 149 |
|
145 | | - CustomerSinkFunc customerSinkFunc = new CustomerSinkFunc(index, type, Arrays.asList(fieldNames), Arrays.asList(columnTypes), idIndexList); |
146 | | - |
147 | 150 | return new MetricElasticsearchSink(userConfig, transports, customerSinkFunc, esTableInfo); |
148 | 151 | } |
149 | 152 |
|
@@ -179,13 +182,13 @@ public ElasticsearchSink genStreamSink(AbstractTargetTableInfo targetTableInfo) |
179 | 182 | type = elasticsearchTableInfo.getEsType(); |
180 | 183 | String id = elasticsearchTableInfo.getId(); |
181 | 184 | String[] idField = StringUtils.split(id, ","); |
182 | | - idIndexList = new ArrayList<>(); |
| 185 | + idFiledNames = new ArrayList<>(); |
183 | 186 | registerTableName = elasticsearchTableInfo.getName(); |
184 | 187 | parallelism = Objects.isNull(elasticsearchTableInfo.getParallelism()) ? |
185 | 188 | parallelism : elasticsearchTableInfo.getParallelism(); |
186 | 189 |
|
187 | 190 | for(int i = 0; i < idField.length; ++i) { |
188 | | - idIndexList.add(Integer.valueOf(idField[i])); |
| 191 | + idFiledNames.add(String.valueOf(idField[i])); |
189 | 192 | } |
190 | 193 |
|
191 | 194 | columnTypes = elasticsearchTableInfo.getFieldTypes(); |
|
0 commit comments