Hadoop -- ES -- CURD
生活随笔
收集整理的這篇文章主要介紹了
Hadoop -- ES -- CURD
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.獲取ES連接
package com.ciic.history.common;import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.transport.TransportAddress;import java.net.InetAddress; import java.net.UnknownHostException;public class DaoUtilL {private static TransportClient clientL = null;private DaoUtilL(){};public static TransportClient getClientL() {try {if (null != clientL) {return clientL;}Settings settings = Settings.settingsBuilder().put("cluster.name", "search1").put("transport.tcp.compress", true).build();InetSocketTransportAddress address1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.43.249"), 9300);InetSocketTransportAddress address2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.43.250"), 9300);InetSocketTransportAddress address3 = new InetSocketTransportAddress(InetAddress.getByName("192.168.43.251"), 9300);InetSocketTransportAddress address4 = new InetSocketTransportAddress(InetAddress.getByName("192.168.43.252"), 9300);TransportAddress[] addressArr = {address1, address2, address3, address4};clientL = TransportClient.builder().settings(settings).build().addTransportAddresses(addressArr);return clientL;} catch (Exception e) {return null;}} }?
2.CURD操作
@Overridepublic JsonEntity queryCustomerPreview(int page, int rows) {TransportClient clientL = DaoUtilL.getClientL();SearchRequestBuilder builder = clientL.prepareSearch();builder.setIndices("esinner_limecustomerpreview_index").setTypes("xian").addSort("imscustomername", SortOrder.ASC).setFrom((page-1)*rows).setSize(rows);SearchResponse response = builder.get();SearchHit[] searchHits = response.getHits().getHits();ArrayList arrayList = new ArrayList();for(int i = 0; i < searchHits.length; i++) {arrayList.add(searchHits[i].getSource());}//查詢總記錄數totalSearchResponse totalResponse = clientL.prepareSearch().setIndices("esinner_limecustomerpreview_index").setSearchType(SearchType.COUNT).setSize(0).get();long length = response.getHits().totalHits();JsonEntity entity = new JsonEntity();entity.setTotal(length);entity.setRows(arrayList);return entity;}-- -- -- -- -- --?
@Overridepublic JsonEntity customerPreviewSearch(EsinnerLimeCustomerPreviewIndex customerPreview, int page, int rows) {TransportClient clientL = DaoUtilL.getClientL();SearchRequestBuilder searchRequestBuilder = clientL.prepareSearch();searchRequestBuilder.setIndices("esinner_limecustomerpreview_index").setTypes("xian").setSearchType(SearchType.DEFAULT).setFrom((page-1)*rows).setSize(rows);BoolQueryBuilder builder = QueryBuilders.boolQuery();boolean flag=true;if(StringUtils.isNotBlank(customerPreview.getImscustomername())){flag=false;builder.must(QueryBuilders.termQuery("imscustomername",customerPreview.getImscustomername()));}if(StringUtils.isNotBlank(customerPreview.getImscustomercode())){flag=false;builder.must(QueryBuilders.matchQuery("imscustomercode",customerPreview.getImscustomercode()));}if(flag==true){searchRequestBuilder.addSort("imscustomercode", SortOrder.ASC);}searchRequestBuilder.setQuery(builder);SearchResponse response = searchRequestBuilder.get();SearchHit[] searchHits = response.getHits().getHits();ArrayList arrayList = new ArrayList();for(int i = 0; i < searchHits.length; i++) {arrayList.add(searchHits[i].getSource());}SearchResponse totalResponse = clientL.prepareSearch().setIndices("esinner_limecustomerpreview_index").setSearchType(SearchType.COUNT).setSize(0).get();long length = response.getHits().totalHits();JsonEntity entity = new JsonEntity();entity.setTotal(length);entity.setRows(arrayList);return entity;}-- -- -- -- -- --?
3.返回數據
啦啦啦
轉載于:https://www.cnblogs.com/ClassNotFoundException/p/7279871.html
總結
以上是生活随笔為你收集整理的Hadoop -- ES -- CURD的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python日记----2017.8.1
- 下一篇: [HZOI 2016][Tyvj 172