ElasticSearch 6.x 学习笔记:24.Java API连接Elasticsearch

24.1 实例演示

package cn.hadron;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

/**
 * 官方参考文档
 * https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html
 */
public class ClientDemo {
    public static void main(String[] args) throws UnknownHostException {
        // 设置集群名称
        Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
        // 创建client
        TransportClient client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("node1"), 9300));
        // 搜索数据
        GetResponse response = client.prepareGet("website", "blog", "1").execute().actionGet();
        // 输出结果
        System.out.println(response.getSourceAsString());
        // 关闭client
        client.close();
    }
}

这里写图片描述

24.2 TransportClient解读

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/transport-client.html

参数说明
client.transport.ignore_cluster_nameSet to true to ignore cluster name validation of connected nodes. (since 0.19.4)
client.transport.ping_timeoutThe time to wait for a ping response from a node. Defaults to 5s.
client.transport.nodes_sampler_intervalHow often to sample / ping the nodes listed and connected. Defaults to 5s.

24.3 TransportClient警告

We plan on deprecating the TransportClient in Elasticsearch 7.0 and removing it completely in 8.0. Instead, you should be using the Java High Level REST Client, which executes HTTP requests rather than serialized Java requests. The migration guide describes all the steps needed to migrate.
我们计划在Elasticsearch 7.0中弃用TransportClient并在8.0中完全删除它。 相反,您应该使用Java高级别REST客户端,它执行HTTP请求而不是序列化的Java请求。 迁移指南介绍了迁移所需的所有步骤。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值