mysql數(shù)據(jù)同步elasticsearch(es)全文檢索容器(Markdown版本)

一坠七、安裝ElasticSearch(下面統(tǒng)稱es,版本6.0.0,環(huán)境windows10)

直接上下載地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.0.0.zip

解壓后目錄如下:
1

啟動es灿渴,./bin/elasticsearch.bat孽鸡;啟動成功如圖

2

默認cluster_name是elasticsearch和端口9200可以修改笼痛,需要修改在config/elasticsearch.yml;上圖

3

二再膳、安裝logstash

下載地址:https://artifacts.elastic.co/downloads/logstash/logstash-6.0.0.zip

解壓目錄
4

先安裝logstash-input-jdbc插件
./bin/logstash-plugin.bat install logstash-input-jdbc

5

在logstash目錄下創(chuàng)建config-mysql啄清,見圖4

6

創(chuàng)建配置文件load_data.conf藏古,配置文件隨便取名晨继,可以創(chuàng)建sql文件烟阐,也可以在conf配置文件中定義,具體下面有說明

先上配置文件內(nèi)容
input {
    stdin {
    }
    jdbc {
      jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/jfinal_club?characterEncoding=utf8&useSSL=false"
      jdbc_user => "root"
      jdbc_password => "root"
      jdbc_driver_library => "D:/ELK/6.0.0/logstash-6.0.0/config-mysql/mysql-connector-java-5.1.43.jar"
      jdbc_driver_class => "com.mysql.jdbc.Driver"
      jdbc_paging_enabled => "true"
      jdbc_page_size => "50000"
      statement_filepath => "D:/ELK/6.0.0/logstash-6.0.0/config-mysql/store_list.sql"
      schedule => "* * * * *"
      use_column_value => false
      record_last_run => true
      last_run_metadata_path => "D:/ELK/6.0.0/logstash-6.0.0/config-mysql/run/store_list"
      type => "sl"
    }

jdbc {
      jdbc_connection_string => "jdbc:mysql://127.0.0.1:3306/jfinal_club?characterEncoding=utf8&useSSL=false"
      jdbc_user => "root"
      jdbc_password => "root"
      jdbc_driver_library => "D:/ELK/6.0.0/logstash-6.0.0/config-mysql/mysql-connector-java-5.1.43.jar"
      jdbc_driver_class => "com.mysql.jdbc.Driver"
      jdbc_paging_enabled => "true"
      jdbc_page_size => "50000"
      statement => "select * from store where updated > date_add(:sql_last_value, interval 8 hour)"
      schedule => "* * * * *"
      use_column_value => false
      record_last_run => true
      last_run_metadata_path => "D:/ELK/6.0.0/logstash-6.0.0/config-mysql/run/store_s"
      type => "st"
    }
}

filter {
    json {
        source => "message"
        remove_field => ["message"]
    }
}

output {

    if[type] == "sl"{
        elasticsearch {
              hosts => ["127.0.0.1:9200"]
              index => "store_list"
              document_type => "jdbc"
              document_id => "%{store_id}}"
        }
    }

    if[type] == "st"{
        elasticsearch {
              hosts => ["127.0.0.1:9200"]
              index => "store_st"
              document_type => "jdbc"
              document_id => "%{id}}"
        }
    }

    stdout {
        codec => json_lines
    }
}

字段解釋;具體的見:https://www.elastic.co/guide/en/logstash/current/plugins-inputs-jdbc.html

圖6中有個run目錄踱稍,在這里是用來存放:sql_last_value的時間值的
store_list.sql
7
先在es中生成index
PUT /store_list
{
    "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "jdbc": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "@version": {
          "type": "keyword"
        },
        "store_id": {
          "type": "long"
        },
        "store_name": {
          "type": "keyword"
        },
        "uid": {
          "type": "text"
        },
        "telephone": {
          "type": "text"
        },
        "street_id": {
          "type": "text"
        },
        "detail": {
          "type": "keyword"
        },
        "address": {
          "type": "keyword"
        },
        "store_created": {
          "type": "date"
        },
        "store_updated": {
          "type": "date"
        },
        "detail_id": {
          "type": "long"
        },
        "type_name": {
          "type": "text"
        },
        "tag": {
          "type": "keyword"
        },
        "overall_rating": {
          "type": "text"
        },
        "navi_location_lng": {
          "type": "double"
        },
        "navi_location_lat": {
          "type": "double"
        },
        "detail_url": {
          "type": "text"
        },
        "comment_num": {
          "type": "integer"
        },
        "detail_created": {
          "type": "date"
        },
        "detail_updated": {
          "type": "date"
        },
        "location_id": {
          "type": "long"
        },
        "lng": {
          "type": "double"
        },
        "lat": {
          "type": "double"
        }
      }
    }
  }
}

上面這種方式可以通過es管理工具執(zhí)行曲饱,比如kibana->dev tools;或者使用curl的方式也可以

curl -XPUT "http://localhost:9200/store_list" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1
  },
  "mappings": {
    "jdbc": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "@version": {
          "type": "keyword"
        },
        "store_id": {
          "type": "long"
        },
        "store_name": {
          "type": "keyword"
        },
        "uid": {
          "type": "text"
        },
        "telephone": {
          "type": "text"
        },
        "street_id": {
          "type": "text"
        },
        "detail": {
          "type": "keyword"
        },
        "address": {
          "type": "keyword"
        },
        "store_created": {
          "type": "date"
        },
        "store_updated": {
          "type": "date"
        },
        "detail_id": {
          "type": "long"
        },
        "type_name": {
          "type": "text"
        },
        "tag": {
          "type": "keyword"
        },
        "overall_rating": {
          "type": "text"
        },
        "navi_location_lng": {
          "type": "double"
        },
        "navi_location_lat": {
          "type": "double"
        },
        "detail_url": {
          "type": "text"
        },
        "comment_num": {
          "type": "integer"
        },
        "detail_created": {
          "type": "date"
        },
        "detail_updated": {
          "type": "date"
        },
        "location_id": {
          "type": "long"
        },
        "lng": {
          "type": "double"
        },
        "lat": {
          "type": "double"
        }
      }
    }
  }
}'

然后通過http://localhost:9200/store_list/查看字段生成情況

store_list就是index,相當于數(shù)據(jù)庫的database

8

然后回到logstash目錄下

執(zhí)行 nohup.exe ./bin/logstash.bat -f config-mysql/load_data.conf &

9

最好加上& 結(jié)尾,后臺運行

然后看數(shù)據(jù)庫同步情況

10

可能有些細節(jié)沒能寫全珠月,如果在集成中遇到什么情況扩淀,可以評論指出

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市啤挎,隨后出現(xiàn)的幾起案子驻谆,更是在濱河造成了極大的恐慌,老刑警劉巖庆聘,帶你破解...
    沈念sama閱讀 221,273評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件胜臊,死亡現(xiàn)場離奇詭異,居然都是意外死亡伙判,警方通過查閱死者的電腦和手機象对,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,349評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宴抚,“玉大人勒魔,你說我怎么就攤上這事」角” “怎么了冠绢?”我有些...
    開封第一講書人閱讀 167,709評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長常潮。 經(jīng)常有香客問我弟胀,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,520評論 1 296
  • 正文 為了忘掉前任孵户,我火速辦了婚禮萧朝,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘延届。我一直安慰自己剪勿,他們只是感情好贸诚,可當我...
    茶點故事閱讀 68,515評論 6 397
  • 文/花漫 我一把揭開白布方庭。 她就那樣靜靜地躺著,像睡著了一般酱固。 火紅的嫁衣襯著肌膚如雪械念。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,158評論 1 308
  • 那天运悲,我揣著相機與錄音龄减,去河邊找鬼。 笑死班眯,一個胖子當著我的面吹牛希停,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播署隘,決...
    沈念sama閱讀 40,755評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼宠能,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了磁餐?” 一聲冷哼從身側(cè)響起违崇,我...
    開封第一講書人閱讀 39,660評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎诊霹,沒想到半個月后羞延,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,203評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡脾还,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,287評論 3 340
  • 正文 我和宋清朗相戀三年伴箩,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鄙漏。...
    茶點故事閱讀 40,427評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡嗤谚,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出泥张,到底是詐尸還是另有隱情呵恢,我是刑警寧澤,帶...
    沈念sama閱讀 36,122評論 5 349
  • 正文 年R本政府宣布媚创,位于F島的核電站渗钉,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鳄橘,卻給世界環(huán)境...
    茶點故事閱讀 41,801評論 3 333
  • 文/蒙蒙 一声离、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧瘫怜,春花似錦术徊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,272評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至暗挑,卻和暖如春笋除,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背炸裆。 一陣腳步聲響...
    開封第一講書人閱讀 33,393評論 1 272
  • 我被黑心中介騙來泰國打工垃它, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人烹看。 一個月前我還...
    沈念sama閱讀 48,808評論 3 376
  • 正文 我出身青樓国拇,卻偏偏與公主長得像,于是被迫代替她去往敵國和親惯殊。 傳聞我的和親對象是個殘疾皇子酱吝,可洞房花燭夜當晚...
    茶點故事閱讀 45,440評論 2 359

推薦閱讀更多精彩內(nèi)容