国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

elasticsearch實(shí)現(xiàn)mysql數(shù)據(jù)同步

這篇具有很好參考價(jià)值的文章主要介紹了elasticsearch實(shí)現(xiàn)mysql數(shù)據(jù)同步。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

當(dāng)酒店數(shù)據(jù)發(fā)生增、刪、改時(shí),要求對(duì)elasticsearch中數(shù)據(jù)也要完成相同操作。

常見的數(shù)據(jù)同步方案有三種:

  • 同步調(diào)用

  • 異步通知

  • 監(jiān)聽binlog

以下使用異步通知同步elasticsearch的數(shù)據(jù)?

引入依賴

<!--amqp-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
   <groupId>org.elasticsearch.client</groupId>
   <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<!--fastjson-->
   <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.66</version>
</dependency>

配置elasticsearch

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@MapperScan(basePackages = "cn.itcast.hotel.mapper")
@SpringBootApplication
public class HotelAdminApplication {

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

    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public RestHighLevelClient client(){
        return  new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://43.139.59.28:9200")
        ));
    }

}

配置交換機(jī)、隊(duì)列

聲明交換機(jī)、隊(duì)列名稱?

constatnts包下新建一個(gè)類MqConstants,存儲(chǔ)交換機(jī)和隊(duì)列的名稱

public class MqConstants {
    /**
     * 交換機(jī)
     */
    public final static String HOTEL_EXCHANGE = "hotel.topic";
    /**
     * 監(jiān)聽新增和修改的隊(duì)列
     */
    public final static String HOTEL_INSERT_QUEUE = "hotel.insert.queue";
    /**
     * 監(jiān)聽刪除的隊(duì)列
     */
    public final static String HOTEL_DELETE_QUEUE = "hotel.delete.queue";
    /**
     * 新增或修改的RoutingKey
     */
    public final static String HOTEL_INSERT_KEY = "hotel.insert";
    /**
     * 刪除的RoutingKey
     */
    public final static String HOTEL_DELETE_KEY = "hotel.delete";
}

聲明交換機(jī)、隊(duì)列

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfig {
    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(MqConstants.HOTEL_EXCHANGE, true, false);
    }

    @Bean
    public Queue insertQueue(){
        return new Queue(MqConstants.HOTEL_INSERT_QUEUE, true);
    }

    @Bean
    public Queue deleteQueue(){
        return new Queue(MqConstants.HOTEL_DELETE_QUEUE, true);
    }

    @Bean
    public Binding insertQueueBinding(){
        return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(MqConstants.HOTEL_INSERT_KEY);
    }

    @Bean
    public Binding deleteQueueBinding(){
        return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(MqConstants.HOTEL_DELETE_KEY);
    }
}

發(fā)送MQ消息 ?

在增、刪、改業(yè)務(wù)中分別發(fā)送MQ消息

@Resource
    private RabbitTemplate rabbitTemplate;
    
    @PostMapping
    public void saveHotel(@RequestBody Hotel hotel){
        hotelService.save(hotel);
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,hotel.getId());
    }

    @PutMapping()
    public void updateById(@RequestBody Hotel hotel){
        if (hotel.getId() == null) {
            throw new InvalidParameterException("id不能為空");
        }
        hotelService.updateById(hotel);
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,hotel.getId());
    }

    @DeleteMapping("/{id}")
    public void deleteById(@PathVariable("id") Long id) {
        hotelService.removeById(id);
        rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_DELETE_KEY,id);
    }

接收MQ消息

接收到MQ消息要做的事情包括:

  • 新增消息:根據(jù)傳遞的hotel的id查詢hotel信息,然后新增一條數(shù)據(jù)到索引庫

  • 刪除消息:根據(jù)傳遞的hotel的id刪除索引庫中的一條數(shù)據(jù)

文檔類

獲取es傳來的值文章來源地址http://www.zghlxwxcb.cn/news/detail-697048.html



import lombok.Data;
import lombok.NoArgsConstructor;
 
@Data
@NoArgsConstructor
public class HotelDoc {
    private Long id;
    private String name;
    private String address;
    private Integer price;
    private Integer score;
    private String brand;
    private String city;
    private String starName;
    private String business;
    private String location;
    private String pic;
    // 排序時(shí)的 距離值
    private Object distance;
    public HotelDoc(Hotel hotel) {
        this.id = hotel.getId();
        this.name = hotel.getName();
        this.address = hotel.getAddress();
        this.price = hotel.getPrice();
        this.score = hotel.getScore();
        this.brand = hotel.getBrand();
        this.city = hotel.getCity();
        this.starName = hotel.getStarName();
        this.business = hotel.getBusiness();
        this.location = hotel.getLatitude() + ", " + hotel.getLongitude();
        this.pic = hotel.getPic();
    }
}
model類?
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("tb_hotel")
public class Hotel {
    @TableId(type = IdType.INPUT)
    private Long id;
    private String name;
    private String address;
    private Integer price;
    private Integer score;
    private String brand;
    private String city;
    private String starName;
    private String business;
    private String longitude;
    private String latitude;
    private String pic;
}
?service類
import cn.itcast.hotel.mapper.HotelMapper;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

@Service
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {

    @Resource
    private RestHighLevelClient client;


    @Override
    public void deleteById(Long id) {
        try {
            // 1.準(zhǔn)備Request
            DeleteRequest request = new DeleteRequest("hotel", id.toString());
            // 2.發(fā)送請(qǐng)求
            client.delete(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void insertById(Long id) {
        try {
            // 0.根據(jù)id查詢酒店數(shù)據(jù)
            Hotel hotel = getById(id);
            // 轉(zhuǎn)換為文檔類型
            HotelDoc hotelDoc = new HotelDoc(hotel);

            // 1.準(zhǔn)備Request對(duì)象
            IndexRequest request = new IndexRequest("hotel").id(hotel.getId().toString());
            // 2.準(zhǔn)備Json文檔
            request.source(JSON.toJSONString(hotelDoc), XContentType.JSON);
            // 3.發(fā)送請(qǐng)求
            client.index(request, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
監(jiān)聽器
import cn.itcast.hotel.constatnts.MqConstants;
import cn.itcast.hotel.service.IHotelService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HotelListener {

    @Autowired
    private IHotelService hotelService;

    /**
     * 監(jiān)聽酒店新增或修改的業(yè)務(wù)
     * @param id 酒店id
     */
    @RabbitListener(queues = MqConstants.HOTEL_INSERT_QUEUE)
    public void listenHotelInsertOrUpdate(Long id){
        hotelService.insertById(id);
    }

    /**
     * 監(jiān)聽酒店刪除的業(yè)務(wù)
     * @param id 酒店id
     */
    @RabbitListener(queues = MqConstants.HOTEL_DELETE_QUEUE)
    public void listenHotelDelete(Long id){
        hotelService.deleteById(id);
    }
}

到了這里,關(guān)于elasticsearch實(shí)現(xiàn)mysql數(shù)據(jù)同步的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Java SpringBoot API 實(shí)現(xiàn)ES(Elasticsearch)搜索引擎的一系列操作(超詳細(xì))(模擬數(shù)據(jù)庫操作)

    Java SpringBoot API 實(shí)現(xiàn)ES(Elasticsearch)搜索引擎的一系列操作(超詳細(xì))(模擬數(shù)據(jù)庫操作)

    小編使用的是elasticsearch-7.3.2 基礎(chǔ)說明: 啟動(dòng):進(jìn)入elasticsearch-7.3.2/bin目錄,雙擊elasticsearch.bat進(jìn)行啟動(dòng),當(dāng)出現(xiàn)一下界面說明,啟動(dòng)成功。也可以訪問http://localhost:9200/ 啟動(dòng)ES管理:進(jìn)入elasticsearch-head-master文件夾,然后進(jìn)入cmd命令界面,輸入npm?run?start?即可啟動(dòng)。訪問http

    2024年02月04日
    瀏覽(34)
  • ElasticSearch搜索引擎:數(shù)據(jù)的寫入流程

    ElasticSearch搜索引擎:數(shù)據(jù)的寫入流程

    (1)ES 客戶端選擇一個(gè)節(jié)點(diǎn) node 發(fā)送請(qǐng)求過去,這個(gè)節(jié)點(diǎn)就是協(xié)調(diào)節(jié)點(diǎn) coordinating node? (2)協(xié)調(diào)節(jié)點(diǎn)對(duì) document 進(jìn)行路由,通過 hash 算法計(jì)算出數(shù)據(jù)應(yīng)該落在哪個(gè)分片?shard 上,然后根據(jù)節(jié)點(diǎn)上維護(hù)的 shard 信息,將請(qǐng)求轉(zhuǎn)發(fā)到對(duì)應(yīng)的實(shí)際處理節(jié)點(diǎn)node上 shard = hash(document_id) %

    2023年04月14日
    瀏覽(27)
  • SpringBoot封裝Elasticsearch搜索引擎實(shí)現(xiàn)全文檢索

    注:本文實(shí)現(xiàn)了Java對(duì)Elasticseach的分頁檢索/不分頁檢索的封裝 ES就不用過多介紹了,直接上代碼: 創(chuàng)建Store類(與ES字段對(duì)應(yīng),用于接收ES數(shù)據(jù)) Elasticsearch全文檢索接口:不分頁檢索 Elasticsearch全文檢索接口:分頁檢索 本文實(shí)現(xiàn)了Java對(duì)Elasticsearch搜索引擎全文檢索的封裝 傳入

    2024年02月04日
    瀏覽(38)
  • 搜索引擎(大數(shù)據(jù)檢索)論述[elasticsearch原理相關(guān)]

    搜索引擎(大數(shù)據(jù)檢索)論述[elasticsearch原理相關(guān)]

    首先需要大致知道搜索引擎有大致幾類:1.全文搜索引擎 2.垂直搜索引擎 3.類目搜索引擎等。 1.全文搜索引擎:是全文本覆蓋的,百度,google等都是全文本搜索,就是我搜一個(gè)詞項(xiàng)“方圓”,那么這個(gè)詞項(xiàng)可以是數(shù)字平方的概念,可以是一個(gè)人名,可以是一首歌等,所有的相

    2023年04月08日
    瀏覽(30)
  • 【搜索引擎2】實(shí)現(xiàn)API方式調(diào)用ElasticSearch8接口

    【搜索引擎2】實(shí)現(xiàn)API方式調(diào)用ElasticSearch8接口

    1、理解ElasticSearch各名詞含義 ElasticSearch對(duì)比Mysql Mysql數(shù)據(jù)庫 Elastic Search Database 7.X版本前有Type,對(duì)比數(shù)據(jù)庫中的表,新版取消了 Table Index Row Document Column mapping Elasticsearch是使用Java開發(fā)的,8.1版本的ES需要JDK17及以上版本;es默認(rèn)帶有JDK,如果安裝es環(huán)境為java8,則會(huì)默認(rèn)使用自帶

    2024年04月17日
    瀏覽(21)
  • 基于Elasticsearch與Hbase組合框架的大數(shù)據(jù)搜索引擎

    基于Elasticsearch與Hbase組合框架的大數(shù)據(jù)搜索引擎

    本項(xiàng)目為學(xué)校大數(shù)據(jù)工程實(shí)訓(xùn)項(xiàng)目,共開發(fā)4周,答辯成績不錯(cuò)。代碼倉庫放文章尾,寫的不好,代碼僅供參考。 對(duì)于結(jié)構(gòu)化數(shù)據(jù) ,因?yàn)樗鼈兙哂刑囟ǖ慕Y(jié)構(gòu),所以我們一般都是可以通過關(guān)系型數(shù)據(jù)庫(MySQL,Oracle 等)的二維表(Table)的方式存儲(chǔ)和搜索,也可以建立索引。

    2024年02月09日
    瀏覽(23)
  • Elasticsearch (ES) 搜索引擎: 數(shù)據(jù)類型、動(dòng)態(tài)映射、多類型(子字段)

    原文鏈接:https://xiets.blog.csdn.net/article/details/132348634 版權(quán)聲明:原創(chuàng)文章禁止轉(zhuǎn)載 專欄目錄:Elasticsearch 專欄(總目錄) ES 映射字段的 數(shù)據(jù)類型 ,官網(wǎng)文檔參考:Field data types。 下面是 ES 常用的一些基本數(shù)據(jù)類型。 字符串 類型: keyword :類型。 text :文本類型。

    2024年03月23日
    瀏覽(38)
  • 用SpringBoot和ElasticSearch實(shí)現(xiàn)網(wǎng)盤搜索引擎,附源碼,詳細(xì)教學(xué)

    用SpringBoot和ElasticSearch實(shí)現(xiàn)網(wǎng)盤搜索引擎,附源碼,詳細(xì)教學(xué)

    可以掃描小程序碼體驗(yàn),切換到搜索Tabbar。 小程序端界面實(shí)現(xiàn) 網(wǎng)頁端實(shí)現(xiàn)界面 對(duì)外提供的api 接口聲明 接口實(shí)現(xiàn) 執(zhí)行搜索策略。 提供2種搜索策略,分別是MySQL和ElasticSearch搜索策略。在配置文件進(jìn)行配置搜索策略。 搜索類型枚舉 配置文件中的搜索策略相關(guān)配置 es搜索策略實(shí)

    2024年02月08日
    瀏覽(21)
  • elasticsearch[五]:深入探索ES搜索引擎的自動(dòng)補(bǔ)全與拼寫糾錯(cuò):如何實(shí)現(xiàn)高效智能的搜索體驗(yàn)

    elasticsearch[五]:深入探索ES搜索引擎的自動(dòng)補(bǔ)全與拼寫糾錯(cuò):如何實(shí)現(xiàn)高效智能的搜索體驗(yàn)

    前一章講了搜索中的拼寫糾錯(cuò)功能,里面一個(gè)很重要的概念就是萊文斯坦距離。這章會(huì)講解搜索中提升用戶體驗(yàn)的另一項(xiàng)功能 - [自動(dòng)補(bǔ)全]。本章直接介紹 ES 中的實(shí)現(xiàn)方式以及真正的搜索引擎對(duì)自動(dòng)補(bǔ)全功能的優(yōu)化。 大家對(duì)上面的這個(gè)應(yīng)該都不陌生,搜索引擎會(huì)根據(jù)你輸入的

    2024年01月24日
    瀏覽(33)
  • 如何使用內(nèi)網(wǎng)穿透工具實(shí)現(xiàn)Java遠(yuǎn)程連接本地Elasticsearch搜索分析引擎

    如何使用內(nèi)網(wǎng)穿透工具實(shí)現(xiàn)Java遠(yuǎn)程連接本地Elasticsearch搜索分析引擎

    簡單幾步,結(jié)合Cpolar 內(nèi)網(wǎng)穿透工具實(shí)現(xiàn)Java 遠(yuǎn)程連接操作本地分布式搜索和數(shù)據(jù)分析引擎Elasticsearch。 Cpolar內(nèi)網(wǎng)穿透提供了更高的安全性和隱私保護(hù),通過使用加密通信通道,Cpolar技術(shù)可以確保數(shù)據(jù)傳輸?shù)陌踩?,這為用戶和團(tuán)隊(duì)提供了更可靠的保護(hù),使他們能夠放心地處理和

    2024年02月04日
    瀏覽(29)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包