Elasticsearch Java API (二): index建立刪除 cluster管理

來源:互聯網
上載者:User

標籤:資料   虛擬   dock   min   列印   類型   plugin   turn   depend   

Elasticsearch Java API (二): index建立刪除 cluster管理 

elastic官網有權威的java api 英文的 需要耐心看 這裡整理下基本操作

建立maven工程添加依賴

        <dependency>            <groupId>org.elasticsearch.client</groupId>            <artifactId>transport</artifactId>            <version>5.2.2</version>        </dependency>        <dependency>            <groupId>net.sf.json-lib</groupId>            <artifactId>json-lib</artifactId>            <version>2.4</version>        </dependency>        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-api</artifactId>            <version>2.7</version>        </dependency>        <dependency>            <groupId>org.apache.logging.log4j</groupId>            <artifactId>log4j-core</artifactId>            <version>2.7</version>        </dependency>        <dependency>            <groupId>org.locationtech.spatial4j</groupId>            <artifactId>spatial4j</artifactId>            <version>0.6</version>        </dependency>        <dependency>            <groupId>com.vividsolutions</groupId>            <artifactId>jts</artifactId>            <version>1.13</version>            <exclusions>                <exclusion>                    <groupId>xerces</groupId>                    <artifactId>xercesImpl</artifactId>                </exclusion>            </exclusions>        </dependency>        <dependency>            <groupId>org.elasticsearch.plugin</groupId>            <artifactId>delete-by-query</artifactId>            <version>2.4.1</version>        </dependency>

 

 

首先自訂一個client用於串連es

/** * Created by forgeeks at 2017-03-22 18:27 */public class MyClient {    private Settings settings;    private TransportClient client;    private IndexResponse response;    public IndexResponse getResponse() {        return response;    }    public void setResponse(IndexResponse response) {        this.response = response;    }    public TransportClient getClient() {        return client;    }    public MyClient(String clusterName, String inetAddress, Integer port) throws UnknownHostException {        settings = Settings.builder().put("cluster.name", clusterName).build();        client = new PreBuiltTransportClient(settings).addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(inetAddress), port));    }    public void close(){        getClient().close();    }}

 串連方法很簡單,照著官方的抄就可以了

接下來看看串連的cluster狀態

    /**     * 列印叢集健康狀態     *     * @param client     */    public static void printClustesInfo(MyClient client) {        ClusterHealthResponse healths = client.getClient().admin().cluster().prepareHealth().get();        Map<String, ClusterIndexHealth> map = healths.getIndices();        String clusterName = healths.getClusterName();        int numberOfDataNodes = healths.getNumberOfDataNodes();        int numberOfNodes = healths.getNumberOfNodes();        System.out.println("叢集名稱: " + clusterName + "     資料節點數:" + numberOfDataNodes + "   得到節點數:" + numberOfNodes + "  索引數:" + map.size());        for (String key : map.keySet()) {            System.out.print(key + "  ");        }           }

 

再看下怎麼建立index,首先要明白像資料庫一樣,如果已存在還建立就會拋異常,所以先檢查

    /**判斷index是否存在     *     * @param client     * @param index     * @return     */    public static boolean indexExists(MyClient client, String index) {        IndicesExistsRequest request = new IndicesExistsRequest(index);        IndicesExistsResponse response = client.getClient().admin().indices().exists(request).actionGet();        if (response.isExists()) return true;        else return false;    }    /**     * 建立索引和類型     *     * @param client     * @param index     * @param type     * @throws IOException     */    public static void createIndex(MyClient client, String index, String type) throws IOException {        if (indexExists(client, index)) {            System.out.println("index 已存在");            return;        }        IndexResponse response = client.getClient().prepareIndex(index, type)                .setSource(jsonBuilder().startObject().endObject()).get();        client.setResponse(response);        System.out.print(response.toString() + "索引成功建立");    }

建立成功console會有資訊

或者敲命令列 要注意的是win和linux下curl 不一定能用 只有安裝了才能用 win下的即使能用也不要用 不是針對 建個虛擬機器 按(一)的方法再跑一台es 叢集設定下 後面再說

叢集 再用xshell連 可以直接用linux下的curl 然後會徹底解放

[[email protected] config]# curl -XGET  132.126.3.180:9200/_cat/indices?v

 

好了,接下來是(三)搜尋

 

Elasticsearch Java API (二): index建立刪除 cluster管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.