微信搜索superit|邀请体验:大数据, 数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

ES学习经历三,es集群部署和调用

未分类 aide_941 68℃

ES学习经历三,es集群部署

 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gcc_java/article/details/79238041

elasticsearch5.5多机集群配置

ELasticsearch 5.5要求JDK版本最低为1.8;

这里使用vmware配置了两台虚拟机,安装的centos系统 IP分别是 192.168.0.154 和 192.168.0.148 ;

使用xshell分别连接两台虚拟机。

192.168.0.154 的作为主节点。

配置集群之前  先把要加群集群的节点的里的data目录下的Node目录 删除,否则集群建立会失败。

分别修改两个elasticsearch.yml配置文件:

cluster.name: my-application                  //唯一的,前面是key,后面的value,value默认即可,有需求也可以修改

node.name: node-1                                    //每台机器一个,主节点这里取node-1,第二台从节点取node-2即可

network.host: 192.168.0.154                     //对外开放ip,写本机ip

http.port: 9200                                            //开放端口,测试使用,相同即可

discovery.zen.ping.unicast.hosts: [“192.168.0.154”]     //设置哪个是主操作机器
//修改后先后启动es,主节点es必须先启动
[elastic@bogon data]$ sh /home/es/elasticsearch-5.5.2/bin/elasticsearch -d
//分别启动测试是否成功的head插件接口
[root@bogon elasticsearch-head]# npm run start
通过head插件可以看到,主节点是星号,从节点也是亮起的,不然就是灰色,并有未集群的说明。
package com.gcc.testDemo;
 
import java.net.InetAddress;
 
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
 
public class TestCon {
 
	private static String host="192.168.0.154";
    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);
	
	public static void main(String[] args) throws Exception{
		TransportClient client = new PreBuiltTransportClient(settings.build())
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port));
		System.out.println(client);
		client.close();
	}
}
package com.gcc.testDemo;
 
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 com.google.gson.JsonObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
 
public class TestIndex {
 
	private static String host="192.168.0.154"; // 服务器地址
	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);
 
	private TransportClient client=null;
 
	@SuppressWarnings({ "resource", "unchecked" })
	@Before
	public void getCient()throws Exception{
		client = new PreBuiltTransportClient(settings.build())
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host),port));
	}
 
	@After
	public void close(){
		if(client!=null){
			client.close();
		}
	}
 
	/**
	 * 创建索引 添加文档
	 * @throws Exception
	 */
	@Test
	public void testIndex()throws Exception{
		JsonObject jsonObject=new JsonObject();
		jsonObject.addProperty("name", "java编程思想");
		jsonObject.addProperty("publishDate", "2012-11-11");
		jsonObject.addProperty("price", 100);
 
		IndexResponse response=client.prepareIndex("book", "java", "1")
				.setSource(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());
	}
 
	/**
	 * 根据id获取文档
	 * @throws Exception
	 */
	@Test
	public void testGet()throws Exception{
		GetResponse response=client.prepareGet("book", "java", "1").get();
		System.out.println(response.getSourceAsString());
	}
 
	/**
	 * 根据id修改文档
	 * @throws Exception
	 */
	@Test
	public void testUpdate()throws Exception{
		JsonObject jsonObject=new JsonObject();
		jsonObject.addProperty("name", "java编程思想2");
		jsonObject.addProperty("publishDate", "2012-11-12");
		jsonObject.addProperty("price", 102);
 
		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());
	}
 
	/**
	 * 根据id删除文档
	 * @throws Exception
	 */
	@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());
	}
}

转载请注明:SuperIT » ES学习经历三,es集群部署和调用

喜欢 (0)or分享 (0)