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存入数据