Java API

最近更新时间:2020-05-14 16:33:28

Elasticsearch5.6.x

Transport Client

  • 创建transport client

    // on startup
    Settings settings = Settings.builder()
        .put("cluster.name", "myClusterName").build();
    //Add transport addresses and do something with the client...
    TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));
    // on shutdown
    client.close();
  • 添加参数,client.transport.sniff=true(默认是false,表示不开启集群嗅探功能)
Settings settings = Settings.builder()
        .put("client.transport.sniff", true).build();
TransportClient client = new PreBuiltTransportClient(settings);

备注:这仅是举例说明添加参数的方式,一般client.transport.sniff不需要修改,更多的细节请参考:ES5.6-transport-client

Elasticsearch6.8.x

强烈建议使用Rest Client,使用Transport Client客户端连接不同版本的elasticsearch实例时,会存在兼容性问题,官方在elasticsearch8.0中不再支持Transport Client

Transport Client

  • 创建transport client

    // on startup
    Settings settings = Settings.builder()
        .put("cluster.name", "myClusterName").build();
    TransportClient client = new PreBuiltTransportClient(settings)
        .addTransportAddress(new TransportAddress(InetAddress.getByName("host1"), 9300))
        .addTransportAddress(new TransportAddress(InetAddress.getByName("host2"), 9300));
    // on shutdown
    client.close();
  • 添加参数:client.transport.sniff=true(默认是false,表示不开启集群嗅探功能)
    Settings settings = Settings.builder()
        .put("client.transport.sniff", true).build();
    TransportClient client = new PreBuiltTransportClient(settings);

备注:这仅是举例说明添加参数的方式,一般client.transport.sniff不需要修改,更多的细节请参考,ES6.8-transport-client

Java High Level REST Client

依赖JDK版本1.8+,pom依赖需要添加

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>6.8.4</version>
</dependency>

举个index,get,delete,update的简单例子:

RestHighLevelClient client = new RestHighLevelClient(
        RestClient.builder(
                new HttpHost("{elasticsearch集群地址}", 9200, "http")));
//index
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("{filed1}", "{value1}");
jsonMap.put("{field2}", "{value2}");
IndexRequest indexRequest = new IndexRequest("{index}", "{type}", "{docid}")
        .source(jsonMap);
IndexResponse indexResponse = client.index(indexRequest , RequestOptions.DEFAULT);
//get
GetRequest getRequest = new GetRequest(
        "{index}",
        "{type}", 
        "{docid}");
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
//delete
DeleteRequest deleteRequest = new DeleteRequest(
        "{index}",   
        "{type}",     
        "{docid}"); 
DeleteResponse deleteResponse = client.delete(
        deleteRequest, RequestOptions.DEFAULT);
//update
UpdateRequest updateRequest = new UpdateRequest("{index}", "{type}", "{docid}")
        .doc(jsonMap);
UpdateResponse updateResponse = client.update(
        updateRequest, RequestOptions.DEFAULT);
client.close();

更多细节请参考官网,ES6.8-Java High Level REST Client

金山云,开启您的云计算之旅

免费注册