欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > 同步数据至ES时,数据丢失问题处理

同步数据至ES时,数据丢失问题处理

2025/2/23 7:31:01 来源:https://blog.csdn.net/qq_39606051/article/details/144341035  浏览:    关键词:同步数据至ES时,数据丢失问题处理

问题背景:

数据同步到es过程中,发现数据丢失问题,原因正是因为写入索引前会先删除索引导致!

总体流程:

  • 使用别名索引E
  • redis获取当前索引B(即E指向B),获取新索引A
  • 初始化新索引A,将数据存储到新索引A
  • redis存储当前索引A,别名索引E指向A

代码实现:

  1. redis获取当前索引B
    public Class<?> getCurrentIndexByAlias(@NonNull String alias, Class<?> defaultClass) {String value = stringRedisTemplate.boundValueOps(INDEX_ALIAS +"_"+ alias).get();if (value == null) {return defaultClass;}return Class.forName(value);}
  1. 获取新索引A,存储数据
     Method method = currentIndex.getMethod("other");//执行方法获取新索引Class<?> newIndex = (Class<?>) method.invoke(currentIndex.getDeclaredConstructor().newInstance());//初始化索引initIndex(newIndex);//存储数据...........
  1. redis存储当前索引A
	redisRepository.switch2IndexOfAlias("E", newIndex);public void switch2IndexOfAlias(String alias, Class<?> clazz) {stringRedisTemplate.boundValueOps(INDEX_ALIAS +"_"+ alias).set(clazz.getName());}
  1. 别名索引E指向A
 esRepository.switchIndex("E",newIndex.getAnnotation(Document.class).indexName(),currentIndex.getAnnotation(Document.class).indexName());
    public <T> void switchIndex(String alias, String newIndexName, String oldIndexName) {AliasActions aliasActions = new AliasActions();//更新新索引if (existsIndex(newIndexName)) {AliasAction.Add addAction = new AliasAction.Add(AliasActionParameters.builder().withIndices(newIndexName).withAliases(alias).build());aliasActions.add(addAction);}//删除旧索引if (existsIndex(oldIndexName)) {AliasAction.Remove removeAction = new AliasAction.Remove(AliasActionParameters.builder().withIndices(oldIndexName).withAliases(alias).build());aliasActions.add(removeAction);}//绑定别名elasticsearchOperations.indexOps(IndexCoordinates.of(newIndexName)).alias(aliasActions);}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词