ElasticSearch是個開源分布式搜索引擎,提供搜集、分析、存儲數據三大功能。它的特點有:分布式,零配置,自動發現,索引自動分片,索引副本機制,restful風格接口,多數據源,自動搜索負載等。主要負責將日志索引并存儲起來,方便業務方檢索查詢。
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 創建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即可像常規的數據庫那樣進行增刪改查的操作。
1.3 配置數據庫
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 添加
調用添加接口:http://localhost:8080/user/add
添加User類的請求體:
{
"id":"1",
"name":"代碼的路",
"job":"碼農",
"deposit":100.0
}
可以看到添加成功:
2.2 讀取
運行讀取接口:http://localhost:8080/user/search/whole?key=碼農
可以獲得剛寫入的User類,是完整結構:
運行讀取接口:http://localhost:8080/user/search/es?key=碼農
可以獲得剛寫入的User類,只有User結構:
打開數據庫,無需手動創建表結構,即可看到User類也已經寫入到數據庫中:
因此可以刻直接從數據庫讀取。
學習更多編程知識,請關注我的公眾號:
-
ES
+關注
關注
0文章
11瀏覽量
20063 -
JAVA
+關注
關注
19文章
2975瀏覽量
105161 -
數據庫
+關注
關注
7文章
3848瀏覽量
64692 -
SpringBoot
+關注
關注
0文章
174瀏覽量
201
發布評論請先 登錄
相關推薦
評論