Elasticsearch Java API (ii): index create delete cluster management

Source: Internet
Author: User
Tags log4j

Elasticsearch Java API (ii): index create delete cluster management

Elastic official website has the authoritative Java API English needs to be patient to see here to tidy up the basic operation

Create a MAVEN project to add dependencies

        <dependency> <groupId>org.elasticsearch.client</groupId> <artifactid&gt ;transport</artifactid> <version>5.2.2</version> </dependency> <depend Ency> <groupId>net.sf.json-lib</groupId> <artifactid>json-lib</artifactid&gt            ; <version>2.4</version> </dependency> <dependency> <groupid>org.apac He.logging.log4j</groupid> <artifactId>log4j-api</artifactId> &LT;VERSION&GT;2.7&L t;/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</arti                    factid> <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</artifacti D> <version>2.4.1</version> </dependency>

First, customize a client to connect ES

/*** Created by Forgeeks at 2017-03-22 18:27*/ Public classmyclient {PrivateSettings Settings; Privatetransportclient Client; Privateindexresponse response;  Publicindexresponse GetResponse () {returnresponse; }     Public voidSetresponse (indexresponse response) { This. Response =response; }     Publictransportclient getclient () {returnclient; }     PublicMyClient (string clustername, String inetaddress, Integer port)throwsunknownhostexception {Settings= Settings.builder (). Put ("Cluster.name", ClusterName). Build (); Client=NewPrebuilttransportclient (Settings). addtransportaddress (Newinetsockettransportaddress (Inetaddress.getbyname (inetaddress), port)); }     Public voidClose () {getclient (). Close (); }}

The connection method is very simple, according to the official copy on it.

Next look at the cluster status of the connection

    /*** Print Cluster health status * *@paramClient*/     Public Static voidPrintclustesinfo (myclient client) {clusterhealthresponse healths=client.getclient (). Admin (). cluster (). Preparehealth (). get (); Map<string, clusterindexhealth> map =healths.getindices (); String clustername=Healths.getclustername (); intNumberofdatanodes =healths.getnumberofdatanodes (); intNumberofnodes =healths.getnumberofnodes (); System.out.println ("Cluster Name:" + clustername + "Data node Number:" + numberofdatanodes + "Get Node Number:" + numberofnodes + "index number:" +map.size ());  for(String key:map.keySet ()) {System.out.print (key+ "  "); }           }

Look at how to create INDEX, first of all to understand the same as the database, if it is already existing and new will throw an exception, so first check

    /**determine if index exists * *@paramClient *@paramIndex *@return     */     Public Static Booleanindexexists (myclient client, String index) {indicesexistsrequest request=Newindicesexistsrequest (index); Indicesexistsresponse Response=client.getclient (). Admin (). Indices (). exists (Request). Actionget (); if(Response.isexists ())return true; Else return false; }    /*** CREATE INDEX and type * *@paramClient *@paramIndex *@paramtype *@throwsIOException*/     Public Static voidCreateIndex (myclient Client, string index, String type)throwsIOException {if(indexexists (client, index)) {SYSTEM.OUT.PRINTLN ("Index already exists"); return; } indexresponse Response=client.getclient (). Prepareindex (index, type). SetSource (Jsonbuilder (). StartObject (). EndObject ()). Get ()        ;        Client.setresponse (response); System.out.print (response.tostring ()+ "Index successfully created"); }

Creating a successful console will have information

Or hit the command line to note that the win and Linux under the curl may not be able to use only installed to use the win even if you can use it or not to build a virtual machine according to (a) the way to run another ES cluster setup next again

The cluster re-use Xshell can be used directly with the Linux curl and will be completely liberated.

[Email protected] config]# curl-xget  132.126.3.180:9200/_cat/indices?v

Okay, here we are. (c) Search

Elasticsearch Java API (ii): index create delete cluster management

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.