首页 > 代码库 > Elasticsearch Java API (二): index创建删除 cluster管理
Elasticsearch Java API (二): index创建删除 cluster管理
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 然后会彻底解放
[root@DOCKER-9 config]# curl -XGET 132.126.3.180:9200/_cat/indices?v
好了,接下来是(三)搜索
Elasticsearch Java API (二): index创建删除 cluster管理
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。