elasticsearch5.4的java api
来源:互联网 发布:四川网络电视台 编辑:程序博客网 时间:2024/06/05 16:31
0、先说一个题外话,安装jar包到maven仓库
1、 创建maven项目,要把create a simple project勾选上
2、 配置pom.xml。
这步很重要,由于要使用spring boot,所以这里要添加
<properties> <elasticsearch.version>5.4.3</elasticsearch.version></properties>
Spring boot的配置
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version></parent><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>
由于使用了x-pack管理elasticsearch,所以参见官网:
https://www.elastic.co/guide/en/x-pack/5.4/api-java.html
<repositories> <!-- add the elasticsearch repo --> <repository> <id>elasticsearch-releases</id> <url>https://artifacts.elastic.co/maven</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>x-pack-transport</artifactId> <version>5.4.3</version> </dependency>
Elasticsearch的配置
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.4.3</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.8.2</version><!--$NO-MVN-MAN-VER$--> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version><!--$NO-MVN-MAN-VER$--> </dependency>
3、 创建源码
package org.cma.hlj.bigdata.controller;import java.net.InetAddress;import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.cluster.health.ClusterHealthStatus;import org.elasticsearch.cluster.health.ClusterIndexHealth;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.InetSocketTransportAddress;import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;public class Test { public static void main(String[] args){ Settings set=Settings.builder().put("cluster.name","cmabd") .put("xpack.security.transport.ssl.enabled",false) .put("xpack.security.user", "xxx:xxx") .put("client.transport.sniff",true).build(); TransportClient client=null; try{ client=new PreBuiltXPackTransportClient(set) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.96.xx.xx"), 9300)) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("10.96.xx.xx"), 9300)); ClusterHealthResponse healths = client.admin().cluster().prepareHealth().get(); String clusterName = healths.getClusterName(); int numberOfDataNodes = healths.getNumberOfDataNodes(); int numberOfNodes = healths.getNumberOfNodes(); System.out.println(clusterName+" "+numberOfDataNodes+" "+numberOfNodes); for (ClusterIndexHealth health : healths.getIndices().values()) { String index = health.getIndex(); int numberOfShards = health.getNumberOfShards(); int numberOfReplicas = health.getNumberOfReplicas(); ClusterHealthStatus status = health.getStatus(); System.out.println(index+" "+numberOfShards+" "+numberOfReplicas+" "+status); } GetResponse response = client.prepareGet("cts_recv", "comlog", "AV01rfXRF3gNro0o5jWE").get(); System.out.println(response); } catch(Exception e) { } finally{ client.close(); } }}
4、 注意事项
Jdk的版本一定要8以上,要不然会报错。
阅读全文
0 0
- elasticsearch5.4的java api
- Elasticsearch5.4 Java API操作
- Elasticsearch5 JAVA API数据遍历
- Elasticsearch5.4集群(二)java API升级
- Elasticsearch5.0新的API规范
- Elasticsearch5.2.1的文档API操作
- Elasticsearch5.4 Java编程注意事项
- elasticsearch5.11集成ik+pinyin分词java api
- elasticsearch5.2.2使用java API创建transport client
- elasticsearch5.x的java实现搜索
- Elasticsearch5.4 安装中文插件的问题
- Elasticsearch5.0在java中调用需要的jar包
- ElasticSearch5.4.3 环境搭建 2017 (5-Java Client Security Api x-pack)
- ElasticSearch5.4.3 环境搭建 2017 (5.1-Java Api x-pack补充说明)
- ElasticSearch5.0的安装
- ElasticSearch5.4搭建
- Elasticsearch5.4 删除type
- elasticsearch5.4集群超时
- gulp使用小结
- 各类IT工具推荐总结(持续更新中)
- eclipse:将编码为GBK的Java源文件批量转为UTF-8
- 【Qt学习笔记】禁止改变窗口大小
- Linux日志文件总管——logrotate
- elasticsearch5.4的java api
- 关于MyBatis的一些知识点
- 并行计算openMP 的简单介绍
- 算法系列——Sort Colors
- qt学习之路1
- JavaScript字符串转换为变量名
- Web开发中常用的定位布局position
- linux 用户启动后加载文件
- Ruby 的变量(作用域)