这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
PUT product001
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "keyword",
"ignore_above": 256
},
"description": {
"type": "text"
},
"price": {
"type": "integer"
}
}
}
}
@Autowired
private ElasticsearchClient elasticsearchClient;
@Override
public void create(String name,
Function<IndexSettings.Builder, ObjectBuilder<IndexSettings>> settingFn,
Function<TypeMapping.Builder, ObjectBuilder<TypeMapping>> mappingFn) throws IOException {
elasticsearchClient
.indices()
.create(c -> c
.index(name)
.settings(settingFn)
.mappings(mappingFn)
);
}
@SpringBootTest
class EsServiceImplTest {
@Autowired
EsService esService;
@Test
void create() throws Exception {
// 索引名
String indexName = "product002";
// 构建setting时,builder用到的lambda
Function<IndexSettings.Builder, ObjectBuilder<IndexSettings>> settingFn = sBuilder -> sBuilder
.index(iBuilder -> iBuilder
// 三个分片
.numberOfShards("3")
// 一个副本
.numberOfReplicas("1")
);
// 新的索引有三个字段,每个字段都有自己的property,这里依次创建
Property keywordProperty = Property.of(pBuilder -> pBuilder.keyword(kBuilder -> kBuilder.ignoreAbove(256)));
Property textProperty = Property.of(pBuilder -> pBuilder.text(tBuilder -> tBuilder));
Property integerProperty = Property.of(pBuilder -> pBuilder.integer(iBuilder -> iBuilder));
// // 构建mapping时,builder用到的lambda
Function<TypeMapping.Builder, ObjectBuilder<TypeMapping>> mappingFn = mBuilder -> mBuilder
.properties("name", keywordProperty)
.properties("description", textProperty)
.properties("price", integerProperty);
// 创建索引,并且指定了setting和mapping
esService.create(indexName, settingFn, mappingFn);
}
}
动手实践之前,有个问题先思考一下
刚才咱们写了那么多代码,才能创建出CreateIndexResponse对象(注意代码:elasticsearchClient.indices().create),怎么就能用JSON轻易的创建出来呢?有什么直接证据或者关键代码吗?
来看看CreateIndexResponse的builder的源码,集成了父类,也实现了接口,
public static class Builder extends WithJsonObjectBuilderBase<Builder>
implements
ObjectBuilder<CreateIndexRequest> {
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!-- 请改为自己项目的parent坐标 -->
<parent>
<artifactId>elasticsearch-tutorials</artifactId>
<groupId>com.bolingcavalry</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- 请改为自己项目的artifactId -->
<artifactId>object-from-json</artifactId>
<packaging>jar</packaging>
<!-- 请改为自己项目的name -->
<name>object-from-json</name>
<url>https://github.com/zq2599</url>
<!--不用spring-boot-starter-parent作为parent时的配置-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 不加这个,configuration类中,IDEA总会添加一些提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<!-- exclude junit 4 -->
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- junit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<!-- elasticsearch引入依赖 start -->
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!-- 使用spring boot Maven插件时需要添加该依赖 -->
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 需要此插件,在执行mvn test命令时才会执行单元测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
<configuration>
<skipTests>false</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
</build>
</project>
package com.bolingcavalry.fromjson;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FromJsonApplication {
public static void main(String[] args) {
SpringApplication.run(FromJsonApplication.class, args);
}
}
@ConfigurationProperties(prefix = "elasticsearch") //配置的前缀
@Configuration
public class ClientConfig {
@Setter
private String hosts;
/**
* 解析配置的字符串,转为HttpHost对象数组
* @return
*/
private HttpHost[] toHttpHost() {
if (!StringUtils.hasLength(hosts)) {
throw new RuntimeException("invalid elasticsearch configuration");
}
String[] hostArray = hosts.split(",");
HttpHost[] httpHosts = new HttpHost[hostArray.length];
HttpHost httpHost;
for (int i = 0; i < hostArray.length; i++) {
String[] strings = hostArray[i].split(":");
httpHost = new HttpHost(strings[0], Integer.parseInt(strings[1]), "http");
httpHosts[i] = httpHost;
}
return httpHosts;
}
@Bean
public ElasticsearchClient elasticsearchClient() {
HttpHost[] httpHosts = toHttpHost();
RestClient restClient = RestClient.builder(httpHosts).build();
RestClientTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// And create the API client
return new ElasticsearchClient(transport);
}
}
elasticsearch:
# 多个IP逗号隔开
hosts: 127.0.0.1:9200
public interface EsService {
/**
* 以InputStream为入参创建索引
* @param name 索引名称
* @param inputStream 包含JSON内容的文件流对象
*/
void create(String name, InputStream inputStream) throws IOException;
}
@Service
public class EsServiceImpl implements EsService {
@Autowired
private ElasticsearchClient elasticsearchClient;
@Override
public void create(String name, InputStream inputStream) throws IOException {
// 根据InputStrea创建请求对象
CreateIndexRequest request = CreateIndexRequest.of(builder -> builder
.index(name)
.withJson(inputStream));
elasticsearchClient.indices().create(request);
}
}
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "keyword",
"ignore_above": 256
},
"description": {
"type": "text"
},
"price": {
"type": "integer"
}
}
}
}
@Test
void createByInputStream() throws Exception {
// 文件名
String filePath = "/Users/will/temp/202206/25/product003.json";
// 索引名
String indexName = "product003";
// 通过InputStream创建索引
esService.create(indexName, new FileInputStream(filePath));
}
default T withJson(Reader input) {
JsonpMapper mapper = SimpleJsonpMapper.INSTANCE_REJECT_UNKNOWN_FIELDS;
return withJson(mapper.jsonProvider().createParser(input), mapper);
}
/**
* 以Reader为入参创建索引
* @param name 索引名称
* @param reader 包含JSON内容的文件流对象
*/
void create(String name, Reader reader) throws IOException;
@Override
public void create(String name, Reader reader) throws IOException {
// 根据Reader创建请求对象
CreateIndexRequest request = CreateIndexRequest.of(builder -> builder
.index(name)
.withJson(reader));
elasticsearchClient.indices().create(request);
}
json文件继续使用刚才创建的product003.json文件
单元测试代码中也增加一个方法,用于验证刚才写的create方法
@Test
void createByReader() throws Exception {
// 文件名
String filePath = "/Users/will/temp/202206/25/product003.json";
// 索引名
String indexName = "product004";
// 通过InputStream创建索引
esService.create(indexName, new FileReader(filePath));
}
public class FileReader extends InputStreamReader {
public FileReader(String fileName) throws FileNotFoundException {
super(new FileInputStream(fileName));
}
接下来要验证的是用字符串来创建请求对象,这个比较实用,用字符串创建请求对象,给我们的应用开发提供了很大的自由度,废话少说,开始写代码
首先还是给EsService接口新增一个方法,入参是索引名称和JSON字符串
/**
* 以字符串为入参创建索引
* @param name 索引名称
* @param jsonContent 包含JSON内容的字符串
*/
void create(String name, String jsonContent) throws IOException;
@Override
public void create(String name, String jsonContent) throws IOException {
// 根据Reader创建请求对象
CreateIndexRequest request = CreateIndexRequest.of(builder -> builder
.index(name)
.withJson(new StringReader(jsonContent)));
elasticsearchClient.indices().create(request);
}
@Test
void createByString() throws Exception {
// 文件名
String jsonContent = "{\n" +
" \"settings\": {\n" +
" \"number_of_shards\": 3,\n" +
" \"number_of_replicas\": 1\n" +
" },\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"keyword\",\n" +
" \"ignore_above\": 256\n" +
" },\n" +
" \"description\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"price\": {\n" +
" \"type\": \"integer\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}\n";
// 索引名
String indexName = "product005";
// 通过InputStream创建索引
esService.create(indexName, jsonContent);
}
T withJson(JsonParser parser, JsonpMapper mapper)
public interface WithJson<T> {
default T withJson(InputStream input) {
JsonpMapper mapper = SimpleJsonpMapper.INSTANCE_REJECT_UNKNOWN_FIELDS;
return withJson(mapper.jsonProvider().createParser(input), mapper);
}
default T withJson(Reader input) {
JsonpMapper mapper = SimpleJsonpMapper.INSTANCE_REJECT_UNKNOWN_FIELDS;
return withJson(mapper.jsonProvider().createParser(input), mapper);
}
T withJson(JsonParser parser, JsonpMapper mapper);
}
Reader queryJson = new StringReader(
"{" +
" \"query\": {" +
" \"range\": {" +
" \"@timestamp\": {" +
" \"gt\": \"now-1w\"" +
" }" +
" }" +
" }" +
"}");
SearchRequest aggRequest = SearchRequest.of(b -> b
.withJson(queryJson)
.aggregations("max-cpu", a1 -> a1
.dateHistogram(h -> h
.field("@timestamp")
.calendarInterval(CalendarInterval.Hour)
)
.aggregations("max", a2 -> a2
.max(m -> m.field("host.cpu.usage"))
)
)
.size(0)
);
Map<String, Aggregate> aggs = client
.search(aggRequest, Void.class)
.aggregations();
String json = "{ " +
" \"type\": \"text\"," +
" \"fields\": {" +
" \"some_field\": { " +
" \"type\": \"keyword\"," +
" \"normalizer\": \"lowercase\"" +
" }" +
" }" +
" }";
Property p = Property.of(b -> b
.withJson(new StringReader(json))
);
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
向ES发送请求时,如何创建请求对象呢?官方推荐的builder patter,在面对复杂的请求对象结构时还好用吗?有没有更加直观简洁的方法,尽在本文一网打尽
SpringBoot应用中操作es8,本篇重点是如何连接带安全检查的es8服务端(https、账号密码、API Key)
正式使用官方的Java API Client操作ES之前,将与之有关的重要知识点先做一轮串讲,后面开始编码时,疑点已扫清,可以愉快而顺畅的实现业务功能