ElasticSearch入门- 设置分片副本数量及putMapping

2019-03-09 00:39|来源: 网络

在之前的一篇文章中,写到如何创建mapping。里面只是简单的创建了一个mapping。其实,这种比较重要并且一旦建立无法修改的操作还是需要仔细规划的。

今天我介绍设置index的分片数量及副本数量,即创建索引的如何指定分片的个数及副本的个数。分片的个数在创建之后是无法再增加和减少的,除非你另外建一个索引库,而副本是可以在运行的时候,动态增加和减少。因此,在创建索引库时,规划好分片(Shard)是非常重要的。

 

1、如何在创建index时,指定分片的个数?

其实代码也很简单。

Settings settings = ImmutableSettings.settingsBuilder()
				//5个主分片
                .put("number_of_shards", 5)
                //测试环境,减少副本提高速度
                .put("number_of_replicas", 0).build();

 在非生产环境,这个副本最好设置成0,即不保留副本,因为每增加一个副本,写数据的成本就增加一倍。本来elasticsearch写的速度就比较慢。

 

2、如果在创建Index时,顺便把type的mapping也创建?

//首先创建索引库
		CreateIndexResponse  indexresponse = client.admin().indices()
		//这个索引库的名称还必须不包含大写字母
		.prepareCreate("testindex").setSettings(settings)
		//这里直接添加type的mapping
		.addMapping("testType", getMapping())

 有几个type就addMapping几个。

综合起来的代码:

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;

import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;

import com.donlianli.es.ESUtils;
/**
 * 创建索引时,指定分片及副本
 * 并且创建type一起执行
 * @author donlianli@126.com
 */
public class PutMappingTest {
	public static void  main(String[] argv) throws Exception{
		Client client = ESUtils.getClient();
		Settings settings = ImmutableSettings.settingsBuilder()
				//5个主分片
                .put("number_of_shards", 5)
                //测试环境,减少副本提高速度
                .put("number_of_replicas", 0).build();
		//首先创建索引库
		CreateIndexResponse  indexresponse = client.admin().indices()
		//这个索引库的名称还必须不包含大写字母
		.prepareCreate("testindex").setSettings(settings)
		//这里直接添加type的mapping
		.addMapping("testType", getMapping())
		.execute().actionGet();
		System.out.println(indexresponse.acknowledged());
	}
	/**
	 * mapping 一旦定义,之后就不能修改。
	 * @return
	 * @throws Exception
	 */
	private static XContentBuilder getMapping() throws Exception{
		XContentBuilder mapping = jsonBuilder()  
			       .startObject()  
			         .startObject("test")  
			         .startObject("properties")         
			           .startObject("id")
			           		.field("type", "long")
			           		.field("store", "yes")
			           	.endObject()    
			           	
			           .startObject("type")
			           		.field("type", "string")
			           		.field("index", "not_analyzed")
			           	.endObject()  
			           	
			           .startObject("catIds")
			           		.field("type", "integer")
			           .endObject()  
			         .endObject()  
			        .endObject()  
			      .endObject();  
		return mapping;
	}
}

 3、如果修改副本的数量?

import org.elasticsearch.action.admin.indices.settings.UpdateSettingsResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import com.donlianli.es.ESUtils;
/**
 * 更新副本个数
 * @author donlianli@126.com
 */
public class UpdateReplicaTest {
	public static void  main(String[] argv) throws Exception{
		Client client = ESUtils.getClient();
		Settings settings =  ImmutableSettings.settingsBuilder()
                //可以更新的配置还有很多,见elasticsearch官网
                .put("number_of_replicas", 2).build();
		//首先创建索引库
		UpdateSettingsResponse  updateSettingsResponse = client.admin().indices()
		.prepareUpdateSettings("testindex").setSettings(settings).execute().actionGet();
		System.out.println(updateSettingsResponse);
	}

}

 

 

 

 

对这类话题感兴趣?欢迎发送邮件至 donlianli@126.com
关于我:邯郸人,擅长Java,Javascript,Extjs,oracle sql。
更多我之前的文章,可以访问  我的空间

转自:http://donlianli.iteye.com/blog/1922294

相关问答

更多
  • 是的,每份20亿份文档的数量是有限制的,这是一个严重的lucene限制。 您可以在单个Lucene索引中拥有最大数量的文档。 从https://issues.apache.org/jira/browse/LUCENE-5843 [ LUCENE-5843 ]开始,限制为2,147,483,519 (= Integer.MAX_VALUE - 128)个文件。 您应该考虑水平缩放。 Yes there is limit to the number of docs per shard of 2 billion, ...
  • 我认为您需要对分区过滤进行分片,以指定允许哪些节点承载特定索引的分片。 https://www.elastic.co/guide/en/elasticsearch/reference/current/shard-allocation-filtering.html I think you need to shard allocation filtering to specify which nodes are allowed to host the shards of a particular index. ...
  • 不需要每次集合更新时都不需要运行它。 根据config中的刷新间隔刷新索引,或者通过调用“_refresh”手动刷新索引 是的,支持分页,使用字段“从”,“大小”,“排序”排序查询请参阅 ElasticSearch分页和排序 是的,你可以在任何领域搜索,请参阅http://www.elasticsearch.org/guide/reference/query-dsl/query-string-query/ no you need not run that every time a collection ge ...
  • 是的,节点可以有一个或多个索引的多个分片。 您可以通过执行GET _cat/shards?v命令GET _cat/shards?v验证它。 在这里阅读更多关于命令的信息 拥有单个节点Elasticsearch集群的问题是索引的副本分片将不会被分配(但主分片将被分配),因为在同一台机器上同时拥有同一分片的主副本并没有意义。 Yes a node can have multiple shards of one or more indices. You can verify it for yourself by ...
  • 如何找到保存到保存文档的节点? 更正确的问题是文档保存到哪个分片,因为分片可以在群集中移动。 您可以使用_search_shards API并提供文档的ID: GET /index/type/_search_shards?routing=4 将一个Person文档保存到具有两个副本的节点后,如何查询此Person并获取哪个副本/节点得到的答案来自哪个? 我认为你不能轻易做到。 您可以降低slowlogs的阈值,并检查slowlog文件以查找搜索请求的特定fetch阶段,以查看某个节点是否记录该状态。 如果 ...
  • 正如瓦尔所说,你通常会有基于时间的指数,所以你可以很容易地(以一种高效的方式)在一定的保留期限后去除数据。 因此,随着您的需求随着时间的推移而改变,您可以更改碎片编号(通常通过索引模板)。 当前版本的Elasticsearch现在支持一个_split API ,它完全符合你的要求:最初使用5个碎片,但可以选择任何因子20(就像一个例子) - 所以5 - > 10 - > 30将是选择。 如果您有5个主分片并且复制因子为1,则仍可以将负载分散到10个节点上:写入5个主分片和5个副本分片; 阅读将转到其中任何一 ...
  • 这是提到这个的文档 。 实际上,它并不明显,但它存在: index.recovery.initial_shards 使用本地网关时,只有在群集中可以分配仲裁分片时才会恢复特定分片。 它可以设置为: 仲裁(默认) 法定人数-1(或一半) 充分 全1。 还支持数值,例如1。 如果您确实希望节点运行,请在index.recovery.initial_shards: 1文件中设置index.recovery.initial_shards: 1 。 Here's the documentation mentionin ...
  • 这是预料之中的。 为什么要这么做? Primaries和复制品正在做同样的工作。 你认为这会解决什么问题? This is expected. Why would you do that? Primaries and replicas are doing the same job. What problems do you think this would solve?
  • 被动复制:“任何新索引的文档将首先存储在主分片上,然后并行复制到关联的副本分片。” https://www.elastic.co/guide/en/elasticsearch/guide/current/_add_failover.html和https://www.elastic.co/guide/en/elasticsearch/guide/current/replica-shards.html应该让事情变得更加清晰。 Passive replication: "Any newly indexed doc ...
  • 如果您对碎片的内容感兴趣,可以使用cat api: GET /_cat/shards If you curios about what are does shards you can use the cat api: GET /_cat/shards