第6讲 6 ElasticSearch搭建集群

1,关机,克隆,关于克隆后的基本网络配置参考文档:https://blog.csdn.net/u010393325/article/details/83653507#comments

  1. 机器名IP结点作用结点名称集群名称
    Cruise192.168.245.40主节点node-1my-application
    Cruise2192.168.245.41从节点node-2my-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_ElasticSearch搭建集群
新建两个索引,一个设置成两个副本,一个设置成一个副本

6_ElasticSearch搭建集群
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());
    }
    
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值