Java Clients for Elasticsearch

 

设置elasticsearch的java heap大小:

The default installation of Elasticsearch is configured with a 1 GB heap.

There are two ways to change the heap size in Elasticsearch.

1、The easiest is to set an environment variable called ES_HEAP_SIZE.

When the server process starts, it will read this environment variable and set the heap accordingly. As an example, you can set it via the command line as follows:

export ES_HEAP_SIZE=10g

 2、you can pass in the heap size via a command-line argument when starting the process, if that is easier for your setup:

./elasticsearch  -d  -DES_JAVA_OPTS="-Xms1g -Xmx10g"

 -Xmx(maximun allowed memory for the process)

 -Xms(minimun  allowed memory for the process)

还有另一种方式,可以直接修改es的启动脚本,修改默认的JAVA_OPTS设置

[es@master001 es-node01]$ vi bin/elasticsearch.in.sh
if [ "x$ES_MIN_MEM" = "x" ]; then
ES_MIN_MEM=256m #-Xms
fi
if [ "x$ES_MAX_MEM" = "x" ]; then
ES_MAX_MEM=1g # -Xmx
fi
if [ "x$ES_HEAP_SIZE" != "x" ]; then
ES_MIN_MEM=$ES_HEAP_SIZE
ES_MAX_MEM=$ES_HEAP_SIZE
fi

 

利用java客户端连接远程elasticsearch服务器时, 

实例化client对象的方式如下:

Client client = null;
        try {

            client = TransportClient.builder().build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("master001"), 9300));
        } catch(UnknownHostException e){
            System.out.println("unkown host: " +e.getMessage());
        } finally {

            return client;
        }

 

抛出异常:NoNodeAvailableException[None of the configured nodes are available: []].

 

解决此异常需要设置cluster.name属性,方式如下:

Client client = null;
        try {
            client = TransportClient.builder().settings(Settings.builder().put("cluster.name", "cobub-es-testing")).build()
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("master001"), 9300));
        } catch(UnknownHostException e){
            System.out.println("unkown host: " +e.getMessage());
        } finally {

            return client;
        }

 修改后,连接正常。

 

参考:

Government Blog:  Java Clients for Elasticsearch

                               Heap: Sizing and Swapping

Spring Data Es in Github: spring-projects/spring-data-elasticsearch

 

 

猜你喜欢

转载自oitebody.iteye.com/blog/2256897