Elasticsearch:运用 Java 创建索引并写入数据
在我之前的文章 “Elasticsearch:Java 运用示例”,我讲述了如何在 Java 应用中创建一个索引,并写入数据。在今天的例子中,我来着重讲述如何有目的地创建按照我们需求的索引,并介绍几种常见的方法写入数据。
安装
我们首先参考如下的文章来安装我们需要的 Elasticsearch 及 Kibana:
- 如何在 Linux,MacOS 及 Windows 上进行安装 Elasticsearch
- Kibana:如何在 Linux,MacOS 及 Windows上安装 Elastic 栈中的 Kibana
针对我们如下的练习,我们的 Elasticsearch 的访问地址为 http://localhost:9200。
此外,针对 Elastic Stack 7.15 及以后的版本,强烈建议对 Elasticsearch 进行安全配置。你可以参考文章 “Elasticsearch:设置 Elastic 账户安全”。我的 Elasticsearch 集群的超级用户 elastic 的密码为 password。
创建 Java 应用
我们用自己喜欢的 IDE 创建一个 Java 应用。在本例中,我将创建一个 Maven 应用:
pom.xml
<?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"><modelVersion>4.0.0</modelVersion><groupId>org.liuxg</groupId><artifactId>Elasticsearch-Java</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>${elastic.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>${elastic.version}</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>${elastic.version}</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.11.1</version></dependency></dependencies><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target><elastic.version>7.15.0</elastic.version></properties></project>
在上面,我们创建一个叫做 ElasticsearchJava 的 class:
ElasticsearchJava.java
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;public class ElasticsearchJava {private static RestHighLevelClient client = null;private static synchronized RestHighLevelClient makeConnection() {final BasicCredentialsProvider basicCredentialsProvider = new BasicCredentialsProvider();basicCredentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "password"));if (client == null) {client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {@Overridepublic HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {httpClientBuilder.disableAuthCaching();return httpClientBuilder.setDefaultCredentialsProvider(basicCredentialsProvider);}}));}return client;}public static void main(String[] args) {client = makeConnection();}}
在上面,我们创建了一个和 Elasticsearch 的连接。请注意我们使用了超级用户 elastic 极其密码。如果我们没有为我们的集群设置密码的话,我们其实可以非常简单地使用如下的代码来进行连接:
private static synchronized RestHighLevelClient makeConnection() {if(client == null) {restHighLevelClient = new RestHighLevelClient(RestClient.builder( new HttpHost("localhost", "9200", "http")));}return client;}
接下来,我们参照 Elastic 官方文档 “Create Index API”,我们使用如下的代码:
public static void main(String[] args) throws IOException {client = makeConnection();CreateIndexRequest request = new CreateIndexRequest("employees");request.settings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0));CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);System.out.println("response id: " + createIndexResponse.index());}}
在上面,我们建立一个连接,并创建一个叫做 employees 的索引。请注意在索引的名字中,我们不可以有大写字母,否则会导致错误。在上面,我们设置 number_of_shards 为1, number_of_replicas 为0。上面代码的输出为:
response id: employees
我们可以使用 Kibana 来检查我们的结果:
从上面的结果中,我们可以看出来 employees 索引已经被成功地创建。
也许你对创建一个索引的 mapping 也感兴趣,那么你可以使用如下的代码来实现:
public static void main(String[] args) throws IOException {client = makeConnection();String mappings = "{\n" +" \"properties\": {\n" +" \"id\": {\n" +" \"type\": \"keyword\"\n" +" },\n" +" \"name\": {\n" +" \"type\": \"text\"\n" +" }\n" + " }\n" +"}";System.out.println("mapping is as follows: ");System.out.println(mappings);try {CreateIndexRequest request = new CreateIndexRequest("employees");request.settings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0));request.mapping(mappings, XContentType.JSON);CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);System.out.println("response id: " + createIndexResponse.index());} catch (Exception e) {
// e.printStackTrace();}}
上面代码运行的结果为:
mapping is as follows:
{"properties": {"id": {"type": "keyword"},"name": {"type": "text"}}
}
response id: employees
我们可以在 Kibana 中进行查看:
GET employees/_mapping
上面命令的结果为:
{"employees" : {"mappings" : {"properties" : {"id" : {"type" : "keyword"},"name" : {"type" : "text"}}}}
}
上面的代码类似于 Kibana 中如下的命令:
PUT employees
{"settings": {"number_of_shards": 1, "number_of_replicas": 0}, "mappings": {"properties": {"id": {"type": "keyword"},"name": {"type": "text"}}}
}
上面的命令创建一个叫做 employees 的索引,并对它进行设置和定义 mappings。
接下来,我们参照另外一个文档 “Index API” 来对已经创建的索引进行写入操作:
// Write documents into employees indexIndexRequest request = new IndexRequest("employees");request.id("1");String jsonString = "{" +"\"id\":\"1\"," +"\"name\":\"liuxg\"" +"}";request.source(jsonString, XContentType.JSON);IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);System.out.println("response id: "+indexResponse.getId());System.out.println("response name: "+indexResponse.getResult().name());
上面的代码想已经创建的 employees 索引写入文档。其中的文档时以 JSON 形式写入的。当然如果我们之前没有创建 employees 这个索引,上面的 API 也将会自动帮我们生成 employees 这个索引,并把相应的文档写入。当然这个索引的 settings 及 mappings 也许不是我们想要的,而是系统按照默认的方式给出的。
上面的命令类似于在 Kibana 中的如下的命令:
PUT employees/_doc/1
{"id": "1","name": "liuxg"
}
我们重新编译并运行我们的代码:
mapping is as follows:
{"properties": {"id": {"type": "keyword"},"name": {"type": "text"}}
}
response id: 1
response name: CREATED
运行完后,我们可以在 Kibana 中通过如下的方式来进行查看:
GET employees/_search
上面的命令显示的结果为:
{"took" : 7,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 1,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "employees","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"id" : "1","name" : "liuxg"}}]}
}
我们可以看到文档已经被正确地写入了。
接下来,我们介绍另外一种写入的方法:
// Method 2: Write documents into employees indexMap<String, Object> jsonMap = new HashMap<>();jsonMap.put("id", "2");jsonMap.put("name", "Nancy");IndexRequest indexRequest = new IndexRequest("employees").id("2").source(jsonMap);IndexResponse indexResponse2 = client.index(indexRequest, RequestOptions.DEFAULT);System.out.println("response id: "+indexResponse2.getId());System.out.println("response name: "+indexResponse2.getResult().name());
运行代码:
mapping is as follows:
{"properties": {"id": {"type": "keyword"},"name": {"type": "text"}}
}
response id: 1
response name: UPDATED
response id: 2
response name: CREATED
我们重新在 Kibana 中进行查看:
GET employees/_search
上面的命令显示的结果为:
{"took" : 165,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 2,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "employees","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"id" : "1","name" : "liuxg"}},{"_index" : "employees","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"name" : "Nancy","id" : "2"}}]}
}
我们看到文档 2 已经被正确地写入。
按照官方的文档,我们可以有另外一种写入的方法:
// Method 3: Write documents into employees indexXContentBuilder builder = XContentFactory.jsonBuilder();builder.startObject();{builder.field("id", "3");builder.field("name", "Jason");}builder.endObject();IndexRequest indexRequest3 = new IndexRequest("employees").id("3").source(builder);IndexResponse indexResponse3 = client.index(indexRequest3, RequestOptions.DEFAULT);System.out.println("response id: "+indexResponse3.getId());System.out.println("response name: "+indexResponse3.getResult().name());
还有:
// Method 4: Write documents into employees indexIndexRequest indexRequest4 = new IndexRequest("employees").id("4").source("id", "4","name", "Mark");IndexResponse indexResponse4 = client.index(indexRequest4, RequestOptions.DEFAULT);System.out.println("response id: "+indexResponse4.getId());System.out.println("response name: "+indexResponse4.getResult().name());
运行上面的代码,我们可以在 Kibana 中进行查看:
"hits" : [{"_index" : "employees","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"id" : "1","name" : "liuxg"}},{"_index" : "employees","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"name" : "Nancy","id" : "2"}},{"_index" : "employees","_type" : "_doc","_id" : "3","_score" : 1.0,"_source" : {"id" : "3","name" : "Jason"}},{"_index" : "employees","_type" : "_doc","_id" : "4","_score" : 1.0,"_source" : {"id" : "4","name" : "Mark"}}]
最后,我们创建一个叫做 Employee 的 Java class:
Employee.java
public class Employee {private String id;private String name;public Employee(String id, String name) {this.id = id;this.name = name;}public String getId() {return id;}public String getName() {return name;}public void setId(String id) {this.id = id;}public void setName(String name) {this.name = name;}
}
在上面一定要注意的是要实现 setters 及 getters。
我们接下来使用如下的代码来写入:
// Method 5: Write documents into employees indexEmployee employee = new Employee("5", "Martin");IndexRequest indexRequest5 = new IndexRequest("employees");indexRequest.id("5");indexRequest.source(new ObjectMapper().writeValueAsString(employee), XContentType.JSON);IndexResponse indexResponse5 = client.index(indexRequest, RequestOptions.DEFAULT);System.out.println("response id: "+indexResponse5.getId());System.out.println("response name: "+indexResponse5.getResult().name());
重新运行代码,并在 Kibana 中进行查看:
GET employees/_search
我们可以看到如下的结果:
"hits" : [{"_index" : "employees","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"id" : "1","name" : "liuxg"}},{"_index" : "employees","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"name" : "Nancy","id" : "2"}},{"_index" : "employees","_type" : "_doc","_id" : "3","_score" : 1.0,"_source" : {"id" : "3","name" : "Jason"}},{"_index" : "employees","_type" : "_doc","_id" : "4","_score" : 1.0,"_source" : {"id" : "4","name" : "Mark"}},{"_index" : "employees","_type" : "_doc","_id" : "5","_score" : 1.0,"_source" : {"id" : "5","name" : "Martin"}}]
为了方便大家学习,我把源码放于 github:https://github.com/liu-xiao-guo/Elasticsearch-java
Elasticsearch:运用 Java 创建索引并写入数据相关推荐
- Python读取多个excel文件(删除字段、数据格式转换、dataframe多表合并)并写入ElasticSearch实战(自动创建索引、写入ElasticSearch、探索性数据分析)
Python读取多个excel文件(删除字段.数据格式转换.dataframe多表合并)并写入ElasticSearch实战(自动创建索引.写入ElasticSearch.探索性数据分析) 目录
- java pdf 报表_关于Java向PDF模板写入数据,以及java制作pdf报表的问题
1.关于java向PDF模板写入数据 写的特别全一步一步来就行. (1)首先下载一个pdf编辑工具Adobe Acrobat DC (2)准备一个pdf模板文件(你可以把自己创建一个只有表头没有内容的 ...
- Java创建文件并写入字符串
Java创建文件并写入字符串,如果文件存在则只写入字符串. String sourceString = "sourceString"; //待写入字符串 byte[] source ...
- php往文件里面写入数据,PHP创建文件及写入数据(覆盖写入,追加写入)的方法详解...
本文实例讲述了PHP创建文件及写入数据(覆盖写入,追加写入)的方法.分享给大家供大家参考,具体如下: 这里主要介绍了PHP创建文件,并向文件中写入数据,覆盖,追加的实现代码,需要的朋友可以参考下: 创 ...
- php追加写入,PHP创建文件及写入数据(覆盖写入,追加写入)的方法详解
本文实例讲述了PHP创建文件及写入数据(覆盖写入,追加写入)的方法.分享给大家供大家参考,具体如下: 这里主要介绍了PHP创建文件,并向文件中写入数据,覆盖,追加的实现代码,需要的朋友可以参考下: 创 ...
- .NET Core(C#) EPPlus创建Excel(.xlsx)写入数据的方法及示例代码
EPPlus是一个使用Open Office XML(Xlsx)文件格式,能读写Excel(.xlsx)文件的开源组件.本文主要介绍.NET Core(C#)中使用EPPlus创建Excel(.xls ...
- java向Excel文件写入数据
package cn.ccaih.read;import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.userm ...
- elasticsearch kabana中创建索引
在kabana中创建索引和索引类型语法 PUT clockin { "mappings": { "time": { } } } 查询索引下的所有数据 GET c ...
- es的基本操作(创建索引,添加数据,删除数据,判断索引是否存在)
1.创建索引+ik分词器 /*** 创建es索引* $indexName 索引名称*/public function createEsIndex($indexName){$esClient = $th ...
最新文章
- cisco路由器基本实验之一 静态路由的配置(Boson NetSim
- 转 10 个最佳的 Node.js 的 MVC 框架
- Typora 快捷键
- Linux workqueue工作原理 【转】
- java学习(113):Calendar类
- Effective Java~9. try-with-resource 优先于 try-catch
- Hadoop精华问答:Hadoop框架最核心的设计是?
- 基于 Layui 的富文本编辑器和封面的实现方案
- 利用FSMT进行文件服务器迁移及整合
- 利用aspnetPager控件加存储过程实现对gridview的分页
- 实现Runnable接口和继承Thread类之间的区别
- ffplay 源码 option 部分阅读ing
- 二维平面坐标系中,判断某点是否在正六边形内 | python 实现 + 数学推导(已知正六边形六个顶点坐标)
- Android 四大组件之一(Activity)
- HashMap简单小结
- LabVIEW FPGA PCIe开发讲解-实战篇:实验61:PCIe DMA+8位ADC(模拟数据采集卡)
- docker部署案例
- 英语影视台词---经典电影台词(世间万物有始皆有终。)
- 微信小程序中转义字符的处理
- 一.c#基础 [Unity_Learn_RPG_1]