最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL 與 Elasticsearch 數(shù)據(jù)不對稱問題解決辦法

 更新時間:2017年08月24日 08:37:16   投稿:lqh  
這篇文章主要介紹了MySQL 與 Elasticsearch 數(shù)據(jù)不對稱問題解決辦法的相關(guān)資料,對于 elasticsearch 增量寫入,但經(jīng)常jdbc源一端的數(shù)據(jù)庫可能會做數(shù)據(jù)庫刪除或者更新操作,這里提供解決辦法,需要的朋友可以參考下

MySQL 與 Elasticsearch 數(shù)據(jù)不對稱問題解決辦法

jdbc-input-plugin 只能實(shí)現(xiàn)數(shù)據(jù)庫的追加,對于 elasticsearch 增量寫入,但經(jīng)常jdbc源一端的數(shù)據(jù)庫可能會做數(shù)據(jù)庫刪除或者更新操作。這樣一來數(shù)據(jù)庫與搜索引擎的數(shù)據(jù)庫就出現(xiàn)了不對稱的情況。

當(dāng)然你如果有開發(fā)團(tuán)隊(duì)可以寫程序在刪除或者更新的時候同步對搜索引擎操作。如果你沒有這個能力,可以嘗試下面的方法。

這里有一個數(shù)據(jù)表 article , mtime 字段定義了 ON UPDATE CURRENT_TIMESTAMP 所以每次更新mtime的時間都會變化

mysql> desc article;
+-------------+--------------+------+-----+--------------------------------+-------+
| Field    | Type     | Null | Key | Default            | Extra |
+-------------+--------------+------+-----+--------------------------------+-------+
| id     | int(11)   | NO  |   | 0               |    |
| title    | mediumtext  | NO  |   | NULL              |    |
| description | mediumtext  | YES |   | NULL              |    |
| author   | varchar(100) | YES |   | NULL              |    |
| source   | varchar(100) | YES |   | NULL              |    |
| content   | longtext   | YES |   | NULL              |    |
| status   | enum('Y','N')| NO  |   | 'N'              |    |
| ctime    | timestamp  | NO  |   | CURRENT_TIMESTAMP       |    |
| mtime    | timestamp  | YES |   | ON UPDATE CURRENT_TIMESTAMP  |    |
+-------------+--------------+------+-----+--------------------------------+-------+
7 rows in set (0.00 sec)

logstash 增加 mtime 的查詢規(guī)則

jdbc {
  jdbc_driver_library => "/usr/share/java/mysql-connector-java.jar"
  jdbc_driver_class => "com.mysql.jdbc.Driver"
  jdbc_connection_string => "jdbc:mysql://localhost:3306/cms"
  jdbc_user => "cms"
  jdbc_password => "password"
  schedule => "* * * * *" #定時cron的表達(dá)式,這里是每分鐘執(zhí)行一次
  statement => "select * from article where mtime > :sql_last_value"
  use_column_value => true
  tracking_column => "mtime"
  tracking_column_type => "timestamp" 
  record_last_run => true
  last_run_metadata_path => "/var/tmp/article-mtime.last"
 }

創(chuàng)建回收站表,這個事用于解決數(shù)據(jù)庫刪除,或者禁用 status = 'N' 這種情況的。

CREATE TABLE `elasticsearch_trash` (
 `id` int(11) NOT NULL,
 `ctime` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

為 article 表創(chuàng)建觸發(fā)器

CREATE DEFINER=`dba`@`%` TRIGGER `article_BEFORE_UPDATE` BEFORE UPDATE ON `article` FOR EACH ROW
BEGIN
 -- 此處的邏輯是解決文章狀態(tài)變?yōu)?N 的時候,需要將搜索引擎中對應(yīng)的數(shù)據(jù)刪除。
 IF NEW.status = 'N' THEN
 insert into elasticsearch_trash(id) values(OLD.id);
 END IF;
 -- 此處邏輯是修改狀態(tài)到 Y 的時候,方式elasticsearch_trash仍然存在該文章ID,導(dǎo)致誤刪除。所以需要刪除回收站中得回收記錄。
  IF NEW.status = 'Y' THEN
 delete from elasticsearch_trash where id = OLD.id;
 END IF;
END

CREATE DEFINER=`dba`@`%` TRIGGER `article_BEFORE_DELETE` BEFORE DELETE ON `article` FOR EACH ROW
BEGIN
 -- 此處邏輯是文章被刪除同事將改文章放入搜索引擎回收站。
 insert into elasticsearch_trash(id) values(OLD.id);
END

接下來我們需要寫一個簡單地 Shell 每分鐘運(yùn)行一次,從 elasticsearch_trash 數(shù)據(jù)表中取出數(shù)據(jù),然后使用 curl 命令調(diào)用 elasticsearch restful 接口,刪除被收回的數(shù)據(jù)。

你還可以開發(fā)相關(guān)的程序,這里提供一個 Spring boot 定時任務(wù)例子。

實(shí)體

package cn.netkiller.api.domain.elasticsearch;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class ElasticsearchTrash {
 @Id
 private int id;

 @Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP")
 private Date ctime;

 public int getId() {
 return id;
 }

 public void setId(int id) {
 this.id = id;
 }

 public Date getCtime() {
 return ctime;
 }

 public void setCtime(Date ctime) {
 this.ctime = ctime;
 }

}

倉庫

package cn.netkiller.api.repository.elasticsearch;

import org.springframework.data.repository.CrudRepository;

import com.example.api.domain.elasticsearch.ElasticsearchTrash;

public interface ElasticsearchTrashRepository extends CrudRepository<ElasticsearchTrash, Integer>{


}

定時任務(wù)

package cn.netkiller.api.schedule;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.rest.RestStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.example.api.domain.elasticsearch.ElasticsearchTrash;
import com.example.api.repository.elasticsearch.ElasticsearchTrashRepository;

@Component
public class ScheduledTasks {
 private static final Logger logger = LoggerFactory.getLogger(ScheduledTasks.class);

 @Autowired
 private TransportClient client;

 @Autowired
 private ElasticsearchTrashRepository alasticsearchTrashRepository;

 public ScheduledTasks() {
 }

 @Scheduled(fixedRate = 1000 * 60) // 60秒運(yùn)行一次調(diào)度任務(wù)
 public void cleanTrash() {
 for (ElasticsearchTrash elasticsearchTrash : alasticsearchTrashRepository.findAll()) {
  DeleteResponse response = client.prepareDelete("information", "article", elasticsearchTrash.getId() + "").get();
  RestStatus status = response.status();
  logger.info("delete {} {}", elasticsearchTrash.getId(), status.toString());
  if (status == RestStatus.OK || status == RestStatus.NOT_FOUND) {
  alasticsearchTrashRepository.delete(elasticsearchTrash);
  }
 }
 }
}

Spring boot 啟動主程序。

package cn.netkiller.api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class Application {

 public static void main(String[] args) {
 SpringApplication.run(Application.class, args);
 }
}
 

以上就是MySQL 與 Elasticsearch 數(shù)據(jù)不對稱問題解決辦法的講解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • mysql中的存儲過程傳參問題

    mysql中的存儲過程傳參問題

    這篇文章主要介紹了mysql中的存儲過程傳參問題,具有很好的參考價值,希望對大家有所幫助,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • mysql數(shù)據(jù)庫鏈接失敗常見問題及解決

    mysql數(shù)據(jù)庫鏈接失敗常見問題及解決

    這篇文章主要介紹了mysql數(shù)據(jù)庫鏈接失敗常見問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • MySQL 分庫分表實(shí)踐

    MySQL 分庫分表實(shí)踐

    本文主要介紹了MySQL 分庫分表實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • MySQL日志的詳細(xì)分析實(shí)例

    MySQL日志的詳細(xì)分析實(shí)例

    MySQL日志記錄了MySQL數(shù)據(jù)庫日常操作和錯誤信息,下面這篇文章主要給大家介紹了關(guān)于MySQL日志的詳細(xì)分析,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • MySQL日期格式以及日期函數(shù)舉例詳解

    MySQL日期格式以及日期函數(shù)舉例詳解

    這篇文章主要給大家介紹了關(guān)于MySQL日期格式及日期函數(shù)的相關(guān)資料,日期在數(shù)據(jù)庫中是一個常見且重要的數(shù)據(jù)類型,在MySQL中我們可以使用各種函數(shù)和格式化選項(xiàng)來處理和顯示日期,需要的朋友可以參考下
    2023-11-11
  • MySQL?聚合函數(shù)、分組查詢、時間函數(shù)詳解

    MySQL?聚合函數(shù)、分組查詢、時間函數(shù)詳解

    這篇文章主要介紹了MySQL?聚合函數(shù)、分組查詢、時間函數(shù)?,主要考驗(yàn)聚合函數(shù)count()求和以及分組查詢,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-08-08
  • Mysql使用索引的正確方法及索引原理詳解

    Mysql使用索引的正確方法及索引原理詳解

    這篇文章主要給大家介紹了關(guān)于Mysql使用索引的正確方法及索引原理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用mysql具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • Mysql 中的secure_file_priv參數(shù)設(shè)置方法

    Mysql 中的secure_file_priv參數(shù)設(shè)置方法

    MySQL的secure_file_priv變量用于限制文件的讀取和寫入,在后期漏洞注入中,可以使用INTO DUMPFILE和INTO OUTFILE將數(shù)據(jù)寫入文件,或者使用LOAD_FILE下載文件,本文介紹Mysql 中的secure_file_priv參數(shù)設(shè)置,感興趣的朋友一起看看吧
    2024-11-11
  • CentOS下重置MySQL的root密碼的教程

    CentOS下重置MySQL的root密碼的教程

    這篇文章主要介紹了CentOS下重置MySQL的root密碼的教程,首先要擁有系統(tǒng)的root權(quán)限,最后還附屬了一個使用mysqladmin下的方法,需要的朋友可以參考下
    2015-11-11
  • MySQL查看和修改字符編碼的實(shí)現(xiàn)方法

    MySQL查看和修改字符編碼的實(shí)現(xiàn)方法

    下面小編就為大家?guī)硪黄狹ySQL查看和修改字符編碼的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-11-11

最新評論

津南区| 古蔺县| 庐江县| 琼中| 巴林左旗| 南昌县| 长兴县| 布拖县| 龙岩市| 新余市| 文水县| 聊城市| 上思县| 两当县| 华亭县| 红安县| 文山县| 禄劝| 平和县| 天祝| 青冈县| 万载县| 长治市| 新竹县| 普兰店市| 惠州市| 通山县| 枣庄市| 沂水县| 河津市| 贵阳市| 仁怀市| 巴彦县| 将乐县| 河西区| 岳阳县| 西藏| 淳安县| 乌兰察布市| 济源市| 余庆县|