版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(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 *索引名稱(chēng)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)建更新索引樂(lè)淘棋牌 * *@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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 《狀態(tài)檢修基礎(chǔ)知識(shí)》課件
- 內(nèi)蒙古呼和浩特市2024屆九年級(jí)上學(xué)期期末考試數(shù)學(xué)試卷(含答案)
- 養(yǎng)老院老人滿(mǎn)意度調(diào)查評(píng)估制度
- 《電動(dòng)機(jī)與電氣傳動(dòng)》課件
- 《市場(chǎng)調(diào)查講座》課件
- 《石墨烯的研究》課件
- 2024年版:國(guó)際文化旅游項(xiàng)目開(kāi)發(fā)合同
- 技術(shù)研發(fā)合作合同(2篇)
- 2024年版金融服務(wù)合同(企業(yè)上市輔導(dǎo))
- 2024天津房屋買(mǎi)賣(mài)合同中房屋租賃保證金及退還3篇
- 模板支撐腳手架集中線(xiàn)荷載、施工總荷載計(jì)算表(修正)
- GB/T 43700-2024滑雪場(chǎng)所的運(yùn)行和管理規(guī)范
- 新媒體部門(mén)崗位配置人員架構(gòu)圖
- 水電站廠(chǎng)房設(shè)計(jì)-畢業(yè)設(shè)計(jì)
- 綜合金融服務(wù)方案課件
- 《鎮(zhèn)原民俗》課件
- 球磨機(jī)崗位作業(yè)指導(dǎo)書(shū)
- 眼科護(hù)理滴眼藥水論文
- 市級(jí)社?;疬\(yùn)行分析報(bào)告
- 2024年遼寧省水資源管理集團(tuán)招聘筆試參考題庫(kù)附帶答案詳解
- 小學(xué)信息技術(shù)畫(huà)圖課件巧妙的直線(xiàn)和曲線(xiàn)
評(píng)論
0/150
提交評(píng)論