第6講 6 ElasticSearch搭建叢集
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/ |
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());
}
}