ElasticSearch 5.x Java API
程序员文章站
2022-06-21 14:41:53
...
1、添加数据
(1)创建Index
[root@node3 ~]# curl -X PUT 'node3:9200/accounts'
{"acknowledged":true,"shards_acknowledged":true,"index":"accounts"}
[root@node3 ~]#
(2)插入数据
[aaa@qq.com conf]# curl -X PUT 'node3:9200/accounts/person/1' -d '
> {
> "user": "张三",
> "title": "工程师",
> "desc": "数据库管理"
> }'
{"_index":"accounts","_type":"person","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":2,"failed":0},"created":true}
[aaa@qq.com conf]#
(3)检索数据
[root@node3 ~]# curl -X GET 'node3:9200/accounts'
{"accounts":{"aliases":{},"mappings":{"person":{"properties":{"desc":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"title":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"user":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}}},"settings":{"index":{"creation_date":"1513147834226","number_of_shards":"5","number_of_replicas":"1","uuid":"V4763Q5FRcm10hbWHcD6lg","version":{"created":"5060399"},"provided_name":"accounts"}}}}[root@node3 ~]#
2、创建Maven项目
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.hadron</groupId>
<artifactId>elasticSearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>elasticSearch</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<elasticsearch.version>5.6.3</elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.1.1</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>
</dependencies>
</project>
说明:实际上我使用的ES版本是5.6.5的,官网https://www.elastic.co/guide/en/x-pack/5.6/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>
...
<dependencies>
<!-- add the x-pack jar as a dependency -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>5.6.5</version>
</dependency>
...
</dependencies>
由于网络问题,这个自定义的Maven创库不能访问。
只好改用
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.1.1</version>
</dependency>
3、Java API测试程序
package cn.hadron.elasticSearch;
import java.net.InetAddress;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
public class Demo {
public static void main(String[] args) {
try {
// 设置集群名称
Settings settings = Settings.builder().put("cluster.name", "my cluster").build();
// 创建client
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.161"), 9300));
// 搜索数据
GetResponse response = client.prepareGet("accounts", "person", "1").execute().actionGet();
// 输出结果
System.out.println(response.getSourceAsString());
// 关闭client
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
运行结果
推荐阅读
-
Java生鲜电商平台-统一格式返回的API架构设计与实战
-
Elasticsearch 常用API
-
Java代码解决ElasticSearch的Result window is too large问题
-
HBase 系列(六)——HBase Java API 的基本使用
-
Java日期时间API系列5-----Jdk7及以前的日期时间类TimeUnit在并发编程中的应用
-
Java8 日期和时间API
-
Java日期时间API系列12-----Jdk8中java.time包中的新的日期时间API类,日期格式化,常用日期格式大全
-
Ehcache基于java API实现
-
Java日期时间API系列8-----Jdk8中java.time包中的新的日期时间API类的LocalDate源码分析
-
Java API学习教程之正则表达式详解