分享

SolrCloud入门:相关概念及数据迁移


问题导读

1.为何产生SolrCloud?
2.SolrCloud哪些概念,含义都是什么?
3.SolrCloud有哪两种路由算法?




版本:
SolrCloud5.0

SolrCloud的设计是为了提供高可用、容错,在分布式环境中进行内容索引和查询请求。

SolrCloud 5.0,对自带的SolrCloud的启动脚本进行了改进,启动SolrCloud变的异常简单,执行

[mw_shl_code=bash,true]$ bin/solr –e cloud[/mw_shl_code]

根据提示输入一些参数,即可启动完成SolrCloud,部署启动完成,如下图

1.png

SolrCloud相关概念
SolrCloud中有四个关键名词:core、collection、shard、node。

core:在Solr单机环境中,core本质上就是单个index。若需有多个index,那必须创建多个core。在SolrCloud环境中,单个index可以横跨多个Solr实例,这意味着单个index是由不同机器上的多个cores组成。

collection:由core组成的逻辑index叫做collection,一个collection是跨越多个cores的index,这使index可扩展并冗余备份。

shard:在SolrCloud中可以有多个collections。Collections可被分片,每个分片可有多个副本(Replica),同一副本下的相同分片称为shards。每个shards下的有一个分片为leader,该leader通过选举策略产生。

node:SolrCloud中,node是运行Solr的Java虚拟机实例,也就是Server(例如Tomcat、Jetty)。
理解core和collection的区别非常重要。在传统的单node solr中,core和collection的概念等同,都代表一个逻辑index。在SolrCloud中,多个nodes下的cores形成一个collection。


SolrCloud路由

SolrCloud中,提供了两种路由算法:
compositeIdimplicit 在创建Collection时,需要通过router.name指定路由策略,默认为compositeId路由。
compositeId
该路由为一致性哈希路由,shards的哈希范围从80000000~7fffffff。初始创建collection是必须指定numShards,compositeId路由算法根据numShards的个数,计算出每个shard的哈希范围,因此路由策略不可以扩展shard。

implicit
该路由方式指定索引具体落在路由到哪个Shard,这与compositeId路由方式索引可均匀分布在每个shard上不同。同时只有在implicit路由策略下才可创建shard。
利用solrJ新建索引时,需要在代码中指定索引具体落在哪个shard上,添加代码:

[mw_shl_code=bash,true]doc.addField("_route_", "shard_X");[/mw_shl_code]

同时在schema.xml添加字段

[mw_shl_code=bash,true]<field name="_route_" type="string"/>
[/mw_shl_code]

利用URL创建implicit路由方式collection:

http://10.21.17.200:9580/solr-5. ... t&shards=shard1,shard2,shard3



SolrRouter源码
在Solr源码中,可以看到,Solr路由的基类为DocRouter抽象类,HashBasedRouter和ImplicitDouter继承自DocRouter,同时CompositeIdRouter又继承HashBasedRouter抽象类,通过一个工具Hash类实现Document的路由策略。

2.png

创建Collection
Solr创建Collection的两种方式:

通过前台界面Add Core创建collection
3.png

5.png

由于在tomcat,setenv.sh,设置-DnumShards=7,所以该collection有7个shards。
需要注意的是:使用compositeId路由创建collection,指定numShards后,不可扩展Shard,即使勉强增加Shard,新建索引也不会落在该Shard上。查看clusterstate.json,可看到新建shard的"range":null

URL创建collection
通过URL创建collection需要满足条件:num of (shards + replications)< num of live nodes
测试环境中3台solr机器,创建collection URL为:
http://10.21.17.200:9580/solr-4.10.0/admin/collections?action=CREATE&name=collection1&router.name=compositeId&numShards=5&replicationFactor=1
执行结果报错

<str name="Operation createcollection caused exception:">
org.apache.solr.common.SolrException:org.apache.solr.common.SolrException:Cannot create collection collection1. Value of maxShardsPerNode is 1, and thenumber of live nodes is 3. This allows a maximum of 3 to be created. Value ofnumShards is 5 and value of replicationFactor is 1. This requires 5 shards tobe created (higher than the allowed number)
</str>
报错原因不满足 5 + 1 < 3

数据迁移
在某些场景中,需要对SolrCloud进行扩容或数据迁移。
根据以上讨论的两种路由算法,implicit实现该需求比较简单,只要创建Shard即可,新建索引时,将索引建到新建Shard上,查询操作,指定collection名称,得到的仍是整个集群返回的结果。
compositeId路由实现上述需求稍微麻烦一下,通过分裂(SPLITSHARD)操作实现。如下图,对Shard1进行分裂,分裂URL为:
http://10.21.17.200:9580/solr-4.10.0-web/admin/collections?action=SPLITSHARD&collection=log4j201503&shard=shard1
6.png
此时Shard1的数据会平均分布到shard1_0和shard1_1上,在利用DELETESHARD API删除Shard1,即可保证数据不冗余。




4.png

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条