elasticsearch工具类

import com.alibaba.druid.pool.DruidConnectionHolder;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.bonc.uni.config.SystemConfig;
import com.bonc.usdp.odk.common.collection.MapUtil;
import com.bonc.usdp.odk.common.string.StringUtil;
import com.bonc.usdp.odk.logmanager.LogManager;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.apache.commons.dbutils.QueryRunner;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ES连接管理类 支持使用 odk-elasticsearch、sql4es 两种方式创建ES连接
 */
public class ESManager {

    private static ESManager instance = null;

    /**
     * odk-elasticsearch 连接接口
     */
//    private ISearchService searchService;

    /**
     * sql4es 数据源连接池
     */
    private static Map<String, DruidDataSource> esDataSources;

    private ESManager() throws UnknownHostException {
        /*searchService = new SearchServiceImpl(SystemConfig.ELASTICSEARCH_IP, SystemConfig.ELASTICSEARCH_PORT,
                SystemConfig.ELASTICSEARCH_NAME);*/
        esDataSources = Maps.newHashMap();
    }

    public static synchronized ESManager getInstance() throws UnknownHostException {
        if (instance == null) {
            instance = new ESManager();
            addShutDownHook();
        }
        return instance;
    }

    /*public ISearchService getSearchService() {
        return searchService;
    }*/

  
    private static String getEsIndexUrl(String index) {
        return "jdbc:sql4es://" + SystemConfig.ELASTICSEARCH_IP + ":" + SystemConfig.ELASTICSEARCH_PORT + "/" + index
                + "?" + "cluster.name=" + SystemConfig.ELASTICSEARCH_NAME;
    }

    /**
     * 创建指定索引的连接池
     *
     * @param index
     * @return
     */
    private static synchronized DruidDataSource getDataSource(String index) {
        if (StringUtil.isEmpty(index)) {
            return null;
        }
        if (esDataSources == null) {
            esDataSources = Maps.newHashMap();
        }

        DruidDataSource dataSource = esDataSources.get(index);
        if (dataSource == null) {
            try {
                ImmutableMap<String, String> conf = ImmutableMap.<String, String> builder()
                        .put(DruidDataSourceFactory.PROP_DRIVERCLASSNAME, "com.bonc.usdp.sql4es.jdbc.ESDriver")
                        .put(DruidDataSourceFactory.PROP_URL, getEsIndexUrl(index))
                        .put(DruidDataSourceFactory.PROP_TESTWHILEIDLE, "false")
                        .put(DruidDataSourceFactory.PROP_MAXACTIVE, "10")
                        .put(DruidDataSourceFactory.PROP_MINIDLE, "3")
                        .put(DruidDataSourceFactory.PROP_INITIALSIZE, "10")
                        .put(DruidDataSourceFactory.PROP_MAXWAIT, "60000")
                        .put(DruidDataSourceFactory.PROP_POOLPREPAREDSTATEMENTS, "true")
                        .put(DruidDataSourceFactory.PROP_MAXOPENPREPAREDSTATEMENTS, "20")

                        // .put(DruidDataSourceFactory.PROP_VALIDATIONQUERY,
                        // "SELECT 'x'")
                        .build();
                dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(conf);
                esDataSources.put(index, dataSource);
            } catch (Exception e) {
                LogManager.Exception(e);
            }
        }
        return dataSource;
    }

    /**
     * 关闭指定索引的连接并从连接池中删除
     *
     * @param index
     */
    public static synchronized void closeDataSource(String index) {
        if (StringUtil.isEmpty(index) || esDataSources == null) {
            return;
        }
        DruidDataSource dataSource = esDataSources.get(index);
        if (dataSource != null) {
            dataSource.close();
            esDataSources.remove(index);
        }
    }

    private static void addShutDownHook() {
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                if (!MapUtil.isEmpty(esDataSources)) {
                    esDataSources.forEach((index, ds) -> ds.close());
                }
            }
        });
    }

    /**
     * 从池中获取指定索引的连接
     *
     * @return
     */
    public static Connection getConnection() {
        Connection conn = null;
        DruidDataSource dataSource = getDataSource(SystemConfig.ELASTICSEARCH_INDEX);
        try {
            // 跳过获取连接验证支持holdability
            DruidConnectionHolder.holdabilityUnsupported = true;
            conn = dataSource.getConnection();
        } catch (SQLException e) {
            LogManager.Exception(e);
        }
        return conn;
    }

    public static QueryRunner getQueryRunner(String index) {
        if (StringUtil.isEmpty(index)) {
            return null;
        }
        DruidDataSource dataSource = getDataSource(index);
        return new QueryRunner(dataSource);
    }

    /**
     * 关闭索引连接
     *
     * @param conn
     * @param statement
     * @param resultSet
     */
    public static void close(Connection conn, Statement statement, ResultSet resultSet) {
        try {
            if (conn != null)
                conn.close();
            if (statement != null)
                statement.close();
            if (resultSet != null)
                resultSet.close();
        } catch (SQLException e) {
            LogManager.Exception(e);
        }
    }

    /**
     * 获取语料库客户端
     *
     * @param
     */
    public static TransportClient init() {
        TransportClient client = null;
        Settings setting = Settings.builder().put("cluster.name", "text").build();
        try {
            client = new PreBuiltTransportClient(setting).
                    addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.16.11.126"), 9500));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return client;
    }

    /**
     * 创建索引库
     *
     * @param
     */
    public static void insert(TransportClient client, String type, String text, int sentence_status,
                              String create_time, String article_id,
                              String task_id, String back_reson
    ) {
        try {
            client.prepareIndex("corpus-annotation", type).setSource(XContentFactory.jsonBuilder()
                    .startObject()
                    .field("text", text)
                    .field("sentence_status", sentence_status)
                    .field("create_time", create_time)
                    .field("task_id", task_id)
                    .field("back_reson", back_reson)
                    .field("corpus_info_id", article_id)
//                    .field(field,"")
                    .endObject())
                    .get();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建索引库
     * @param
     */
    public static void insert(String type,String text,int sentence_status,
                              String create_time,String article_id,
                              String task_id, String back_reson
                              ){
        Settings setting = Settings.builder().put("cluster.name", "text").build();
        TransportClient client = null;
        try {
            client = new PreBuiltTransportClient(setting).
                    addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.16.11.126"), 9500));

            client.prepareIndex("corpus-annotation", type).setSource(XContentFactory.jsonBuilder()
                    .startObject()
                    .field("text",text)
                    .field("sentence_status",sentence_status)
                    .field("create_time",create_time)
                    .field("task_id",task_id)
                    .field("back_reson",back_reson)
                    .field("corpus_info_id",article_id)
//                    .field(field,"")
                    .endObject())
                    .get();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取语料库客户端
     * @param
     */
    public static TransportClient getClient(){
        Settings setting = Settings.builder().put("cluster.name", "text").build();
        TransportClient client =null;
        try {
             client = new PreBuiltTransportClient(setting).
                    addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("172.16.11.126"), 9500));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return client;
    }



    public static void main(String[] args) {
        String content = "  独角兽企业上市,将对我国资本市场格局会产生深远影响,A股玩法也要变了。   独角兽概念股连日上攻。昨天(3月5日)安彩高科、普路通、佳都科技、精达股份等个股均封涨停板。   概念股涨停背后是频频拂面的政策春风。   2018年政府工作报告,明确提出“支持优质创新型企业上市融资”。";

        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher m = p.matcher(content);
        String clean = m.replaceAll("");
        System.out.println("clean");
        System.out.println(clean);
    }
}

猜你喜欢

转载自blog.csdn.net/Cassiel717/article/details/88862935
今日推荐