ElasticSearch集群 Java-API 使用

一、集群搭建

   先搭单机,然后集群。这里省略了单机中的一些配置。集群中同样也需要 ;

   Linux中 ElasticSearch 安装使用

1. 集群架构:一主二从

    mkdir cluster 

   复制3份 解压后的elasticsearch到cluster

2. 配置:

  主机配置: config/elasticsearch.yml 配置文件;

         ① cluster.name:  cluster   集群名称

         ② node.name:  cluster     节点名称

         ③ node.master: true  这是主机

         ④ network.host:  192.168.xxx.xxx 服务地址

         ⑤ http.port:  9200  服务端口号

   从机配置:config/elasticsearch.yml 配置文件;

          ① cluster.name: cluster   集群名称,注意要和主机的一样

          ② node.name:   slave_1  节点名称

          ③ discovery.zen.ping.unicast.hosts: [“192.168.xxx.xxx”]   这里填写主机的服务地址,用于找到主机

          ④ network.host: 192.168.xxx.xxx   服务地址

          ⑤ http.port:  9201  服务端口号

3. JVM 参数 ;如果使用虚拟机搭建集群,可能会存在内存不足。导致集群不能成功启动 ;

    启动报错:

Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x???, ???, 0) failed; error='Cannot allocate memory' (errno=12)
#
# There is insufficient memory for the Java Runtime Environment to continue.
# Native memory allocation (mmap) failed to map ??? bytes for committing reserved memory.

    修改config/jvm.options下的 :

 -Xms1g 为 -Xms512M 
 -Xmx1g 为 -Xms512M

二、启动 集群 ; 先主后从

    bin目录下 sh elasticsearch -d  后台启动; 

    从机同上;

三、查看集群状态

1. 简单模式

http://ES服务地址:端口号/_cat/nodes

Tips:这里的ES服务可以是主机,也可以从机

2. 完整模式:

http://ES服务地址:端口号/_cat/nodes?v

即在简单模式的URL后面加上了 “?v”

ip              heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
???.???.???.???           ??          ??   ?    ?.??    0.??     0.?? mdi       *      master
???.???.???.???           ??          ??   ?    ?.??    0.??     0.?? mdi       -      slaver-1
???.???.???.???           ??          ??   ?    ?.??    0.??     0.?? mdi       -      slaver-2

   可以看到ES服务的ip,内存、CPU的使用情况,以及集群名称,谁是主机【master列标为* 的即主机;- 表示从机】

3. JSON格式

http://ES服务地址:端口号/_cluster/stats

   响应内容:

{
  "_nodes": {
    "total": 总节点个数,
    "successful": 成功启动的节点个数,
    "failed": 0
  },
  "cluster_name": "集群名称",
  "timestamp": 集群启动时的毫秒数,
  "status": "green",
  "indices": {},
  "nodes": {}
}

   可以看到总节点个数,成功启动的节点个数,集群名称,集群启动时的毫秒数以及当前的服务状态(集群健康值)

    Tips:集群健康值分为:green(健康)、yellow(良好)和red(差)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用ElasticsearchJava API连接Elasticsearch集群,需要添加以下三个依赖:org.elasticsearch.client.elasticsearch-rest-high-level-client、org.elasticsearch.client:elasticsearch-rest-client、org.elasticsearch:elasticsearch。这些依赖可以通过Maven或Gradle添加到项目中。接下来,可以使用ElasticsearchJava API编写代码来连接Elasticsearch集群。具体步骤如下: 1. 创建RestHighLevelClient对象,该对象是与Elasticsearch集群进行通信的主要入口点。 2. 创建IndexRequest对象,该对象表示要索引的文档。 3. 使用IndexRequest对象设置索引名称、文档类型和文档ID等信息。 4. 使用IndexRequest对象设置要索引的文档内容。 5. 使用RestHighLevelClient对象执行IndexRequest对象,将文档索引到Elasticsearch集群中。 下面是一个示例代码,用于将文档索引到Elasticsearch集群中: ```java import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.rest.RestStatus; import java.io.IOException; public class ElasticsearchExample { private RestHighLevelClient client; public ElasticsearchExample(RestHighLevelClient client) { this.client = client; } public void indexDocument(String index, String type, String id, String json) throws IOException { IndexRequest request = new IndexRequest(index, type, id); request.source(json, XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); if (response.status() == RestStatus.CREATED) { System.out.println("Document indexed successfully"); } else { System.out.println("Failed to index document"); } } public static void main(String[] args) throws IOException { RestHighLevelClient client = new RestHighLevelClient(/* Elasticsearch连接配置 */); ElasticsearchExample example = new ElasticsearchExample(client); example.indexDocument("my_index", "my_type", "1", "{\"name\":\"John Doe\",\"age\":25}"); client.close(); } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值