使用 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...

相关推荐
爬山算法2 天前
MongoDB(113)如何使用第三方工具进行MongoDB监控?
数据库·mongodb
qq_283720053 天前
Python3 模块精讲:pymongo(第三方)超详细教程 ——MongoDB 连接 + 全 CURD 实战
数据库·mongodb
杨云龙UP4 天前
Docker 部署 MongoDB 6.0 数据库每日自动备份实践:本地 + 异地保留 7 天_20260429
linux·运维·数据库·mongodb·docker·容器·centos
稳联技术老娜5 天前
Profinet转EtherCAT网关通讯架构及EtherCAT超距故障解决原理
mongodb
别来无恙blwy6 天前
windows MongoDB升级-自动升级脚本-自动检测升级到任意版本
数据库·windows·mongodb
snow@li6 天前
数据库-MongoDB:常用语法 / MongoDB 核心知识技能梳理
数据库·mongodb
刘晨鑫110 天前
MongoDB数据库应用
数据库·mongodb
爬山算法10 天前
MongoDB(92)什么是变更流(Change Streams)?
数据库·mongodb
小冯不疯10 天前
旺店通与金蝶云星空入库单管理对接方案
mongodb
MongoDB 数据平台10 天前
为编码代理引入 MongoDB 代理技能和插件
数据库·mongodb