1. 程式人生 > 實用技巧 >java 連線操作Elasticsearch(一)

java 連線操作Elasticsearch(一)

  • 匯入Elasticsearch依賴包
    <!-- Elasticsearch6.4.3 依賴 -->
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency
> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId
>org.elasticsearch.plugin</groupId> <artifactId>transport-netty4-client</artifactId> <version>${elasticsearch.version}</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId> <version>${elasticsearch.version}</version> </dependency>
  • properties配置檔案
elasticsearch.ip=es庫ip
elasticsearch.port=9300(es對外連線埠)
elasticsearch.cluster.name=elasticsearch
elasticsearch.shards=3
elasticsearch.replicas=1
elasticsearch.pool=3
elasticsearch.regenerateIndexEnabled=false
elasticsearch.syncDataEnabled=true
elasticsearch.sniff=true

  • 獲取連線
public static Connection getConn(){
        Properties properties = new Properties();
        // 使用ClassLoader載入properties配置檔案生成對應的輸入流
        InputStream in = ES_Utils.class.getClassLoader().getResourceAsStream("elasticsearch.properties");
        // 使用properties物件載入輸入流
        try {
            properties.load(in);
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        String url = properties.getProperty("postgresql.url");
        String driver = properties.getProperty("postgresql.driver");
        String user = properties.getProperty("postgresql.user");
        String password = properties.getProperty("postgresql.password");
        
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, password); //獲取連線
        } catch (ClassNotFoundException e){
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        return conn;
    }    
  • 獲取單利客戶端
public static TransportClient getSingleClient() {
        Properties properties = new Properties();
        // 使用ClassLoader載入properties配置檔案生成對應的輸入流
        InputStream in = ES_Utils.class.getClassLoader().getResourceAsStream("elasticsearch.properties");
        // 使用properties物件載入輸入流
        try {
            properties.load(in);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        String host = properties.getProperty("elasticsearch.ip");
        String cluster_name = properties.getProperty("elasticsearch.cluster.name");
        int port = Integer.parseInt(properties.getProperty("elasticsearch.port"));
        
        /*
         * int shards =
         * Integer.parseInt(properties.getProperty("elasticsearch.shards")); int
         * replicas =
         * Integer.parseInt(properties.getProperty("elasticsearch.replicas"));
         * String sniff = properties.getProperty("elasticsearch.sniff");
         */

        if (client == null) {
            synchronized (TransportClient.class) {
            }
            if (client == null) {
                try {
                    /**
                     * 配置資訊 client.transport.sniff 增加嗅探機制,找到ES叢集
                     * thread_pool.search.size 增加執行緒池個數,暫時設為3
                     */
                    Settings esSetting = Settings.builder().put("cluster.name", cluster_name).build();
                    // 配置資訊Settings自定義
                    client = new PreBuiltTransportClient(esSetting);
                    TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(host), port);
                    client.addTransportAddress(transportAddress);
                } catch (Exception e) {
                    LOGGER.error("elasticsearch TransportClient create error!!!", e);
                }
            }
        }
        return client;
    }
    
  • 獲取es庫客戶端
@SuppressWarnings("resource")
    public static ElasticsearchClient getEsClient(){
        Properties properties = new Properties();
        // 使用ClassLoader載入properties配置檔案生成對應的輸入流
        InputStream in = ES_Utils.class.getClassLoader().getResourceAsStream("elasticsearch.properties");
        // 使用properties物件載入輸入流
        try {
            properties.load(in);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        String host = properties.getProperty("elasticsearch.ip");
        int port = Integer.parseInt(properties.getProperty("elasticsearch.port"));

        try {
            es_client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return es_client;
    }