1,关机,克隆,关于克隆后的基本网络配置参考文档:https://blog.csdn.net/u010393325/article/details/83653507#comments
-
机器名 IP 结点作用 结点名称 集群名称 Cruise 192.168.245.40 主节点 node-1 my-application Cruise2 192.168.245.41 从节点 node-2 my-application
2,修改配置文件, vi /home/es/elasticsearch-5.5.2/config/elasticsearch.yml
具体配置信息参考:https://blog.csdn.net/u010393325/article/details/84102144
3,删除/home/es/elasticsearch-5.5.2/data目录下的 nodes,
命令:rm -rf nodes;
4,启动 :先启动主节点(包括 ElasticSearch 和 ElasticSearch-head),
再启动从节点(ElasticSearch),
启动:ElasticSearch su elastic sh /home/es/elasticsearch-5.5.2/bin/elasticsearch 启动:ElasticSearch-head cd elasticsearch-head/ npm run start |
5, 测试集群,
新建两个索引,一个设置成两个副本,一个设置成一个副本
6,Java代码测试集群
package com.cruise; import java.net.InetAddress; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.transport.client.PreBuiltTransportClient; import com.google.gson.JsonObject; public class TestCON { private static String host="192.168.245.40"; private static int port=9300; public static final String CLUSTER_NAME="my-application"; private static Settings.Builder settings=Settings.builder().put("cluster.name",CLUSTER_NAME); @SuppressWarnings("resource") public static void main(String[] args) throws Exception{ TransportClient client = new PreBuiltTransportClient(settings.build()) .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port)); JsonObject jsonObject = new JsonObject(); IndexResponse indexResponse = client.prepareIndex("book","java","1") .setSource(jsonObject.toString(), XContentType.JSON).get(); System.out.println(client); client.close(); } } |
增删改查测试:
package com.cruise;
import java.net.InetAddress;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.google.gson.JsonObject;
public class TestIndex {
private static String host="192.168.245.40";
private static int port=9300;
private TransportClient client =null;
public static final String CLUSTER_NAME="my-application";
private static Settings.Builder settings=Settings.builder().put("cluster.name",CLUSTER_NAME);
@SuppressWarnings({ "resource", "unchecked" })
@Before
public void getClient() throws Exception{
client = new PreBuiltTransportClient(settings.build())
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port));
}
@After
public void close(){
if(client!=null){
client.close();
}
}
@Test
public void testIndex() throws Exception{
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "java编程思想");
jsonObject.addProperty("publishDate", "2011-11-11");
jsonObject.addProperty("pirce", "100");
IndexResponse indexResponse = client.prepareIndex("book","java","1")
.setSource(jsonObject.toString(), XContentType.JSON).get();
System.out.println("索引名称:"+indexResponse.getIndex());
System.out.println("类型:"+indexResponse.getType());
System.out.println("id:"+indexResponse.getId());
System.out.println("当前索引状态:"+indexResponse.status());
}
@Test
public void testGet() throws Exception{
GetResponse getResponse = client.prepareGet("book","java","1").get();
System.out.println(getResponse.getSourceAsString());
}
@Test
public void testUpdate() throws Exception{
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "java编程思想22");
jsonObject.addProperty("publishDate", "2011-11-22");
jsonObject.addProperty("pirce", "122");
UpdateResponse response = client.prepareUpdate("book","java","1").setDoc(jsonObject.toString(), XContentType.JSON).get();
System.out.println("索引名称:"+response.getIndex());
System.out.println("类型:"+response.getType());
System.out.println("id:"+response.getId());
System.out.println("当前索引状态:"+response.status());
}
@Test
public void testDelete() throws Exception{
DeleteResponse response = client.prepareDelete("book","java","1").get();
System.out.println("索引名称:"+response.getIndex());
System.out.println("类型:"+response.getType());
System.out.println("id:"+response.getId());
System.out.println("当前索引状态:"+response.status());
}
}