MySQL数据库导入Elasticsearch

安装ES及交互插件

https://my.oschina.net/frylan/blog/639980

将已有的数据库导入ES

下载工具包

从以下地址下载elasticsearch-jdbc工具包(注意版本需要和ES版本对应)
https://github.com/jprante/elasticsearch-jdbc

配置导入脚本

将工具包解压缩到ES安装目录下,并修改路径$(es安装目录)\elasticsearch-jdbc\bin\mysql-simple-example.bat内容(注意备份原文件)

修改的地方重点在

  1. JDBC驱动配置(url,user,password)
  2. 需要导入的数据 (sql语句)
  3. 导出后ES中Index和type名字(index,type)

原文件

@echo off

set DIR=%~dp0
set LIB=%DIR%..\lib\*
set BIN=%DIR%..\bin

REM ???
echo {^
    "type" : "jdbc",^
    "jdbc" : {^
        "url" : "jdbc:mysql://localhost:3306/test",^
        "user" : "",^
        "password" : "",^
        "sql" :  "select *, page_id as _id from page",^
        "treat_binary_as_string" : true,^
        "elasticsearch" : {^
             "cluster" : "elasticsearch",^
             "host" : "localhost",^
             "port" : 9300^
        },^
        "index" : "metawiki"^
      }^
}^ | "%JAVA_HOME%\bin\java" -cp "%LIB%" -Dlog4j.configurationFile="%BIN%\log4j2.xml" "org.xbib.tools.Runner" "org.xbib.tools.JDBCImporter"

修改后文件

@echo off

set DIR=%~dp0
set LIB=%DIR%..\lib\*
set BIN=%DIR%..\bin

REM ???
echo {^
    "type" : "jdbc",^
    "jdbc" : {^
        "url" : "jdbc:mysql://localhost:3306/zhihu",^
        "user" : "root",^
        "password" : "123456",^
        "sql" :  "select user_id as id, user_name as name, user_avatar as avatar, user_short_description as description from user",^
        "treat_binary_as_string" : true,^
        "elasticsearch" : {^
             "cluster" : "my-application",^
             "host" : "localhost",^
             "port" : 9300^
        },^
        "index":"zhihu",^
        "type":"user"^
      }^
}^ | java -cp "%LIB%" -Dlog4j.configurationFile="%BIN%\log4j2.xml" "org.xbib.tools.Runner" "org.xbib.tools.JDBCImporter"

用cmd进入刚刚bin目录,运行mysql-simple-example.bat,即可完成导入

验证结果

在浏览区输入http://localhost:9200/_plugin/head/ 可以看到数据已经成功导入。

    原文作者:Cheava
    原文地址: https://www.jianshu.com/p/7fe17b89afd9
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞