자바 작업 elasticsearch 도구 클래스

40403 단어 【JavaEE】
package com.ncs.dao;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.PrefixQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.SpanFirstQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.ncs.Conf;
import com.ncs.entity.ESEntity;

/**
 *   Elasticserach
 * @author syl
 *
 * @date 2017 4 7    5:59:23
 */
public class ElasticSearchDAO {

    private static final Logger logger = LoggerFactory
            .getLogger(ElasticSearchDAO.class);

    //       
    private static TransportClient client = null;
    private volatile static BulkRequestBuilder prepareBulk;

    //        
    static {
        /*// es 5.X     
        String EsHosts = Conf.get("es.hosts");

        Settings settings = Settings.builder()
                .put("cluster.name", Conf.get("es.cluster.name"))//       
                .put("tclient.transport.sniff", true).build();//            ,         ip        

        //      
        client = new PreBuiltTransportClient(setting);
        String[] nodes = EsHosts.split(",");
        for (String node : nodes) {
            if (node.length() > 0) {//      node(   、                 node)
                String[] hostPort = node.split(":");
                try {
                    client.addTransportAddress(new InetSocketTransportAddress(
                            InetAddress.getByName(hostPort[0]), Integer.parseInt(hostPort[1])));
                } catch (Exception e) {
                    e.printStackTrace();
                } 
            }
        } */

        // es 2.4.4     
        String EsHosts = Conf.get("es.hosts");

        Settings settings = Settings.settingsBuilder()
                .put("cluster.name", Conf.get("es.cluster.name"))//       
                .put("tclient.transport.sniff", true).build();//            ,         ip        

        //      
        client = TransportClient.builder().settings(settings).build();
        String[] nodes = EsHosts.split(",");
        for (String node : nodes) {
            if (node.length() > 0) {//      node(   、                 node)
                String[] hostPort = node.split(":");
                try {
                    client.addTransportAddress(new InetSocketTransportAddress(
                            InetAddress.getByName(hostPort[0]), Integer.parseInt(hostPort[1])));
                } catch (Exception e) {
                    e.printStackTrace();
                } 
            }
        }
    }

    /**
     *      ,    
     */
    public static void closeClient() {

        client.close();
        logger.info("client closed!");
    }

    /**
     *       
     * 
     * @param indexname
     *                
     */
    public static void createIndexName(String indexname) {
        client.admin().indices().prepareCreate(indexname).execute().actionGet();
        logger.info("create index " + indexname + " success!");
    }

    /**
     *   mapping(feid("indexAnalyzer","ik")     IK  ;
     * feid("searchAnalyzer","ik")     ik  ;        IK      )
     * 
     * @param indexname
     *                ;
     * @param mappingType
     *                
     * @throws Exception
     */
    public static void createMapping(String indexname, String mappingType)
            throws Exception {
        new XContentFactory();
        XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
//              .startObject("_ttl").field("enable", false).endObject()
                .startObject("properties")
                .startObject("domain").field("type", "string")
                    .field("store", "yes").field("index", "not_analyzed")
                .endObject()
                .startObject("rowKeys").field("type", "string")
                    .field("store", "yes").field("index", "not_analyzed")
                .endObject()
//              .startObject("clazz").field("type", "string")
//                  .field("store", "yes").field("index", "not_analyzed")
//              .endObject()
//              .startObject("type").field("type", "string")
//                  .field("store", "yes").field("index", "not_analyzed")
//              .endObject()
//              .startObject("rdata").field("type", "string")
//                  .field("store", "yes").field("index", "not_analyzed")
//              .endObject()
//              .startObject("ispId")
//                  .field("type", "integer").field("store", "yes")
//                  .field("index", "not_analyzed")
//              .endObject()
//              .startObject("version")
//                  .field("type", "long").field("store", "yes")
//                  .field("index", "not_analyzed")
//              .endObject()
                .endObject().endObject();
        PutMappingRequest mapping = Requests.putMappingRequest(indexname)
                .type(mappingType).source(builder);
        client.admin().indices().putMapping(mapping).actionGet();
        logger.info("create mapping " + mappingType + " success!");
    }

    /**
     *     ,     
     * 
     * @param indexName
     *                 ,  es           。        
     * @param indexType
     *            Type     ,                  ,               。
     * @param jsondata
     *            json       
     * @return
     */
    public static void createIndexResponse(String indexname, String type,
            List jsondata) {

        //             .setRefresh(true)       ,               
        IndexRequestBuilder requestBuilder = client.prepareIndex(indexname,
                type).setRefresh(true);
        for (int i = 0; i < jsondata.size(); i++) {
            IndexResponse actionGet = requestBuilder.setId(i + 1 + "").setSource(jsondata.get(i))
                    .execute().actionGet();

            logger.info("response.getVersion():" + actionGet.getVersion());
        }
    }

    /**
     *     ,       
     * 
     * @param indexname
     *                 
     * @param type
     *                
     * @param jsondata
     *            json     
     * @return
     */
    public static IndexRequestBuilder createIndexResponse(String indexname,
            String type, String jsondata, String id) {

        IndexRequestBuilder response = client.prepareIndex(indexname, type)
        //          ID
                .setId(id).setSource(jsondata);
        //   index       
//      logger.info("response.getVersion():" + response.getVersion());
        return response;
    }

    /**
     *        
     * @param indexname //    
     * @param type      //    
     * @param jsondata  // json   
     * @param id        // id
     */
    public static void creatIndexResponseBulk(String indexname,
            String type, String jsondata, String id)  throws Exception{
        prepareBulk = client.prepareBulk();
        prepareBulk = prepareBulk.add(ElasticSearchDAO.createIndexResponse(indexname, type, jsondata, id));

    }

    /**
     *       ,    prepareBulk
     */
    public static void execute() throws Exception {
         //  1000     
        prepareBulk.execute().actionGet();
    }


    /**
     *       、   、id,       
     * 
     * @param indexname
     *                
     * @param type
     *               
     * @param id
     *                      
     */
    public static DeleteResponse deleteResponse(String indexname, String type,
            String id) {
        DeleteResponse response = client.prepareDelete(indexname, type, id)
                .execute().actionGet();
        logger.info(response.getId() + " delete success!");
        return response;
    }

    /**
     *      、   、id、  、 ,            
     * 
     * @param indexname
     *               
     * @param type
     *               
     * @param id
     * @param cloumn
     *              
     * @param value
     *             
     * @return
     */
    public static UpdateResponse updataResponse(String indexname, String type,
            String id, String cloumn, String value) {
        Map params = Maps.newHashMap();
        params.put(cloumn, value);
        UpdateResponse response = client.prepareUpdate(indexname, type, id)
        // .setScript("ctx._source." + cloumn + "=" + cloumn,
        // ScriptType.INLINE).setScriptParams(params)
                .execute().actionGet();
        logger.info("updata success!");
        return response;
    }

    /**
     *       、   、id,       
     * 
     * @param indexname
     *                
     * @param type
     *               
     * @param id
     *                      
     */
    public static GetResponse getResponse(String indexname, String type,
            String id) {
        GetResponse response = client.prepareGet(indexname, type, id).execute()
                .actionGet();
        logger.info("response.getId():" + response.getId() + "response.getSourceAsString():" + response.getSourceAsString());
        return response;
    }

    /**
     *        ESEntity
     * 
     * @param queryBuilder
     *                
     * @param indexname
     *                
     * @param type
     *               
     * @return
     */
    public static List searcherESEntitys(
            QueryBuilder queryBuilder, String indexname, String type) {

        List list = new ArrayList();

        //     
        SearchResponse searchResponse = client.prepareSearch(indexname)
                .setTypes(type).setQuery(queryBuilder).execute().actionGet();

        //       
        SearchHits hits = searchResponse.getHits();
        System.out.println("      =" + hits.getTotalHits());
        SearchHit[] searchHists = hits.getHits();

        String domain;
        String rowKeys;
        //           Record  ,   list   
        if (searchHists.length > 0) {
            for (SearchHit hit : searchHists) {
                domain = (String) hit.getSource().get("domain");
                rowKeys = (String) hit.getSource().get("rowKeys");
                list.add(new ESEntity(domain, rowKeys));
            }
        }
        return list;
    }

     /**
      *  ESEntity       ES 
      * @param esEntities       
      * @param indexname        
      * @param type             
      */
    public static void intoEs(List esEntities, 
            String indexname, String type) {

        String receiveString;
        try {
            //          es 
            for (ESEntity esEntity : esEntities) {
                //       json   
                receiveString = JSON.toJSONString(esEntity);
                //     es     
                ElasticSearchDAO.creatIndexResponseBulk(indexname, type, receiveString, esEntity.getDomain());
            }
            //     es 
            ElasticSearchDAO.execute();
            logger.info(esEntities.size() + " row eSEntity has inesrt into es!");
        } catch (Exception e) {
            logger.error(" inesrt into es error!" + e.toString());
            e.printStackTrace();
        }
    }

    @SuppressWarnings({ "unused" })
    public static void main(String[] args) {

        String indexname = "zone";
        String type = "records";

        //          
        // List jsondata = Zone.getInitJsonData();

        //   index     ,(         )
        // ElasticSearchJavaAPI.createIndexResponse(indexname, type, jsondata);

        //     
        // //////////////////////////////////////////////////////////////////////////////////////
        //            
        QueryBuilder queryBuilder = QueryBuilders.termQuery("owner", "sex2");

        // content field,test     .
        //   must      ,mustNot       ,should      
        //   bool        must,        should  ,      minimum_should_match            should  .
        QueryBuilder qb = QueryBuilders
                .boolQuery()
                .must(QueryBuilders.termQuery("rdata", "ns1.101domain.com"))
                // .must(QueryBuilders.termQuery("rdata", "ns1.sdc.org.cn"))
                .mustNot(
                        QueryBuilders
                                .termQuery("rdata", "nsgbr.comlaude.co.uk"))
                .should(QueryBuilders.termQuery("rdata", "ns1.sdc.org.cn"));

        //              ,  elasticsearch     IK                    
        QueryBuilder queryBuilder3 = QueryBuilders.termQuery("owner", " ");
        QueryBuilder queryBuilder4 = QueryBuilders.termQuery("owner", " ");

        BoolQueryBuilder bool = QueryBuilders.boolQuery();
        bool.must(queryBuilder3);
        bool.must(queryBuilder4);

        //       
        PrefixQueryBuilder prefixQuery = QueryBuilders.prefixQuery("domain", "163");

        //      
        WildcardQueryBuilder wildcardQuery = QueryBuilders.wildcardQuery(
                "rdata", "?ns"); // ? *

        //                 
        FuzzyQueryBuilder fuzzyQuery = QueryBuilders.fuzzyQuery("name", "sex");

        //     
        RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("rdata")
                .from(0).to(10);

        //     
        MatchAllQueryBuilder matchAllQuery = QueryBuilders.matchAllQuery();

        //   id  
        QueryBuilder queryBuilder2 = QueryBuilders.boolQuery().must(
                QueryBuilders.termQuery("id", 1));

        //     Field     ,                 ,        :ns    3    ,nsgbr      
        SpanFirstQueryBuilder spanFirstQuery = QueryBuilders.spanFirstQuery(
                QueryBuilders.spanTermQuery("rdata", "nsgbr"), // Query
                3 // Max End position
                );
        // String  
        QueryStringQueryBuilder queryString = QueryBuilders.queryStringQuery("sex");
        //     
        // MatchQueryBuilder textPhrase = QueryBuilders.textPhrase("", null);

        // /////////////////////////////////////////////////////////////////////////////

//       List result = ElasticSearchDAO.searcherDNResourceRecords(prefixQuery, indexname,
//       type);
//       for (int i = 0; i < result.size(); i++) {
//           DNResourceRecord record = result.get(i);
//           System.out.println("(" + record.getId() + ")    :" + record.getOwner()+ "\t  :" + record.getRdata());
//       }
//       
//       GetResponse response = ElasticSearchDAO.getResponse(indexname, type, "2");

//       List registerInfos = ElasticSearchDAO.searcherDNRegisterInfos(prefixQuery, indexname, type);
//       for (DNRegisterInfo dnRegisterInfo : registerInfos) {
//           System.out.println("   :" + dnRegisterInfo.getName() + "       :"
//           + dnRegisterInfo.getRegistrantName() + " " + dnRegisterInfo.getUpdatedDate());
//       }

//      List list = ElasticSearchDAO.searcherESEntitys(prefixQuery, indexname, type);
//      for (ESEntity esEntity : list) {
//          System.out.println(esEntity);
//      }

        //      mapping
        ElasticSearchDAO.createIndexName("zone");
        try {
            ElasticSearchDAO.createMapping("zone", "records");
        } catch (Exception e) {
            e.printStackTrace();
        }

        // System.out.println("********************************");
        // ElasticSearchDAO.deleteResponse(indexname, type, "sex4.tld.");
//       ElasticSearchDAO.updataResponse(indexname, type, "sex4.tld.", "id", "sex4.tld._info");

        ElasticSearchDAO.closeClient();
    }
}

좋은 웹페이지 즐겨찾기