• 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操作

        @Override
        public 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());
            }
            //查询总记录数total
            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;
        }

    -- -- -- -- -- -- 

        @Override
        public 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.返回数据

    啦啦啦

  • 相关阅读:
    python得到今天前的七天每天日期
    python 实现元组中的的数据按照list排序, python查询mysql得到的数据是元组格式,按照list格式对他们排序
    NoReverseMatch at /salesman/zhuce/ Reverse for '/zhuce/' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
    为什么springMVC和Mybatis逐渐流行起来了?
    图像的七个不变矩 可用于图像的匹配
    【可视化必备】大数据时代的可视化工具
    常用机器视觉工具----图像分析工具(blob分析)
    【转】七种常见阈值分割代码(Otsu、最大熵、迭代法、自适应阀值、手动、迭代法、基本全局阈值法)
    C#编写滤镜 图片色调取反效果(Invert)
    Bitmap四种属性
  • 原文地址:https://www.cnblogs.com/ClassNotFoundException/p/7279871.html
Copyright © 2020-2023  润新知