ElasticSearch是個開源分布式搜索引擎恭垦,提供搜集昧谊、分析附井、存儲數(shù)據(jù)三大功能贿堰。它的特點有:分布式愚战,零配置宗兼,自動發(fā)現(xiàn),索引自動分片孟岛,索引副本機制瓶竭,restful風格接口督勺,多數(shù)據(jù)源渠羞,自動搜索負載等。主要負責將日志索引并存儲起來智哀,方便業(yè)務方檢索查詢次询。
ElasticSearch安裝方式參考:
1 Spring項目
項目GitHub地址:https://github.com/Snowstorm0/learn-es
項目Gitee地址:https://gitee.com/Snowstorm0/learn-es
1.1 配置ES客戶端
public class RestClientConfig extends AbstractElasticsearchConfiguration {
@Override
@Bean
public RestHighLevelClient elasticsearchClient() {
final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200")
.build();
return RestClients.create(clientConfiguration).rest();
}
}
1.2 創(chuàng)建User類
public class UserEntity {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String job;
private Double deposit;
private Date processTime = new Date();
}
配置完成后,ElasticSearch即可像常規(guī)的數(shù)據(jù)庫那樣進行增刪改查的操作瓷叫。
1.3 配置數(shù)據(jù)庫
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/sys?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
2 運行項目
2.1 添加
調(diào)用添加接口:http://localhost:8080/user/add
添加User類的請求體:
{
"id":"1",
"name":"代碼的路",
"job":"碼農(nóng)",
"deposit":100.0
}
可以看到添加成功:
2.2 讀取
運行讀取接口:http://localhost:8080/user/search/whole?key=碼農(nóng)
可以獲得剛寫入的User類屯吊,是完整結構:
運行讀取接口:http://localhost:8080/user/search/es?key=碼農(nóng)
可以獲得剛寫入的User類,只有User結構:
打開數(shù)據(jù)庫摹菠,無需手動創(chuàng)建表結構盒卸,即可看到User類也已經(jīng)寫入到數(shù)據(jù)庫中:
因此可以刻直接從數(shù)據(jù)庫讀取。
?
?
學習更多編程知識次氨,請關注我的公眾號:
?
?