win下ElasticSearch数据的同步(canal同步,与代码逻辑同步)

第一种:使用MQ与代码逻辑同步(个人不推荐)

这种方法会增加一些本身不符合业务需求的逻辑代码,增加一些不必要的工作量,不利于后续的迭代升级。

引入rabbitmq依赖,添加配置

<!--rabbitmq-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>
  rabbitmq:
    host: 127.0.0.1

rabbitmq添加消息队列

在这里插入图片描述

编写接收消息类

@Component
@RabbitListener(queues = "searchroom_house") //监听队列名称
public class HouseListener {

    @Autowired
    HouseSearchDao houseSearchDao;

    @RabbitHandler
    public void houseListener(Map<String, String> map){
        if (StringUtils.isNotBlank(map.get(HouseConstant.create))){

            HouseDTO houseDTO = JSON.parseObject(map.get(HouseConstant.create), HouseDTO.class);
            HouseIndex houseIndex = new HouseIndex();
            BeanUtils.copyProperties(houseDTO, houseIndex);
            houseIndex.setId(Long.valueOf(houseDTO.getId()));
            houseIndex.setHouseId(Long.valueOf(houseDTO.getId()));

            BeanUtils.copyProperties(houseDTO.getHouseDetail(), houseIndex);

            StringBuffer tags = new StringBuffer();
            for (String tag : houseDTO.getTags()){
                tags.append(tags+",");
            }
            houseIndex.setTags(tags.toString().substring(0, tags.length()-1));

            houseSearchDao.save(houseIndex);
        }
    }

}

添加房源信息时,发送消息至消息队列

//通知ES创建索引数据
        HouseDTO houseDTO = new HouseDTO();
        BeanUtils.copyProperties(house, houseDTO);

        HouseDetailDTO houseDetailDTO = new HouseDetailDTO();
        BeanUtils.copyProperties(houseDetail ,houseDetailDTO);
        houseDTO.setHouseDetail(houseDetailDTO);

        houseDTO.setTags(houseVo.getTags());

        Map<String, String> map = new HashMap<>();
        map.put(HouseConstant.create, JSON.toJSONString(houseDTO));
        rabbitTemplate.convertAndSend("searchroom_house", map);

打开ES-head查看是否添加成功

在这里插入图片描述

第二种:借助工具同步

在这里插入图片描述

win配置canal环境:https://blog.csdn.net/weixin_45528987/article/details/105714752

下载安装canal-adapter,链接:https://github.com/alibaba/canal/releases

修改配置文件

在这里插入图片描述

server:
  port: 8081
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8
    default-property-inclusion: non_null

canal.conf:
  mode: tcp # kafka rocketMQ   模式
  canalServerHost: 127.0.0.1:11111 # canal Server的地址端口
#  zookeeperHosts: slave1:2181
#  mqServers: 127.0.0.1:9092 #or rocketmq
#  flatMessage: true
  batchSize: 500
  syncBatchSize: 1000
  retries: 0
  timeout:
  accessKey:
  secretKey:
  username:
  password:
  vhost:
  srcDataSources:   # mysql数据源
    defaultDS:
      url: jdbc:mysql://127.0.0.1:3306/searchroom?characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
      username: root
      password: zxf18377217936
  canalAdapters:
  - instance: example # canal instance Name or mq topic name canal-server的instance 名字或者mq topic名字
    groups:
    - groupId: g1  # 组id
      outerAdapters:
      - name: logger
#      - name: rdb
#        key: mysql1
#        properties:
#          jdbc.driverClassName: com.mysql.jdbc.Driver
#          jdbc.url: jdbc:mysql://127.0.0.1:3306/mytest2?useUnicode=true
#          jdbc.username: root
#          jdbc.password: 121212
#      - name: rdb
#        key: oracle1
#        properties:
#          jdbc.driverClassName: oracle.jdbc.OracleDriver
#          jdbc.url: jdbc:oracle:thin:@localhost:49161:XE
#          jdbc.username: mytest
#          jdbc.password: m121212
#      - name: rdb
#        key: postgres1
#        properties:
#          jdbc.driverClassName: org.postgresql.Driver
#          jdbc.url: jdbc:postgresql://localhost:5432/postgres
#          jdbc.username: postgres
#          jdbc.password: 121212
#          threads: 1
#          commitSize: 3000
#      - name: hbase
#        properties:
#          hbase.zookeeper.quorum: 127.0.0.1
#          hbase.zookeeper.property.clientPort: 2181
#          zookeeper.znode.parent: /hbase
      - name: es7  # 数据输出方向   1.1.5版本中conf目录下有es6与es7,你使用的版本是哪一个就写那一个
        hosts: 127.0.0.1:9300 # 127.0.0.1:9200 for rest mode es数据地址
        properties:
#          mode: transport # or rest  # 假如你使用的9200端口,需要开启此配置,并且设置为rest
#          # security.auth: test:123456 #  only used for rest mode
          cluster.name: elasticsearch   # es集群中名称


1.1.5不同于其它版本,其它版本conf下只有一个es文件夹,1.1.5存在es6与es7,配置可以参考但是请不要照搬

在conf文件下的es7或者es6中添加对elasticsearch的映射配置,名字随意起,文件后缀必须为yml
在这里插入图片描述

dataSourceKey: defaultDS   #指定数据源,这个值和adapter的application.yml文件中配置的srcDataSources值对应。
destination: example       #指定canal-server中配置的某个实例的名字
groupId: g1                #组ID,默认就好
esMapping:                 #ES的mapping(映射)
  _index: searchroom            #要同步到的ES的索引名称(自定义)
  _type: house              #ES索引的类型名称(自定义)
  _id: id                 #ES标示文档的唯一标示,通常对应数据表中的主键ID字段
  #pk: id                  #如果不需要_id, 则需要指定一个属性为主键属性
  sql: "select a.id AS id, a.id AS houseId, a.room, a.parlour, a.floor, a.total_floor AS totalFloor, a.watch_times AS watchTimes, a.build_year AS buildYear,a.cover,a.title, a.price, a.area, a.create_time AS createTime, a.last_update_time AS lastUpdateTime,a.city_en_name AS cityEnName, a.region_en_name AS regionEnName, a.direction, a.distance_to_subway AS distanceToSubway,a.street, a.district from house a "
  #这里就是数据表中的每个字段到ES索引中叫什么名字的sql映射,注意映射到es中的每个字段都要是唯一的,不能重复。
  #etlCondition: "where t.occur_time>='{0}'"
  commitBatch: 3000

进入bin目录下,启动startup,正确启动完成是不会存在报错
存在报错可以尝试到gihub下载源码查看报错行数原因
在这里插入图片描述
到数据库中随意修改一条数据
UPDATE house SET title = ‘canal修改同步测试’ WHERE id = 46;
控制台会数据
在这里插入图片描述
有强迫症的可以去日志文件中去查看
在这里插入图片描述

打开head,查看数据是否更改,如图表示测试成功

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值