专注的阿熊 阅读(107) 评论(0)

单机:

spring:

    elasticsearch:

      rest:

        uris: ["http://xxx.xxx.xxx.xxx:9200"]

        connection-timeout: 20s

        username: xxxxxxxx

        password: xxxxxxxxx

集群:

spring:

  data:

    elasticsearch:

      cluster-nodes: xxx.xxx.xx.3:9201,xxx.xxx.xx.3:9202,xxx.xxx.xx.4:9201,xxx.xxx.xx.4:9202

      cluster-name: xxxxxxxxxxx

  elasticsearch:

    rest:

      uris: ["http://xxx.xxx.xx.3:9201"]

      connection-timeout: 20s

      username: xxxxx

      password: xxxxxxxxxx

spingboot父依赖版本:

   

org.springframework.boot    

spring-boot-starter-parent

2.3.4.RELEASE

跟单网添加实体类映射:

package com.aipf.apply.esparam;

import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import org.springframework.data.elasticsearch.annotations.DateFormat;import org.springframework.data.elasticsearch.annotations.Document;import org.springframework.data.elasticsearch.annotations.Field;import org.springframework.data.elasticsearch.annotations.FieldType;import ponent;import java.util.Date;

@Data@AllArgsConstructor@NoArgsConstructor@Document(indexName = "two_tickets_ai_info")@Componentpublic class CameraNotice {    private Long id;    @Field(name = "@timestamp",type = FieldType.Date,format = DateFormat.custom,pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")    protected Date timestamp;  @Field(name = "in_cnt")    private int inCount;  @Field(name = "out_cnt")    private int outCount;@Field(name = "no_judge_cnt")    private int noJudgeCount;     @Field(name = "camera_id")    private Integer cameraId;

添加dao层操作对象

package com.aipf.apply.dao;

importcom.aipf.apply.esparam.CameraNotice;import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;

public interface CameraNoticeDao extends ElasticsearchCrudRepository {}

操作es

@Autowiredprivate CameraNoticeDao cameraNoticeDao;

CameraNotice cameraNotice = new CameraNotice();

cameraNoticeDao.save(cameraNotice); //es存入数据