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

相关推荐
AI全栈实验室5 天前
MongoDB迁移金仓踩了5个坑,最后一个差点回滚
mongodb
数据知道6 天前
MongoDB 元素查询运算符:使用 `$exists` 检查字段是否存在及处理缺失字段
数据库·mongodb
数据知道6 天前
MongoDB 批量写操作:`bulkWrite()` 在数据迁移与清洗中的高性能应用
数据库·mongodb
数据知道6 天前
MongoDB 数组更新操作符:`$push`、`$pull`、`$addToSet` 管理列表数据
数据库·mongodb
数据知道6 天前
MongoDB 更新操作符 `$set` 与 `$unset`:精准修改字段与删除字段
数据库·mongodb
数据知道6 天前
MongoDB 数值更新原子操作:`$inc` 实现点赞、计数器等高并发原子操作
数据库·算法·mongodb
数据知道7 天前
MongoDB 数组查询专项:`$all`、`$elemMatch` 与精确匹配数组的使用场景
数据库·mongodb
数据知道7 天前
MongoDB 正则表达式查询:在 MongoDB 中实现模糊搜索与索引优化陷阱
数据库·mongodb·正则表达式
正在走向自律7 天前
文档数据库替换新范式:金仓数据库MongoDB兼容性深度解析与实践指南
数据库·mongodb·国产数据库·金仓数据库
I'mAlex8 天前
金仓数据库平替MongoDB实操解析:多模融合赋能企业文档数据管理国产化升级
数据库·mongodb·kingbasees·金仓数据库