spingboot集成elasticsearch终极版

yml配置文件配置es

单机:

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父依赖版本:

<parent>    
	<groupId>org.springframework.boot</groupId>    
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.3.4.RELEASE</version>
</parent>

添加实体类映射:

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 org.springframework.stereotype.Component;
import java.util.Date;

@Data@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "two_tickets_ai_info")
@Component
public 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;

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

public interface CameraNoticeDao extends ElasticsearchCrudRepository<CameraNotice,Long> {}

操作es

@Autowired
private CameraNoticeDao cameraNoticeDao;

CameraNotice cameraNotice = new CameraNotice();
cameraNoticeDao.save(cameraNotice); //向es存入数据
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值