Elasticsearch写入数据之elasticsearch-java

在《Elasticsearch8.4.2在windows系统下的安装介绍》中介绍了如何安装ES,那么安装成功后改如何将数据写入到ES呢?写入ES数据的方式有很多,本次将介绍一种写入方式elasticsearch-java来写入数据到ES,elasticsearch-java是官方提供的java sdk写入方式,用户只需要配置相关参数就可以方便写入数据,源码地址:https://github.com/elastic/elasticsearch-java

前期需要引入相关的jar包到项目中,如下所示:

<dependency>
	<groupId>co.elastic.clients</groupId>
	<artifactId>elasticsearch-java</artifactId>
	<version>8.4.2</version>
	<exclusions>
		<exclusion>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore-nio</artifactId>
		</exclusion>
		<exclusion>
		    <groupId>org.apache.httpcomponents</groupId>
		    <artifactId>httpcore</artifactId>
	    </exclusion>
    </exclusions>
</dependency>
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpcore</artifactId>
    <version>4.4.15</version>
</dependency>
<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpcore-nio</artifactId>
	<version>4.4.15</version>
	<exclusions>
		<exclusion>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
		</exclusion>
	</exclusions>
</dependency>

编写如下代码

        // Create the low-level client
        String host = "localhost";
        int port = 9200;

        //tag::create-secure-client-fingerprint
        String fingerprint = "指纹信息";
        SSLContext sslContext = TransportUtils.sslContextFromCaFingerprint(fingerprint);
        BasicCredentialsProvider credsProv = new BasicCredentialsProvider(); // <2>
        credsProv.setCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(ES用户名, ES密码)
        );

        RestClient restClient = RestClient
                .builder(new HttpHost(host, port, "https")) // <3>
                .setHttpClientConfigCallback(hc -> hc
                        .setSSLContext(sslContext) // <4>
                        .setDefaultCredentialsProvider(credsProv)
                )
                .build();

        // Create the transport and the API client
        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        ElasticsearchClient client = new ElasticsearchClient(transport);
        try {
            String tradeMsg = "{\"a\":\"b\"}";
            IndexRequest<JsonData> request = IndexRequest.of(i -> i
                    .index("索引名称")
                    .document(JsonData.fromJson(tradeMsg))
            );
            client.index(request);
        } catch (IOException e) {
            e.printStackTrace();
        }

其中有几个地方必须先准备好,用户名&密码、指纹信息和索引名称。

用户名&密码

在安装ES时默认会生成ES的用户名和密码,将它copy出来填写到代码的对应的位置上,安装过程可以参考之前的介绍

指纹信息

在安装ES时默认会生成指纹信息,将它copy出来填写到代码的对应的位置上,安装过程可以参考之前的介绍,本次是默认安装ES,没有做过任何的其它变更,那么需要通过以HTTPS的方式去连接ES服务端,其它方式可能连接不上。代码中演示的方式是通过指纹信息获取SSL上下文信息,然后以HTTPS的方式访问ES服务端。

索引名称

相当于定义数据库表名,代码中需要定义一个索引的名称,告诉系统需要将数据写入到哪里,也可以通过Kibana的开发者工具预先创建好索引并定义好各个字段的类型。

具体elasticsearch-java的使用方式,官方也有文档介绍:Basic authentication | Elasticsearch Java API Client [8.4] | Elastic

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要对百万数据Elasticsearch 索引进行重命名,可以尝试以下性能优化方法: 1. 执行重命名操作时,尽量避免在同一节点上同时执行其他繁重的操作,以减少节点的负载和竞争。 2. 可以考虑将索引分成多个分片,然后在多个节点上执行并行的重命名操作。这可以通过设置索引的分片数来实现,例如: ``` PUT /my_index/_settings { "index": { "number_of_shards": 5 } } ``` 这将将索引分成 5 个分片,每个分片都可以在不同的节点上处理。 3. 使用 Elasticsearch Bulk API 执行批量操作。Bulk API 可以一次性处理多个操作,从而提高索引重命名的性能,例如: ``` POST /_bulk { "update": { "_id": "1", "_index": "my_index", "_type": "_doc" } } { "doc": { "name": "new_name" } } { "update": { "_id": "2", "_index": "my_index", "_type": "_doc" } } { "doc": { "name": "new_name" } } ... ``` 这将在一次 API 调用中更新多个文档的名称,而不是逐个更新。 4. 在执行重命名操作之前,可以考虑关闭索引的刷新机制。刷新操作会将新数据写入磁盘,从而增加索引重命名的时间和开销。可以使用以下命令关闭索引的刷新机制: ``` POST /my_index/_settings { "index": { "refresh_interval": "-1" } } ``` 这将关闭索引的刷新机制。在执行完索引重命名操作后,可以使用以下命令重新启用刷新机制: ``` POST /my_index/_settings { "index": { "refresh_interval": "1s" } } ``` 这将每秒钟执行一次索引刷新操作。请注意,关闭刷新机制可能会导致某些查询结果不准确,因为查询可能会返回尚未刷新的数据

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JoseKe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值