成人无码www免费视频,日韩欧美一区二区三区电影,干鸡巴视频中国,色婷婷综合久久一区二区

您的位置:首頁 > 要聞 >

世界百事通!SpringBoot整合ElasticSearch

2023-03-25 22:02:17 來源:騰訊云

ElasticSearch是個(gè)開源分布式搜索引擎,提供搜集、分析、存儲(chǔ)數(shù)據(jù)三大功能。它的特點(diǎn)有:分布式,零配置,自動(dòng)發(fā)現(xiàn),索引自動(dòng)分片,索引副本機(jī)制,restful風(fēng)格接口,多數(shù)據(jù)源,自動(dòng)搜索負(fù)載等。主要負(fù)責(zé)將日志索引并存儲(chǔ)起來,方便業(yè)務(wù)方檢索查詢。

1 安裝ES

下載地址:https://www.elastic.co/cn/downloads/elasticsearch

選擇Windows版本,我下載的是7.17.0。解壓后即可完成安裝。


(相關(guān)資料圖)

進(jìn)入bin文件, 雙擊執(zhí)行 elasticsearch.bat,然后打開瀏覽器,進(jìn)入頁面: http://localhost:9200,看到以下輸出,表示啟動(dòng)成功。

2 Spring項(xiàng)目

項(xiàng)目GitHub地址:https://github.com/Snowstorm0/learn-es

項(xiàng)目Gitee地址:https://gitee.com/Snowstorm0/learn-es

2.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();    }}

2.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ù)庫那樣進(jìn)行增刪改查的操作。

2.3 配置數(shù)據(jù)庫

spring.datasource.username=rootspring.datasource.password=rootspring.datasource.url=jdbc:mysql://localhost:3306/sys?characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC

3 運(yùn)行項(xiàng)目

3.1 添加

調(diào)用添加接口:http://localhost:8080/user/add

添加User類的請(qǐng)求體:

{  "id":"1",  "name":"代碼的路",  "job":"碼農(nóng)",  "deposit":100.0}

可以看到添加成功:

3.2 讀取

運(yùn)行讀取接口:http://localhost:8080/user/search/whole?key=碼農(nóng)

可以獲得剛寫入的User類,是完整結(jié)構(gòu):

運(yùn)行讀取接口:http://localhost:8080/user/search/es?key=碼農(nóng)

可以獲得剛寫入的User類,只有User結(jié)構(gòu):

打開數(shù)據(jù)庫,無需手動(dòng)創(chuàng)建表結(jié)構(gòu),即可看到User類也已經(jīng)寫入到數(shù)據(jù)庫中:

因此可以刻直接從數(shù)據(jù)庫讀取。

關(guān)鍵詞:

[責(zé)任編輯:xwzkw]

相關(guān)閱讀

广元市| 阿尔山市| 进贤县| 夹江县| 富源县| 杂多县| 宜宾市| 横峰县| 岗巴县| 河北省| 巴中市| 湾仔区| 望奎县| 临安市| 潼南县| 裕民县| 鱼台县| 深圳市| 海门市| 当涂县| 怀安县| 游戏| 思茅市| 郸城县| 双牌县| 东辽县| 静海县| 内江市| 宁国市| 无极县| 湘潭市| 望都县| 镇平县| 静乐县| 晋宁县| 通化市| 遂平县| 永德县| 金堂县| 锦屏县| 华蓥市|