Elasticsearch 是一个分布式的、面向生产规模工作负载优化的搜索引擎。
Kibana 可以将 Elasticsearch 中的数据转化为直观的图表、图形和仪表盘。
这篇文章,您将学习本地安装 Elasticsearch 和 Kibana,以及使用开发工具/ Java SDK 创建索引和搜索数据。
1 本地安装1.1 创建网络我们需要创建一个供 Elasticsearch 和 Kibana 使用的 network。这个 network 将被用于 Elasticsearch 和 Kibana 之间的通信。
brdocker network create elastic1.2 安装 ES拉取 Elasticsearch 镜像
brdocker pull docker.elastic.co/elasticsearch/elasticsearch:{version}这里的版本 version ,我们选取:8.9.0。
brdocker pull docker.elastic.co/elasticsearch/elasticsearch:8.9.0启动 docker elasticsearch 镜像
brdocker run --name elasticsearch --net elastic -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -t docker.elastic.co/elasticsearch/elasticsearch:8.9.0修改 elasticsearch 用户密码
brbin/elasticsearch-reset-password --username elastic -i1.3 安装 Kibana拉取 Kibana 镜像
brdocker pull docker.elastic.co/kibana/kibana:8.9.0启动 Kibana 镜像
brdocker run --name kibana --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.9.0因为启动安装 Kibana ,需要 token , 所以进入 elasticsearch 容器 ,执行:
bin/elasticsearch-create-enrollment-token -s kibana输入 token 之后,刷新页面,进入登录页面:
2 接口测试我们可以使用 Kibana Dev tools 来进行接口测试。
2.1 添加向索引添加单个文档,提交一个 HTTP POST 请求,目标是该索引。
POST /customer/_doc/1{ "firstname": "Jennifer", "lastname": "Walters"}该请求会自动创建名为customer的索引(如果不存在),然后添加一个 ID 为 1的新文档,同时存储并建立firstname和lastname字段的索引。
新文档可以立即从集群中的任何节点获取。您可以使用 GET 请求来检索它,请求中需指定其文档 ID :
GET /customer/_doc/1要一次性添加多个文档,请使用 _bulk API。批量数据必须是以换行分隔的 JSON(NDJSON)格式。每一行必须以换行字符(\n)结尾,包括最后一行。
PUT customer/_bulk{ "create": { } }{ "firstname": "Monica","lastname":"Rambeau"}{ "create": { } }{ "firstname": "Carol","lastname":"Danvers"}{ "create": { } }{ "firstname": "Wanda","lastname":"Maximoff"}{ "create": { } }{ "firstname": "Jennifer","lastname":"Takeda"}2.2 搜索已索引的文档可以在准实时的情况下进行搜索。下面的搜索将在customer索引中匹配所有名为 Jennifer 的顾客。
GET customer/_search{ "query" : { "match" : { "firstname": "Jennifer" } }}2.3 视图进入 Kibana Data Views :
然后创建数据视图 :
创建数据视图之后,可以在 Analytics > Discover 查看索引数据。
3 Java SDK 实战3.1 依赖<dependency> <groupId>co.elastic.clients</groupId> <artifactId>elasticsearch-java</artifactId> <version>8.9.0</version></dependency><dependency> <groupId>io.github.hakky54</groupId> <artifactId>sslcontext-kickstart</artifactId> <version>7.1.0</version></dependency><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.3</version></dependency><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.12.3</version></dependency><dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.12.3</version></dependency><dependency> <groupId>jakarta.json</groupId> <artifactId>jakarta.json-api</artifactId> <version>2.0.1</version></dependency><!-- 强制走高版本 ,防止和springboot 依赖冲突 --><dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>8.9.0</version></dependency>3.2 创建客户端RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "https"));final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("elastic", "ilxw@19841201"));// Elasticsearch 提供了 Https 服务,创建 client 建立 SSL 链接时没有做证书验证 ;SSLFactory sslFactory = SSLFactory.builder().withUnsafeTrustMaterial().withUnsafeHostnameVerifier().build();builder = builder.setHttpClientConfigCallback( httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider) setSSLContext(sslFactory.getSslContext()) .setSSLHostnameVerifier(sslFactory.getHostnameVerifier()) );RestClient restClient = builder.build();// Create the transport with a Jackson mapperElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());// And create the API clientElasticsearchClient esClient = new ElasticsearchClient(transport);3.3 创建文档创建索引名为 products ,新建一个文档 id 为 1 。
ProductPo product = new ProductPo(1, "Bag", 42);IndexRequest<Object> indexRequest = new IndexRequest.Builder<>().index("products").id(String.valueOf(product.getId())).document(product).build();IndexResponse response = esClient.index(indexRequest);System.out.println("Indexed with version " + response.version());3.4 查询文档brGetResponse<ProductPo> response = esClient.get(g -> gbr .index("products")br .id(String.valueOf(1)),br ProductPo.classbr);brbrif (response.found()) {br ProductPo product = response.source();br System.out.println("Product name " + product.getName());br} else {br System.out.println("Product not found");br}3.5 修改文档br Map<String, Object> doc = new HashMap<String, Object>();br // 文档产品名称调整为 my bikebr doc.put("name", "my bike");br doc.put("price", 100);brbr BulkOperation op = new BulkOperation.Builder().update(br i -> i.action(new UpdateAction.Builder<>().doc(doc).docAsUpsert(true).build()).id("1"))br .build();brbr List<BulkOperation> list = Collections.singletonList(op);br BulkResponse response = esClient.bulk(bulkBuilder -> bulkBuilder.index("products").operations(list));3.6 删除文档bresClient.delete(d -> d.index("products").id("1"));参考文档:
1、Elasticsearch 官方文档:
https://www.elastic.co/guide/en/elasticsearch/client/java-api-client/current/getting-started-java.html
2、Github文档
https://github.com/elastic/elasticsearch