一.Collections API java
參考:https://cwiki.apache.org/confluence/display/solr/Collections+API node
由於API比較多,我就不一一列舉,只列出比較重要的幾個
1.建立collection
官方示例:/admin/collections?action=CREATE&name=name&numShards=number&replicationFactor=number&maxShardsPerNode=number&createNodeSet=nodelist&collection.configName=configname
apache
(1) 個人示例:api
http://192.168.66.99:8080/solr/admin/collections?action=CREATE&name=test&numShards=2&replicationFactor=2&maxShardsPerNode=3安全
name指明collection名稱服務器
numShards指明分片數async
replicationFactor指明副本數ide
maxShardsPerNode 每一個節點最大分片數(默認爲1)學習
(2)當咱們想指定配置文件,索引目錄時,能夠加入以下參數測試
property.name=value |
string |
No |
Set core property name to value. See core.properties file contents. |
可選參數以下:
key
Description
name |
The name of the SolrCore. You'll use this name to reference the SolrCore when running commands with the CoreAdminHandler. |
config |
The configuration file name for a given core. The default is solrconfig.xml. |
schema |
The schema file name for a given core. The default is schema.xml |
dataDir |
Core's data directory as a path relative to the instanceDir, data by default. |
configSet |
If set, the name of the configset to use to configure the core (see Config Sets). |
properties |
The name of the properties file for this core. The value can be an absolute pathname or a path relative to the value of instanceDir. |
transient |
If true, the core can be unloaded if Solr reaches the transientCacheSize. The default if not specified is false. Cores are unloaded in order of least recently used first. |
loadOnStartup |
If true, the default if it is not specified, the core will loaded when Solr starts. |
coreNodeName |
Added in Solr 4.2, this attributes allows naming a core. The name can then be used later if you need to replace a machine with a new one. By assigning the new machine the same coreNodeName as the old core, it will take over for the old SolrCore. |
ulogDir |
The absolute or relative directory for the update log for this core (SolrCloud) |
shard |
The shard to assign this core to (SolrCloud) |
collection |
The name of the collection this core is part of (SolrCloud) |
roles |
Future param for SolrCloud or a way for users to mark nodes for their own use. |
(3) 運行http://192.168.66.99:8080/solr/admin/collections?action=CREATE& name=test&numShards=2&replicationFactor=2&maxShardsPerNode=3&property.schema=schema2.xml&property.dataDir=/usr/local/data/solr
以上命令將會建立collection test,指定schema2.xml做爲其schema配置文件,並指定/usr/local/data/solr爲其數據存放目錄
(注意若是指定相關配置文件,首先要向zookeeper中上傳相關的配置,運行一下命令將schema2.xml上傳到zookeeper
java -classpath .:/usr/local/solr/solrhome-1/lib/* org.apache.solr.cloud.ZkCLI -cmd upconfig -zkhost 127.0.0.1:1181,127.0.0.1:2181,127.0.0.1:3181 -confdir /usr/local/solr/solrhome-1/update/ -confname solr-conf
)
在我本機運行時出現錯:
org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException:Error CREATEing SolrCore 'test_shard1_replica1': Unable to create core: test_shard1_replica1 Caused by: Lock obtain timed out: NativeFSLock@/usr/local/data/solr/index/write.lock
這是由於3個節點都在我本機,咱們將索引目錄指定爲同一個,這種建立方式默認的數據文件夾會重複,咱們能夠分別指定分片文件夾
2.刪除collection
官方示例:/admin/collections?action=DELETE&name=collection
個人示例:http://192.168.66.99:8080/solr/admin/collections?action=DELETE&name=test
3.建立分片
官方示例:/admin/collections?action=CREATESHARD&shard=shardName&collection=name
/admin/collections?action=SPLITSHARD: split a shard into two new shards
個人示例:http://192.168.66.99:8080 /solr/admin/collections?action=CREATESHARD&collection=test&shard=shard1&name=test_shard1_replica1&property.schema=schema2.xml&property.dataDir=/usr/local/data/solr/test_shard1_replica1
本人測試,若是collection是使用第1節方式建立的,使用這種方式進行建立分片時,沒法正確執行,緣由待研究
4.其餘
/admin/collections?action=RELOAD: reload a collection
/admin/collections?action=SPLITSHARD: split a shard into two new shards
/admin/collections?action=CREATESHARD: create a new shard
/admin/collections?action=DELETESHARD: delete an inactive shard
/admin/collections?action=CREATEALIAS: create or modify an alias for a collection
/admin/collections?action=DELETEALIAS: delete an alias for a collection
/admin/collections?action=DELETEREPLICA: delete a replica of a shard
/admin/collections?action=ADDREPLICA: add a replica of a shard
/admin/collections?action=CLUSTERPROP: Add/edit/delete a cluster-wide property
/admin/collections?action=MIGRATE: Migrate documents to another collection
/admin/collections?action=ADDROLE: Add a specific role to a node in the cluster
/admin/collections?action=REMOVEROLE: Remove an assigned role
/admin/collections?action=OVERSEERSTATUS: Get status and statistics of the overseer
/admin/collections?action=CLUSTERSTATUS: Get cluster status
/admin/collections?action=REQUESTSTATUS: Get the status of a previous asynchronous request
/admin/collections?action=LIST: List all collections
二.Cores API
solr的core在我看來是對shard進行各類操做的,一個core可視爲一個shard或者其replica的管理,可是也能夠建立collection,
參考:https://cwiki.apache.org/confluence/display/solr/CoreAdminHandler+Parameters+and+Usage
訪問方式: http://localhost:8983/solr/admin/cores?action=action,操做有如下幾種
1.查看狀態
官方示例:http://localhost:8983/solr/admin/cores?action=STATUS&core=core0
2.建立core
官方示例:http://localhost:8983/solr/admin/cores?action=CREATE&name=coreX&instanceDir=path/to/dir&config=config_file_name.xml&schema=schem_file_name.xml&dataDir=data
可選參數基本與建立collection相同
Parameter
Description
name |
The name of the new core. Same as "name" on the <core> element. |
instanceDir |
The directory where files for this SolrCore should be stored. Same as instanceDir on the <core> element. |
config |
(Optional) Name of the config file (solrconfig.xml) relative to instanceDir. |
schema |
(Optional) Name of the schema file (schema.xml) relative to instanceDir. |
datadir |
(Optional) Name of the data directory relative to instanceDir. |
configSet |
(Optional) Name of the configset to use for this core (see Config Sets) |
collection |
(Optional) The name of the collection to which this core belongs. The default is the name of the core. collection.<param>=<value> causes a property of <param>=<value> to be set if a new collection is being created. Use collection.configName=<configname> to point to the configuration for a new collection. |
shard |
(Optional) The shard id this core represents. Normally you want to be auto-assigned a shard id. |
property.name=value |
(Optional) Sets the core property name to value. See core.properties file contents. |
async |
(Optional) Request ID to track this action which will be processed asynchronously |
個人示例:
http://192.168.66.99:8080/solr/admin/cores?action=CREATE&name=test&collection=test&shard=shard1&instanceDir=/usr/local/data/solr/solr-1/test/&schema=schema2.xml
name指明core名稱 該名稱爲solrhome下的文件夾名稱,該文件夾下存放該分片的數據文件
collection指明collection名稱 若collection 不存在則建立 若存在則判斷shard
shard指明分片名稱 若shard不存在,則建立 若存在則建立一個該分片的副本
該命令會在 http://192.168.66.99:8080上建立一個名爲test的collection,而且建立一個名爲shard1的分片,而且該機器爲這個分片的leader
http://192.168.66.99:8080/solr/admin/cores?action=CREATE&name=test_shard1_replica_2&collection=test&shard=shard1
該命令會在 http://192.168.66.99:8080上爲test建立shard1的副本
3.刷新core
官方示例:http://localhost:8983/solr/admin/cores?action=RELOAD&core=core0
4.重命名core
官方示例:http://localhost:8983/solr/admin/cores?action=RENAME&core=core0&other=core5
5.交換core
官方示例:http://localhost:8983/solr/admin/cores?action=SWAP&core=core1&other=core0
6.下線core
官方示例:http://localhost:8983/solr/admin/cores?action=UNLOAD&core=core0
可選參數:
deleteIndex: if true, will remove the index when unloading the core.
deleteDataDir: if true, removes the data directory and all sub-directories.
deleteInstanceDir: if true, removes everything related to the core, including the index directory, configuration files, and other related files.
async: if set to a value, makes the call asynchronous. This call can then be tracked using the REQUESTSTATUS API.
7.合併索引
官方示例:
方 式1:http://localhost:8983/solr/admin/cores?action=MERGEINDEXES& core=core0&indexDir=/opt/solr/core1/data/index&indexDir=/opt/solr/core2/data/index
方式2:http://localhost:8983/solr/admin/cores?action=mergeindexes&core=core0&srcCore=core1&srcCore=core2
8.切分
官方示例:http://localhost:8983/solr/admin/cores?action=SPLIT&core=core0&targetCore=core1&targetCore=core2
可選參數:
Parameter
Description
Multi-valued
core |
The name of the core to be split. |
false |
path |
The directory path in which a piece of the index will be written. |
true |
targetCore |
The target Solr core to which a piece of the index will be merged |
true |
ranges |
A comma-separated list of hash ranges in hexadecimal format |
false |
split.key |
The key to be used for splitting the index |
false |
async |
(Optional) Request ID to track this action which will be processed asynchronously |
false |
9.查看請求狀態
官方示例:http://localhost:8983/solr/admin/cores?action=REQUESTSTATUS&requestid=1
三.collection實踐拓展
上述API提供給了咱們一組操做collection和core的方法,如今來想想實際場景中可能遇到的問題
1.場景1新增collection
搭建完solrcloud後咱們首先要考慮的就是創建collection,並對其進行分片,咱們有兩種方式來作這件事
(1)讓solrcloud自動幫咱們分片,指定分片名稱等,即運行命令:
http://192.168.66.99:8080/solr/admin/collections?action=CREATE&name=test&numShards=2&replicationFactor=2&maxShardsPerNode=3
(2)本身指定每一個分片的機器,即分別運行命令:
http://192.168.66.99:7080/solr/admin/cores?action=CREATE&name=test_shard1_replica_1&collection=test&shard=shard1
...
這兩種方式都可以指定配置文件,及存儲路徑
2.場景2-擴容
隨着數據量和訪問量的增大,咱們須要對solrcloud進行擴容,以維持其運行,這又可能包含兩種場景
(1)增長一個collection shard
方式一:使用action=SPLITSHARD將一個分片切分紅兩塊,而後再進行重命名等其餘操做
方式二:使用cores?action=CREATE&name=test&collection=test&shard=shard1直接建立
(2)增長一個shard的副本
一樣使用cores?action=CREATE&name=test&collection=test&shard=shard1直接建立
3.場景3-更換服務器
我的建議以下,先將新服務器加入solrcloud,同步索引文件,而後再下線老服務器,安全快捷直接經過管理界面便可實現
經過以上場景能夠發現,使用core api在實際狀況下可能更加快捷,所以能夠重點學習
4.另外,有時咱們在配置solrcloud過程當中可能會出現各類配置錯誤,這種錯誤會在solrcloud的管理界面進行提示,好比配置collection時指定schema.xml而在zookeeper中並不存在指定的文件 這時solrcloud就會提示: test3_shard2_replica1: org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Could not load core configuration for core test3_shard2_replica1 如何處理這種錯誤呢: (1)刪除solrhome下的相關文件夾 (2)挨個重啓solrcloud節點