使用 Logstash 迁移 MongoDB 数据到 Easysearch

大家好!在前面的文章中,我们已经详细介绍了如何通过 LogstashCanal 工具实现 MySQL 数据向 Easysearch 的迁移。如果您正在使用 MongoDB 作为数据存储,并希望将其中的数据迁移到 Easysearch 中,这篇指南或许能为您提供一些帮助。

为了实现 MongoDB 与 Easysearch 之间的数据迁移,Logstash 依然是一个非常成熟稳定的工具选择。在接下来的内容中,我们将介绍如何利用 Logstash 实现 MongoDB 到 Easysearch 的数据迁移。

MongoDB

MongoDB 作为一款功能强大的 NoSQL 数据库,与主流数据库系统一样支持 JDBC 连接方式。这意味着我们可以通过 Logstash 的 JDBC 插件实现与 MongoDB 的连接,从而将数据迁移至 Easysearch。

MongoDB 测试数据如下

Logstash

本次测试使用的 Logstash 版本为 8.17.4 。使用 JDBC 连接 MongoDB 数据库,要下载对应数据库的 MongoDB JDBC Driver,下载后解压。

测试配置文件 mongodb_to_easysearch.conf

plain 复制代码
input {
  jdbc{
    jdbc_driver_class => "Java::com.wisecoders.dbschema.mongodb.JdbcDriver"
    jdbc_driver_library => "/usr/share/logstash/driver/mongojdbc4.8.3.jar"
    jdbc_user => "user"
    jdbc_password => "usertest"
    jdbc_connection_string => "jdbc:mongodb://localhost:27017/test"
    statement => "db.collection_test.find({},{'_id': false})"
  }
}

filter {
    mutate {
        rename => {
            "[document][tags]" => "tags"
            "[document][type]" => "type"
            "[document][summary]" => "summary"
            "[document][username]" => "username"
            "[document][content]" => "content"
            "[document][category]" => "category"
            "[document][created]" => "created"
            "[document][url]" => "url"
            "[document][id]" => "id"
            "[document][title]" => "title"
        }
        remove_field => [ "document" ]
    }
}

output {
    elasticsearch {
        hosts => ["https://127.0.0.1:9200"]
        index => "mongo_doc"
        manage_template => false
        ssl_verification_mode => none
        user => "admin"
        password => "admin-test"
    }
}
  • jdbc_driver_library:指定解压后的 jar 包地址
  • statement:指定要迁移的 collection
  • filter:可选,默认会把源字段全部放到 document 字段下,改成符合 ES 的习惯
  • index:迁移数据进入的索引

运行 Logstash 进行数据迁移。

使用 INFINI Console 查看索引数据。可以看到目标数据已经成功迁移至 Easysearch。

作者:杨帆,极限科技(INFINI Labs)高级解决方案架构师、《老杨玩搜索》栏目 B 站 UP 主,拥有十余年金融行业服务工作经验,熟悉 Linux、数据库、网络等领域。目前主要从事 Easysearch、Elasticsearch 等搜索引擎的技术支持工作,服务国内私有化部署的客户。

原文:infinilabs.cn/blog/2025/m...

相关推荐
Wang's Blog3 小时前
Nestjs框架: 基于Mongodb的多租户功能集成和优化
数据库·mongodb·多租户
鼠鼠我捏,要死了捏3 小时前
深入解析MongoDB分片原理与运维实践指南
mongodb·性能优化·sharding
~央千澈~18 小时前
MongoDB数据库详解-针对大型分布式项目采用的原因以及基础原理和发展-卓伊凡|贝贝|莉莉
数据库·mongodb
csdn_aspnet2 天前
在 Windows 上安装设置 MongoDB及常见问题
windows·mongodb
Fireworkitte2 天前
MongoDB的操作
数据库·mongodb
Fireworkitte2 天前
MongoDB常用场景
数据库·mongodb
LiberInfo2 天前
MongoDB 副本集搭建与 Monstache 实时同步 Elasticsearch 全流程教程
数据库·mongodb·elasticsearch·搜索引擎·docker·kibana·monstache
~央千澈~4 天前
MongoDB频繁掉线频繁断开服务的核心原因以及解决方案-卓伊凡|贝贝|莉莉|糖果
大数据·mongodb·elasticsearch·搜索引擎
moxiaoran57535 天前
使用docker安装MongoDB
mongodb·docker·容器
TinpeaV5 天前
Elasticsearch / MongoDB / Redis / MySQL 区别
大数据·redis·mysql·mongodb·elasticsearch