写入es有两种,一种是create请求,覆盖操作。一种update局部更新操作。本次讨论各种情况es写入的版本控制问题,以及flink1.6.x与es6.x的小插曲
1:测试不控制版本号的create覆盖操作
Requests.indexRequest() .index("test") .type("_doc") .id(inputjson.getString("serialid")) .source(JsonUtil.convert2Map(inputjson.toString().trim()));
es上数据
改变数据再写一次
可见不控制时每次写入操作都会是version自动加1: 此时与——score 里 version字段大小无关
2:对version进行指定写入该条version=2 的数据
写入
jsons.put("serialid","testcreate") ; jsons.put("value1","1") ; jsons.put("version","3") ;
Requests.indexRequest() .index("test") .type("version") .id(inputjson.getString("serialid")) .versionType(VersionType.EXTERNAL_GTE) .version(Long.valueOf(inputjson.getString(""))) .source(JsonUtil.convert2Map(inputjson.toString().trim()));
es数据
当我们指定的version比此时version3 要小时
version conflict, current version [3] is higher than the one provided [2]]] 会直接抛出version异常
(VersionType.EXTERNAL_GTE允许写入相等的数据,VersionType.EXTERNAL 对相等的verison也会抛出version异常不允许写入),
但是当我们指定的version 为null
经测试该条数据会被过过滤,不写入也不会抛异常,
当只指定versiontype 不指定version 时 异常不能正常写入illegal version value [-3] for version type [EXTERNAL_GTE];
注:若之前写入指定了version,后续又不指定versiontype与version 能正常写入,version仍在原基础上加1;
3:测试update操作
UpdateRequest updateRequest = new UpdateRequest() .index("assemble_train_grab_order").type("_doc").id(inputjson.getString("serialid")) .doc(JsonUtil.convert2Map(inputjson.toString().trim())).upsert(JsonUtil.convert2Map(inputjson.toString().trim())) .retryOnConflict(5);
能正常update但是verion也自动加1
指定version时
.versionType(VersionType.EXTERNAL_GTE) .version(Long.valueOf(inputjson.getString("version")))
直接抛异常
version type [EXTERNAL_GTE] is not supported by the update API;
可见局部更新不支持版本控制操作。
(不同版本类型控制)
inernal:若给定的版本与存储文档版本相同,则仅索引文档。
external or external_gt:若给定版本严格高于存储文档版本或没有现有文档,则仅索引文档。给定版本用作新版本,并与新文档一起存储,提供的版本必须是非负长号。
external_gte:仅在给定版本等于或高于存储文档的版本时索引文档。若没有现有文档,操作也将成功。给定版本将用作新版本,并将与新文档一起存储。提供版本必须是非负长号。
注:external_gte版本类型适用于特殊用例,使用不当会导致数据丢失。还有另一个选项,force已被弃用,因为它可能导致主分片和副本分片发散。
flink1.6.0写入es6.x时update诡异问题
<dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-elasticsearch6_${scala.binary.version}</artifactId> <version>1.6.0</version> <scope>compile</scope> </dependency>
此时进行update操作时有异常
Exception in thread "main" org.apache.flink.runtime.client.JobExecutionException: Job execution failed.
at org.apache.flink.runtime.jobmaster.JobResult.toJobExecutionResult(JobResult.java:146)
at org.apache.flink.runtime.minicluster.MiniCluster.executeJobBlocking(MiniCluster.java:630)
at org.apache.flink.streaming.api.environment.LocalStreamEnvironment.execute(LocalStreamEnvironment.java:123)
at TestJob2Es6.main(TestJob2Es6.java:104)
Caused by: java.lang.NoSuchMethodError: org.elasticsearch.action.bulk.BulkProcessor.add(Lorg/elasticsearch/action/ActionRequest;)Lorg/elasticsearch/action/bulk/BulkProcessor;
at org.apache.flink.streaming.connectors.elasticsearch.BulkProcessorIndexer.add(BulkProcessorIndexer.java:76)
查看异常BulkProcessor 没有接受ActionRequest类型的add方法
查看发现
确实没有,但是add方法接收的是DocWriteRequest ,并非ActionRequest 但是
是由UpdateRequest调用的。
UpdateRequest 同时继承了DocWriteRequest ,并非ActionRequest ,有点疑惑为何 只认ActionRequest ,报错找不到方法。
<dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-elasticsearch6_${scala.binary.version}</artifactId> <version>1.6.3</version> <scope>compile</scope>
</dependency> 解决了此问题