当前位置:   article > 正文

java es_基于java的ES开发

es的java开发

package com.rodge.elasticSearch.firstDemo;

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.InetSocketTransportAddress;

import org.elasticsearch.transport.client.PreBuiltTransportClient;

import org.junit.Test;

public class ESFirstDemo {

@SuppressWarnings("unchecked")

public TransportClient client() throws UnknownHostException {

// es地址, 地址为192.168.253.129(此处地址不能加"http://"), 此处的端口为es的tcp端口为9300,

// 而不是之前的http9200端口, 如果es有多可节点, 可以创建多个node, 然后再client中add进去

// 注意,这里的端口号不是9200,而是9300。9200端口是用来让HTTP REST

// API来访问ElasticSearch,而9300端口是传输层监听的默认端口。

InetSocketTransportAddress node = new InetSocketTransportAddress(

InetAddress.getByName("192.168.253.129"), 9300);

// es的配置类

Settings settings = Settings.builder().put("cluster.name", "wali").build();

// TransportClient是es启动的核心类, 后续的关于es的开发都是围绕着TransportClient进行的

TransportClient client = new PreBuiltTransportClient(settings);

client.addTransportAddress(node);

return client;

}

@Test

public void get() throws UnknownHostException {

GetResponse result = client().prepareGet("book", "novel", "1").get();

System.out.println(result.getSource().toString());

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/919821
推荐阅读
相关标签
  

闽ICP备14008679号