Elasticsearch--Logstash定時同步MySQL數據到Elasticsearch

新地址體驗:http://www.zhouhong.icu/post/139

1、Logstash介紹

Logstash是elastic技術棧中的一個技術。它是一個數據採集引擎,能夠從數據庫採集數據到es中。咱們能夠經過設置自增id主鍵或者時間來控制數據的自動同步,這個id或者時間就是用於給logstash進行識別的html

id:假設如今有1000條數據,Logstatsh識別後會進行一次同步,同步完會記錄這個id爲1000,之後數據庫新增數據,那麼id會一直累加,Logstatsh會有定時任務,發現有id大於1000了,則增量加入到es中java

時間:同理,一開始同步1000條數據,每條數據都有一個字段,爲time,初次同步完畢後,記錄這個time,下次同步的時候進行時間比對,若是有超過這個時間的,那麼就能夠作同步,這裏能夠同步新增數據,或者修改元數據,由於同一條數據的時間更改會被識別,而id則不會。mysql

2、安裝Logstash

Elasticsearch安裝,集羣搭建請移步:nginx

ES安裝:http://www.zhouhong.icu/post/128git

ES集羣搭建、與SpringBoot整合:http://www.zhouhong.icu/post/138es6

準備:算法

  1. 數據庫:db_mblog
  2. 表:mto_post
  3. Logstatsh
  4. mysql-connector-java-8.0.11.jar 

數據準備:sql

CREATE TABLE `mto_post` (
    `id` DOUBLE ,
    `author_id` DOUBLE ,
    `channel_id` DOUBLE ,
    `comments` DOUBLE ,
    `created` DATETIME ,
    `favors` DOUBLE ,
    `featured` DOUBLE ,
    `status` DOUBLE ,
    `summary` VARCHAR (420),
    `tags` VARCHAR (192),
    `thumbnail` VARCHAR (384),
    `title` VARCHAR (192),
    `views` DOUBLE ,
    `weight` DOUBLE 
); 
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('35','1','1','0','2020-02-11 11:17:24','0','0','0','若是有來生,要作一棵樹, 站成永恆。沒有悲歡的姿式, 一半在塵土裏安詳, 一半在風裏飛揚; 一半灑落蔭涼, 一半沐浴陽光。 很是沉默、很是驕傲。 從不依靠、從不尋找。 若是有來生,要化成一陣風, 一瞬間也能成爲永恆。 沒有善感的情懷, 沒有多情的...','','//qiniusave.zhouhong.icu/3NSPTP433IDHNH7G9FLEL1ANU3.jpg','若是有來生---三毛','110','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('37','1','1','0','2020-02-13 13:51:34','2','0','0','從明天起,作一個幸福的人 餵馬、劈柴,周遊世界 從明天起,關心糧食和蔬菜 我有一所房子,面朝大海,春暖花開; 從明天起,和每個親人通訊 告訴他們個人幸福 那幸福的閃電告訴個人 我將告訴每個人; 給每一條河每一座山取一個溫暖的名字 陌生人,我也...','夏日','//qiniusave.zhouhong.icu/2VPB7PA4NFLG1MP764H1NRMILN.jpg','面朝大海,春暖花開','64','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('38','1','1','0','2020-02-21 13:55:27','1','0','0','人生有一首詩, 當咱們擁有它的時候, 每每並無讀懂它; 而當咱們可以讀懂它的時候, 它卻早已遠去, 這首詩的名字就叫 青春。 青春是那麼美好, 在這段不可複製的旅途當中, 咱們擁有獨一無二的記憶, 無論它是迷茫的、孤獨的、不安的, 仍是歡騰的、...','時光','//qiniusave.zhouhong.icu/1VHFIK2DVSGUCJNPDL707NEBIN.jpg','青春','95','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('39','1','5','0','2021-01-15 14:45:42','0','1','0','>歡迎訪問個人博客園:Tom-shushu的博客園 5六、關於緩存穿透,緩存擊穿,緩存雪崩,熱點數據失效問題的解決方案 5五、CENTOS7下用FASTDFS搭建圖片服務器 5四、JAVA實現PDF和EXCEL生成和數據動態插入以及導出 5...','博客園搬家','//qiniusave.zhouhong.icu/GJEPCOMLNME5EVODJVRQV1EL.jpg','個人博客園博文搬新家啦^_^','224','3');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('40','1','1','1','2020-03-21 20:59:27','0','0','0','我有時喜歡一我的行走。閒暇之餘,可以不作閒人,卻又不可以沒有閒情。這樣一來,我有時就喜歡一我的行走。在這個喧囂的世界裏,惟有一我的行走纔沒有羈絆束縛;惟有一我的行走才能感受到本身真實的心跳;惟有一我的行走才能放縱屬於本身的那份情緒。在一我的中享受...','微笑','//qiniusave.zhouhong.icu/QL7IAJGT9I3MIC7QNAEVI80FP.jpg','獨處--領悟','207','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('43','1','5','0','2020-04-19 02:46:56','0','1','0','關於緩存穿透,緩存擊穿,緩存雪崩,熱點數據失效問題的解決方案 1.咱們使用緩存時的業務流程大概爲:  當咱們查詢一條數據時,先去查詢緩存,若是緩存有就直接返回,若是沒有就去查詢數據庫,而後返回。這種狀況下就可能出現下面的一些現象。 2.緩存穿透 ...','Redis 緩存,博客園遷移','//qiniusave.zhouhong.icu/13I57KJ8PRII7RIQIP60F5RTFA.jpg','關於緩存穿透,緩存擊穿,緩存雪崩,熱點數據失效問題的解決方案','30','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('44','1','4','0','2020-04-24 15:06:24','0','0','0','LeetCode傳送門:判斷一個整數是不是迴文數 package com.zhouhong.LeetCode; import java.util.Stack; public class LeetCode0009 { //判斷一個整數是不是迴文數。...','LeetCode','//qiniusave.zhouhong.icu/3P1G4QARU5FC6CD80AB0J4QCPF.jpg','LeetCode0009:判斷一個整數是不是迴文數','5','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('45','1','4','0','2020-05-07 21:10:47','0','0','0','傳送門:LeetCode26原地刪除重複出現的排序元素 package com.zhouhong.LeetCode; //給定一個排序數組,你須要在 原地 刪除重複出現的元素,使得每一個元素只出現一次,返回移除後數組的新長度。 // 不要使用額外的...','雙指針,LeetCode','//qiniusave.zhouhong.icu/3F64LCIEC353EFOU9RH35S10C1.jpg','LeetCode0026:原地刪除重複出現的排序元素','7','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('46','1','4','0','2020-06-15 10:15:04','0','0','0','傳送門:LeetCode27原地移除目標值 package com.zhouhong.LeetCode; //給你一個數組 nums 和一個值 val,你須要 原地 移除全部數值等於 val 的元素,並返回移除後數組的新長度。 // 不要使用額外...','LeetCode,雙指針,數組','//qiniusave.zhouhong.icu/3LLSKK2ETVUCHKFMML1HMEJ3H5.jpg','LeetCode0027:原地移除目標值','5','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('47','1','4','0','2020-09-19 17:31:01','0','0','0','傳送門:LeetCode0034找出排序數組中給定值的開始結束位置 package com.zhouhong.LeetCode; import java.util.ArrayList; /** *給定一個按照升序排列的整數數組 nums,和一個目...','LeetCode,數組','//qiniusave.zhouhong.icu/3U38M4FL08SO1SBFU7282F15AH.jpg','LeetCode0034:找出排序數組中給定值的開始結束位置','9','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('48','1','4','0','2020-10-08 12:36:30','0','0','0','傳送門:LeetCode0035搜索插入位置 package com.zhouhong.LeetCode; /** * 給定一個排序數組和一個目標值,在數組中找到目標值,並返回其索引。 * 若是目標值不存在於數組中,返回它將會被按順序插入的位置。...','數組,二分搜索,LeetCode','//qiniusave.zhouhong.icu/4F2BNHQL652GQVN6EU6EAKUCU.jpg','LeetCode0035:搜索插入位置','8','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('49','1','4','0','2020-10-11 18:41:03','0','0','0','傳送門:LeetCode0075顏色分類排序 package com.zhouhong.LeetCode; import java.util.Arrays; //給定一個包含紅色、白色和藍色,一共 n 個元素的數組,原地對它們進行排序,使得相同顏...','快排,LeetCode','//qiniusave.zhouhong.icu/1CRLSEE4KFD9RT3SMRBBI7RDTT.jpg','LeetCode0075:顏色分類排序','7','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('50','1','4','0','2020-10-15 15:47:24','0','0','0','傳送門:LeetCode0136只出現一次的元素 package com.zhouhong.LeetCode; import java.util.Arrays; //給定一個非空整數數組,除了某個元素只出現一次之外,其他每一個元素均出現兩次。找出那...','LeetCode,雙指針','//qiniusave.zhouhong.icu/2GTM1BRNT8EARTTVKAAOJ4KOJM.jpg','LeetCode0136:只出現一次的元素','12','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('51','1','4','0','2020-10-20 09:57:02','0','0','0','傳送門:LeetCode0164最大間距,排序後兩兩數字直接最大的差值 package com.zhouhong.LeetCode; import java.util.Arrays; //給定一個無序的數組,找出數組在排序以後,相鄰元素之間最大的...','LeetCode,雙指針','//qiniusave.zhouhong.icu/G7MRRA45B1G2BDEE422V93PMH.jpg','LeetCode0164:最大間距,排序後兩兩數字直接最大的差值','13','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('52','1','4','0','2020-11-07 15:59:40','0','0','0','傳送門:LeetCode0206翻轉一個單鏈表 package com.zhouhong.LeetCode; public class LeetCode0206 { public class ListNode { int val; ListNod...','LeetCode,鏈表','//qiniusave.zhouhong.icu/25D5QTG1CPRRAO1MTGH5CAIB08.jpg','LeetCode0206:翻轉一個單鏈表','13','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('53','1','4','0','2020-11-19 16:02:28','0','0','0','傳送門:LeetCode0242有效的字母異位詞 package com.zhouhong.LeetCode; import java.util.Arrays; //給定兩個字符串 s 和 t ,編寫一個函數來判斷 t 是不是 s 的字母異位詞。...','LeetCode','//qiniusave.zhouhong.icu/1ICO3UF95GO0C91NVNOI76E5E5.jpg','LeetCode0242:有效的字母異位詞','21','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('66','1','4','0','2020-12-01 11:28:56','0','0','0','傳送門:LeetCode283移動零 給定一個數組 nums,編寫一個函數將全部 0 移動到數組的末尾,同時保持非零元素的相對順序。 必須在原數組上操做,不能拷貝額外的數組。儘可能減小操做次數。 1、雙指針解決 /** * 分爲兩種狀況:...','LeetCode,雙指針','//qiniusave.zhouhong.icu/2VPB7PA4NFLG1MP764H1NRMILN.jpg','LeetCode0283:移動零','19','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('67','1','4','1','2020-12-06 19:32:54','0','0','0','傳送門:LeetCode0287尋找重複的數 給定一個包含 n + 1 個整數的數組 nums,其數字都在 1 到 n 之間(包括 1 和 n),可知至少存在一個重複的整數。假設只有一個重複的整數,找出這個重複的數。 說明:// 不能更改原數組(...','LeetCode,雙指針','//qiniusave.zhouhong.icu/2HSPJJEUOQP9DC4AGG7D92CJ09.jpg','LeetCode0287:尋找重複的數','34','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('68','1','4','0','2020-12-10 20:37:29','0','0','0','數組中有一個數字出現的次數超過數組長度的一半,請找出這個數字。例如輸入一個長度爲9的數組{1,2,3,2,2,2,5,4,2}。因爲數字2在數組中出現了5次,超過數組長度的一半,所以輸出2。若是不存在則輸出0  /** * 排序後,次數大於一半,...','字符串,算法','//qiniusave.zhouhong.icu/3L7DV6AKJ0SEJ5D4QQ408C78LE.jpg','找出次數超過數組一半長度的數','32','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('69','1','4','0','2020-12-28 21:42:13','0','0','0','方法1、循環實現  static void method1(){ Scanner in = new Scanner(System.in); String str = in.nextLine(); char[] arr = str.toCharAr...','算法','//qiniusave.zhouhong.icu/TO6E9PCMLB92EL0AR1HQJ5GA8.jpg','5種方法實現字符串翻轉','27','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('70','1','8','0','2021-01-11 16:44:46','1','0','0','package com.zhouhong.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; imp...','工具','//qiniusave.zhouhong.icu/2GTM1BRNT8EARTTVKAAOJ4KOJM.jpg','SQL轉Java工具','25','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('71','1','4','0','2021-01-13 00:18:37','0','0','0','傳送門:LeetCode0455分發餅乾 1、題目描述 假設你是一位很棒的家長,想要給你的孩子們一些小餅乾。可是,每一個孩子最多隻能給一塊餅乾。 對每一個孩子 i,都有一個胃口值 g[i],這是能讓孩子們知足胃口的餅乾的最小尺寸;而且每塊餅...','貪心算法,LeetCode','//qiniusave.zhouhong.icu/3MBBFME404DS6EMAVMAA4LHAPK.jpg','貪心算法--LeetCode0455分發餅乾','17','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('72','1','4','0','2021-01-19 01:53:11','0','0','0','LeetCode0509斐波那契數列    1、題目 斐波那契數,一般用 F(n) 表示,造成的序列稱爲 斐波那契數列 。該數列由 0 和 1 開始,後面的...','動態規劃,LeetCode,斐波那契','//qiniusave.zhouhong.icu/1ES930G0D90TIIDB7LLGGDFGG.jpg','動態規劃--LeetCode0509斐波那契數列,不一樣方法耗時比較','61','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('74','14','8','0','2021-01-21 23:45:20','0','0','0','1.空格:默認匹配、普通匹配 location / { root /home; } 2.= :精確匹配(表示匹配到  /home/resources/img/face.png 這張圖片) location = /resourc...','Nginx','//qiniusave.zhouhong.icu/2U5F2D30E1BGD8GTVVI830BDF7.jpg','Nginx配置文件nginx.conf中location的匹配原則','19','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('79','1','8','0','2021-01-23 12:50:08','0','0','0','跨域問題的本質: 兩個站點域名不同,就會出現跨域問題。 因此跨域問題的本質是域名不一樣 1、跨域問題解決 一、CORS跨域資源共享---Cross-origin resource sharing 它容許瀏覽器向跨源服務器,發出XMLHttpReq...','跨域,Nginx,架構','//qiniusave.zhouhong.icu/2R5POVKLSG2JTH0B3LVVR337QP.jpg','Nginx、SprongBoot Cors解決跨域問題與Nginx中靜態資源防盜鏈配置','10','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('80','1','8','0','2021-01-23 13:21:46','0','0','0','1、Linux下Nginx的安裝 1.去官網 http://nginx.org/download/下載對應的Nginx安裝包,推薦使用穩定版本。 2.上傳Nginx到Linux服務器。 3.安裝依賴環境 (1)安裝gcc環境   yum inst...','Nginx,架構','//qiniusave.zhouhong.icu/3J6TGJ2KOSSEDFCEJHR056M91E.jpg','Nginx安裝/進程模型/事件處理機制/詳細配置/定時切割日誌','19','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('82','1','4','0','2021-01-24 01:08:41','0','0','0','傳送門:LeetCode0070爬樓梯問題 題目: 假設你正在爬樓梯。須要 n 階你才能到達樓頂。每次你能夠爬 1 或 2 個臺階。你有多少種不一樣的方法能夠爬到樓頂呢?  跟斐波那契數列同樣 解法1、使用動態規劃方法解決 cl...','斐波那契,LeetCode,動態規劃','//qiniusave.zhouhong.icu/2NMPPF6J9ENNRG7IDS8O1HU9Q7.jpg','動態規劃--LeetCode0070爬樓梯問題','14','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('83','1','8','0','2021-01-24 11:22:05','0','0','0','1、Nginx模塊化體系結構 event module:事件模塊 phase handler:處理客戶端請求和內容相應 output filter:輸出過濾,例如gzip,upstream 反向代理模塊,會把請求打到真正的服務器去處理 load ...','Nginx,架構,負載均衡','//qiniusave.zhouhong.icu/3894VA2SDV5G84FUN95A4FEADG.jpg','Nginx集羣、負載均衡解析','14','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('84','1','8','0','2021-01-24 13:26:04','0','0','0','1、四層負載均衡     四層負載均衡 : 可以爲企業網絡設備或者服務器帶來更好的服務,提升吞吐量、提高併發性能、加強服務器的處理能力,而且能夠提升服務器計算能力,使得咱們的網絡設備更加靈活。 負載均衡是當有大量的併發請求來到服務器的時...','架構,Nginx','//qiniusave.zhouhong.icu/1H8HTTRSR9J46EN8BDDOGU353S.jpg','四層、七層和DNS負載均衡','22','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('85','1','8','0','2021-01-24 13:55:57','0','0','0','1、利用Nginx構建三臺TomCat集羣  咱們如今有四臺機器,一臺用來作Nginx負載用,另外三臺爲正常的集羣節點。 配置反向代理: 配置一個server虛擬主機,用來監聽端口,用來接收http請求 location配置proxy_pass ...','Nginx,負載均衡','//qiniusave.zhouhong.icu/FHLEQ8SEBVO0PB26OVO3P7PQT.jpg','Nginx集羣構建以及負載均衡策略---輪詢、加權輪詢','19','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('86','1','8','0','2021-01-24 14:53:54','0','0','0','1、max_conns max_conns 用於限制一臺服務器的最大鏈接數,默認值是0表示無限制,每一個工做進程都能生效,限流,老版本要商業版才能使用; # worker進程設置1個,便於測試觀察成功的鏈接數 worker_processes 1;...','Nginx,架構','//qiniusave.zhouhong.icu/1MGQUSCKEGCA91VF9AOH26MNUR.jpg','Nginx中upstream指令參數解析和keepalive提升吞吐量','28','0');
INSERT INTO `mto_post` (`id`, `author_id`, `channel_id`, `comments`, `created`, `favors`, `featured`, `status`, `summary`, `tags`, `thumbnail`, `title`, `views`, `weight`) VALUES('98','1','8','0','2021-01-24 21:45:33','0','0','0','1、今天登陸個人網站,忽然發現報了下面的一個錯誤:  個人第一反應是:超時了應該是Nginx代理沒有設置超時時間,默認的超時時間估計過小了,而後就按照正常的方式用Xshell鏈接服務器,應該是網絡或者是其餘的緣由吧,好巧不巧的個人Xshell鏈接...','問題','//qiniusave.zhouhong.icu/2D98FKFVS216MBOIS5TS9CTR7R.jpg','Nginx報504 gateway timeout錯誤的解決方法','33','0');

官方連接:
https://www.elastic.co/cn/downloads/past-releasesshell

注:使用Logstatsh的版本號與elasticsearch版本號須要保持一致數據庫

本插件用於同步,es6.x起自帶,這個是集成在了 logstash中的。因此直接配置同步數據庫的配置文件便可。

  • 解壓

tar -zxvf logstash-6.4.3.tar.gz
  • 將解壓文件夾移到 /usr/local 下面

mv logstash-6.4.3 /usr/local
  • 進入目錄新建文件夾

cd  /usr/local/logstash-6.4.3/
mkdir sync
  • 進入 sync 目錄新建  logstash-db-sync.conf  和  my_blog.sql 兩個文件

    • logstash-db-sync.conf 內容以下
    • input {
          jdbc {
              # 設置 MySql/MariaDB 數據庫url以及數據庫名稱
              jdbc_connection_string => "jdbc:mysql://192.168.2.158:3306/db_mblog?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=GMT"
              # 用戶名和密碼
              jdbc_user => "root"
              jdbc_password => "root"
              # 數據庫驅動所在位置,能夠是絕對路徑或者相對路徑(在maven 倉庫裏面能夠獲得)
              jdbc_driver_library => "/usr/local/logstash-6.4.3/sync/mysql-connector-java-8.0.11.jar"
              # 驅動類名
              jdbc_driver_class => "com.mysql.cj.jdbc.Driver"
              # 開啓分頁
              jdbc_paging_enabled => "true"
              # 分頁每頁數量,能夠自定義
              jdbc_page_size => "10000"
              # 執行的sql文件路徑
              statement_filepath => "/usr/local/logstash-6.4.3/sync/my_blog.sql"
              # 設置定時任務間隔  含義:分、時、天、月、年,所有爲*默認含義爲每分鐘跑一次任務
              schedule => "* * * * *"
              # 索引類型
              type => "_doc"
              # 是否開啓記錄上次追蹤的結果,也就是上次更新的時間,這個會記錄到 last_run_metadata_path 的文件
              use_column_value => true
              # 記錄上一次追蹤的結果值
              last_run_metadata_path => "/usr/local/logstash-6.4.3/sync/blog_track_time"
              # 若是 use_column_value 爲true, 配置本參數,追蹤的 column 名,能夠是自增id或者時間
              tracking_column => "updated_time"
              # tracking_column 對應字段的類型
              tracking_column_type => "timestamp"
              # 是否清除 last_run_metadata_path 的記錄,true則每次都從頭開始查詢全部的數據庫記錄
              clean_run => false
              # 數據庫字段名稱大寫轉小寫
              lowercase_column_names => false
          }
      }
      output {
          elasticsearch {
              # es地址
              hosts => ["192.168.2.120:9200"]
              # 同步的索引名
              index => "my_blog"
              # 設置_docID和數據相同
              document_id => "%{id}"
          }
          # 日誌輸出
          stdout {
              codec => json_lines
          }
      }
    • my_blog.sql 文件內容以下
    • SELECT
      a.id  AS id,
      a.comments  AS comments,
      a.created  AS created,
      a.favors  AS favors,
      a.featured  AS featured,
      a.status  AS mstatus,
      a.summary  AS summary,
      a.tags  AS tags,
      a.thumbnail  AS thumbnail,
      a.title  AS title,
      a.views  AS views,
      a.weight AS weight
      FROM
      mto_post a
      WHERE
      a.created >= :sql_last_value
  • 進入 bin 目錄啓動

    • ./logstash -f /usr/local/logstash-6.4.3/sync/logstash-db-sync.conf
  • 查看

 

可見數據已經成功添加至ES中。

  • 自定義模板配置ik中文分詞器

    • GET   http://192.168.2.120:9200/_template/logstash   獲取到 logstash  修改相關內容,在 /usr/local/logstash-6.4.3/sync 下面新建 logstash-ik.json 將獲取到的內容修改後複製進去
    • logstash-ik.json內容以下:
    • {
          "order": 0,
          "version": 1,
          "index_patterns": ["*"],
          "settings": {
              "index": {
                  "refresh_interval": "5s"
              }
          },
          "mappings": {
              "_default_": {
                  "dynamic_templates": [
                      {
                          "message_field": {
                              "path_match": "message",
                              "match_mapping_type": "string",
                              "mapping": {
                                  "type": "text",
                                  "norms": false
                              }
                          }
                      },
                      {
                          "string_fields": {
                              "match": "*",
                              "match_mapping_type": "string",
                              "mapping": {
                                  "type": "text",
                                  "norms": false,
                                  "analyzer": "ik_max_word",
                                  "fields": {
                                      "keyword": {
                                          "type": "keyword",
                                          "ignore_above": 256
                                      }
                                  }
                              }
                          }
                      }
                  ],
                  "properties": {
                      "@timestamp": {
                          "type": "date"
                      },
                      "@version": {
                          "type": "keyword"
                      },
                      "geoip": {
                          "dynamic": true,
                          "properties": {
                              "ip": {
                                  "type": "ip"
                              },
                              "location": {
                                  "type": "geo_point"
                              },
                              "latitude": {
                                  "type": "half_float"
                              },
                              "longitude": {
                                  "type": "half_float"
                              }
                          }
                      }
                  }
              }
          },
          "aliases": {}
      }
    • 將 logstash-ik.json 引入配置文件 logstash-db-sync.conf output模塊中作以下修改:
    • output {
          elasticsearch {
              # es地址
              hosts => ["192.168.2.120:9200"]
              # 同步的索引名
              index => "my_blog"
              # 設置_docID和數據相同
              document_id => "%{id}"
              # 定義模板名稱
              template_name => "myik"
              # 模板所在位置
              template => "/usr/local/logstash-6.4.3/sync/logstash-ik.json"
              # 重寫模板
              template_overwrite => true
              #  默認爲true,false關閉logstash自動管理模板功能,若是自定義模板,則設置爲false
              manage_template => false
          }
          # 日誌輸出
          stdout {
              codec => json_lines
          }
      }
    • 啓動
    • ./logstash -f /usr/local/logstash-6.4.3/sync/logstash-db-sync.conf
相關文章
相關標籤/搜索