版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
關(guān)于elasticsearchJavaHighLevelREST的封裝操作pox.xml文件添加以下內(nèi)容org.elasticsearch.clientelasticsearch-rest-high-level-client6.3.2新建ESHighLevelRestUtil.javapackagecom;importjava.util.HashMap;importjava.util.Map;importorg.apache.http.HttpHost;importorg.elasticsearch.ElasticsearchException;importorg.elasticsearch.action.admin.indices.create.CreateIndexRequest;importorg.elasticsearch.action.admin.indices.create.CreateIndexResponse;importorg.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;importorg.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;importorg.elasticsearch.action.admin.indices.get.GetIndexRequest;importorg.elasticsearch.action.delete.DeleteRequest;importorg.elasticsearch.action.delete.DeleteResponse;importorg.elasticsearch.action.index.IndexRequest;importorg.elasticsearch.action.index.IndexResponse;importorg.elasticsearch.action.support.replication.ReplicationResponse;importorg.elasticsearch.action.DocWriteResponse;importorg.elasticsearch.client.RestClient;importorg.elasticsearch.client.RestHighLevelClient;importmon.settings.Settings;importmon.xcontent.XContentType;importorg.elasticsearch.rest.RestStatus;publicclassESHighLevelRestUtil{ staticRestHighLevelClientclient=newRestHighLevelClient( RestClient.builder(newHttpHost("49",9200,"http"))); /** *驗(yàn)證索引是否存在 * *@paramindex *索引名稱638棋牌 *@return *@throwsException */ publicbooleanindexExists(Stringindex)throwsException{ GetIndexRequestrequest=newGetIndexRequest(); request.indices(index); request.local(false); request.humanReadable(true); booleanexists=client.indices().exists(request); returnexists; } /** * *@paramindex *@paramindexType *@paramproperties *結(jié)構(gòu):{name:{type:text}}{age:{type:integer}} *@return *@throwsException */ publicbooleanindexCreate(Stringindex,StringindexType, Map<string,object="">properties)throwsException{ if(indexExists(index)){ returntrue; } CreateIndexRequestrequest=newCreateIndexRequest(index); request.settings(Settings.builder().put("index.number_of_shards",3) .put("index.number_of_replicas",2)); Map<string,object="">jsonMap=newHashMap<>(); Map<string,object="">mapping=newHashMap<>(); mapping.put("properties",properties); jsonMap.put(indexType,mapping); request.mapping(indexType,jsonMap); CreateIndexResponsecreateIndexResponse=client.indices().create( request); booleanacknowledged=createIndexResponse.isAcknowledged(); returnacknowledged; } /** *刪除索引 * *@paramindex *@return *@throwsException */ publicbooleanindexDelete(Stringindex)throwsException{ try{ DeleteIndexRequestrequest=newDeleteIndexRequest(index); DeleteIndexResponsedeleteIndexResponse=client.indices().delete( request); returndeleteIndexResponse.isAcknowledged(); }catch(ElasticsearchExceptionexception){ if(exception.status()==RestStatus.NOT_FOUND){ returntrue; }else{ returnfalse; } } } /** *創(chuàng)建更新文檔 * *@paramindex *@paramindexType *@paramdocumentId *@paramjosonStr *@return *@throwsException */ publicbooleandocumentCreate(Stringindex,StringindexType, StringdocumentId,StringjosonStr)throwsException{ IndexRequestrequest=newIndexRequest(index,indexType,documentId); request.source(josonStr,XContentType.JSON); IndexResponseindexResponse=client.index(request); if(indexResponse.getResult()==DocWriteResponse.Result.CREATED ||indexResponse.getResult()==DocWriteResponse.Result.UPDATED){ returntrue; } ReplicationResponse.ShardInfoshardInfo=indexResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ returntrue; } if(shardInfo.getFailed()>0){ for(ReplicationResponse.ShardInfo.Failurefailure:shardInfo .getFailures()){ thrownewException(failure.reason()); } } returnfalse; } /** *創(chuàng)建更新索引樂淘棋牌 * *@paramindex *@paramindexType *@paramdocumentId *@parammap *@return *@throwsException */ publicbooleandocumentCreate(Stringindex,StringindexType, StringdocumentId,Map<string,object="">map)throwsException{ IndexRequestrequest=newIndexRequest(index,indexType,documentId); request.source(map); IndexResponseindexResponse=client.index(request); if(indexResponse.getResult()==DocWriteResponse.Result.CREATED ||indexResponse.getResult()==DocWriteResponse.Result.UPDATED){ returntrue; } ReplicationResponse.ShardInfoshardInfo=indexResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ returntrue; } if(shardInfo.getFailed()>0){ for(ReplicationResponse.ShardInfo.Failurefailure:shardInfo .getFailures()){ thrownewException(failure.reason()); } } returnfalse; } /** *創(chuàng)建索引 * *@paramindex *@paramindexType *@paramjosonStr *@return *@throwsException */ publicStringdocumentCreate(Stringindex,StringindexType,StringjosonStr) throwsException{ IndexRequestrequest=newIndexRequest(index,indexType); request.source(josonStr,XContentType.JSON); IndexResponseindexResponse=client.index(request); Stringid=indexResponse.getId(); if(indexResponse.getResult()==DocWriteResponse.Result.CREATED ||indexResponse.getResult()==DocWriteResponse.Result.UPDATED){ returnid; } ReplicationResponse.ShardInfoshardInfo=indexResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ returnid; } if(shardInfo.getFailed()>0){ for(ReplicationResponse.ShardInfo.Failurefailure:shardInfo .getFailures()){ thrownewException(failure.reason()); } } returnnull; } /** *創(chuàng)建索引 * *@paramindex *@paramindexType *@parammap *@return *@throwsException */ publicStringdocumentCreate(Stringindex,StringindexType, Map<string,object="">map)throwsException{ IndexRequestrequest=newIndexRequest(index,indexType); request.source(map); IndexResponseindexResponse=client.index(request); Stringid=indexResponse.getId(); if(indexResponse.getResult()==DocWriteResponse.Result.CREATED ||indexResponse.getResult()==DocWriteResponse.Result.UPDATED){ returnid; } ReplicationResponse.ShardInfoshardInfo=indexResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ returnid; } if(shardInfo.getFailed()>0){ for(ReplicationResponse.ShardInfo.Failurefailure:shardInfo .getFailures()){ thrownewException(failure.reason()); } } returnnull; } publicbooleandocumentDelete(Stringindex,StringindexType, StringdocumentId)throwsException{ DeleteRequestrequest=newDeleteRequest(index,indexType,documentId); DeleteResponsedeleteResponse=client.delete(request); if(deleteResponse.getResult()==DocWriteResponse.Result.NOT_FOUND){ returntrue; } ReplicationResponse.ShardInfoshardInfo=deleteResponse.getShardInfo(); if(shardInfo.getTotal()!=shardInfo.getSuccessful()){ returntrue; } if(shardInfo.getFailed()>0){ for(ReplicationResponse.ShardInfo.Failurefailure:shardInfo .getFailures()){ thrownewException(failure.reason()); } } returnfalse; }}新建ESHighLevelRestTest.javapackagecom;importjava.util.HashMap;importjava.util.Map;importorg.apache.http.HttpHost;importorg.elasticsearch.action.admin.indices.get.GetIndexRequest;importorg.elasticsearch.client.RestClient;importorg.elasticsearch.client.RestHighLevelClient;publicclassESHighLevelRestTest{ publicstaticvoidmain(String[]args)throwsException{ //TODOAuto-generatedmethodstub ESHighLevelRestUtilutil=newESHighLevelRestUtil(); System.out.println(util.indexExists("indextest001")); Map<string,object="">map=newHashMap<string,object="">(); map
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年雞場(chǎng)生態(tài)養(yǎng)殖與技術(shù)開發(fā)合同3篇
- 2024適用個(gè)人借貸協(xié)議模板版B版
- 2024年第三方擔(dān)保責(zé)任合同執(zhí)行與監(jiān)督管理細(xì)則3篇
- 2024年離婚財(cái)產(chǎn)分配模板合同
- 2025年度風(fēng)力發(fā)電機(jī)組安裝合同3篇
- 2024環(huán)保項(xiàng)目居間合作合同
- 2024智能交通工具設(shè)計(jì)與制造合作協(xié)議
- 2024旅行社租車協(xié)議、合同
- 2024年社區(qū)生鮮自助取貨協(xié)議3篇
- 2024房地產(chǎn)融資居間合同格式范文
- 2024年酒店式公寓承包合同
- 學(xué)校安全存在的問題及整改措施
- 2025年八省聯(lián)考內(nèi)蒙古高考生物試卷真題答案詳解(精校打印)
- 校園公園綠化養(yǎng)護(hù)協(xié)議
- 貓抓病的護(hù)理
- 2024版城市綠化養(yǎng)護(hù)合同補(bǔ)充協(xié)議3篇
- GB/T 19799.2-2024無損檢測(cè)超聲檢測(cè)試塊第2部分:2號(hào)標(biāo)準(zhǔn)試塊
- 2024-2025學(xué)年冀教新版八年級(jí)上冊(cè)數(shù)學(xué)期末復(fù)習(xí)試卷(含詳解)
- DB45T 1831-2018 汽車加油加氣站防雷裝置檢測(cè)技術(shù)規(guī)范
- 水資源調(diào)配與優(yōu)化-洞察分析
- 無人機(jī)職業(yè)生涯規(guī)劃
評(píng)論
0/150
提交評(píng)論