elasticsearch使用river同步mysql數據


====== mysql的river介紹======
      - 什么是river?river代表es的一個數據源,也是其它存儲方式(如:數據庫)同步數據到es的一個方法。它是以插件方式存在的一個es服務,通過讀取river中的數據並把它索引到es中,官方的river有couchDB的,RabbitMQ的,Twitter的,Wikipedia的。這里主要研究針對mysql的river。
      - mysql的river插件:mysql的river安裝見https://github.com/jprante/elasticsearch-river-jdbc,就不具體介紹了。
      - 實踐測試:
      * 環境:
       服務器172.16.170.21   數據庫:profile 表user
      * 創建索引
        curl -XPUT 'http://localhost:9200/profile'
      * 創建數據表與索引映射
curl -XPUT 'http://localhost:9200/profile/user/_mapping' -d '
{
    "user": {
        "properties": {
            "id": {
                "type": "string",
                "store": "yes"
            },
            "name": {
                "type": "string",
                "store": "yes"
            },
            "login_name": {
                "type": "string",
                "store": "yes"
            }
        }
    }
}'


      * 運行river同步數據


curl -XPUT 'http://localhost:9200/_river/who_jdbc_river/_meta' -d '{
    "type": "jdbc",
    "jdbc": {
        "driver": "com.mysql.jdbc.Driver",
        "url": "jdbc:mysql://localhost:3306/profile",
        "user": "root",
        "password": "root",
        "sql": "select id as _id,name,login_name from user",
        "index": "profile",
        "type": "user",
        "bulk_size": 100,
        "max_bulk_requests": 30,
        "bulk_timeout": "10s",
        "flush_interval": "5s",
        "schedule": "0 0-59 0-23 ? * *"
    }
}'


      * 增量更新索引
增量更新,表需要維護時間戳,發現時間戳更新的列需要更新
curl -XPUT 'http://localhost:9200/_river/who_jdbc_river/_meta' -d '{
    "type": "jdbc",
    "jdbc": {
        "driver": "com.mysql.jdbc.Driver",
        "url": "jdbc:mysql://localhost:3306/profile",
        "user": "root",
        "password": "root",
        "sql": [
            {
                "statement": "select id as _id,name,login_name from user where mytimestamp > ?",
                "parameter": [
                    "$river.state.last_active_begin"
                ]
            }
        ],
        "index": "profile",
        "type": "user",
        "bulk_size": 100,
        "max_bulk_requests": 30,
        "bulk_timeout": "10s",
        "flush_interval": "5s",
        "schedule": "0 0-59 0-23 ? * *"
    }
}'



刪除river

curl -XDELETE 'localhost:9200/_river/report_jdbc_river'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM