1. 程式人生 > >Solr13 - 通過SolrCloud的RESTful API對叢集進行增刪改查操作

Solr13 - 通過SolrCloud的RESTful API對叢集進行增刪改查操作

文章目錄

說明: 本篇所有curl操作是在終端中進行的, 可以省去curl和url中的引號, 在瀏覽器中訪問, 效果更明顯.

1 建立操作

1.1 建立collection

直接在瀏覽器的URL位址列進行操作, 命令如下:

http://localhost:8080/solr/admin/collections?action=CREATE&
name=mycollection&numShards=3&replicationFactor=4

或者: 直接在終端的命令列中操作(注意curl之後的內容需要加單引號或雙引號):

curl 'http://172.16.11.96:8080/solr/admin/collections?action=CREATE&name=mycollection&numShards=3&replicationFactor=4'

上述方式建立的Collection中, Shard和Replica由Solr自動分配, 不能手動選擇具體的資料存放路徑、例項存放路徑.

1.2 建立core

手動指定例項存放路徑和資料存放路徑:

curl 'http://localhost:8080/solr/admin/cores?action=CREATE&name=my_collection-shard1-replica1&instanceDir=/usr/solr/my_collection-shard1-replica1&dataDir=/data_solr/my_collection-shard1-replica1&collection=my_collection&shard=shard1'
curl 'http://localhost:8080/solr/admin/cores?action=CREATE&name=my_collection-shard1-replica2&instanceDir=/usr/solr/my_collection-shard1-replica2&dataDir=/data_solr/my_collection-shard1-replica2&collection=my_collection&shard=shard1'

這樣可以創建出一個collection, 並自己指定該collection的shard和replica的所有配置項.

1.3 建立API引數說明

  1. action: 要操作動作的名稱;

  2. name: 要建立的集合名稱;

  3. numShards: 集合分片的個數;

  4. replicationFactor(副本因子): 每個分片配備的副本數, 包括Leader和Replica;

  5. collection.configName: 建立新集合時所使用的配置檔案的名稱, 若不指定, 將預設使用name作為配置檔案的名稱.

  6. createNodeSet: 如果不提供該引數, 建立操作會將Replica分佈到所有活躍的Solr節點上. 該引數用於建立分片和副本的節點集合, 格式如下:
    createNodeSet=node1:8081_solr,node2:8082_solr,node3:8083_solr

  7. maxShardsPerNode: 指定每個Node可以建立的Shard數, 預設為1.
    a) 建立操作將生成numShards * replicationFactor個副本, 並儘可能均勻地分佈在所有活躍的Node上;
    b) 為保證高可用, 同一個Solr節點上不能存在同一Shard的多個副本;
    c) 如果maxShardsPerNode * nodeNum < numShards * replicationFactor, CREATE操作將失敗, 報錯資訊如下:

org.apache.solr.common.SolrException:org.apache.solr.common.SolrException: Cannot create collection mycollection. Value of maxShardsPerNode is 3, and the number of live nodes is 3. This allows a maximum of 9 to be created. Value of numShards is 3 and value of replicationFactor is 4. This requires 12 shards to be created (higher than the allowed number)

2 刪除操作

2.1 刪除collection

collections的API不支援UNLOAD操作; 將直接刪除某一collection, 包括目錄檔案.

curl 'http://localhost:8080/solr/admin/collections?action=DELETE&name=collection1&indent=true'

name: 將被刪除的集合的名稱;

indent=true: 格式化(有縮排)顯示響應結果.

2.2 解除安裝core

cores的API不支援DELETE操作; 將解除安裝某一指定collection_shard_replica, 並不會刪除目錄檔案.

curl 'http://localhost:8080/solr/admin/cores?action=UNLOAD&core=collection1_shard1_replica2&deleteIndex=true&indent=true'

2.3 刪除shard

curl 'http://localhost:8080/solr/admin/collections?action=DELETESHARD&shard=shard1&collection=collection1'

3 載入操作

3.1 重新載入collection

collections的API不支援LOAD操作; 被解除安裝了的core並不會被RELOAD進來.

curl 'http://localhost:8080/solr/admin/collections?action=RELOAD&name=collection1&indent=true'

name: 將被重新載入的集合的名稱.

3.2 載入core

cores的API不支援RELOAD操作;

可以LOAD某一個collection, 但被解除安裝了的core未能被LOAD進來;

可以LOAD某一個指定的core, 但被解除安裝了的core也未能被LOAD進來.

curl 'http://localhost:8080/solr/admin/cores?action=LOAD&core=collection1_shard1_replica2&indent=true'

4 檢視操作

4.1 檢視叢集的Cloud data

此操作亦可通過瀏覽器的URL檢視, 格式化響應結果, 更加清晰:

curl 'http://localhost:8080/solr/zookeeper?wt=json&detail=true&path=/clusterstate.json'

4.2 檢視叢集中的所有core

curl 'http://localhost:8080/solr/admin/collections?action=LIST'

4.3 檢視叢集的健康狀況

curl 'http://localhost:8080/solr/admin/collections?action=CLUSTERSTATUS'

可以檢視到Shard的路由、活躍狀態、副本狀態等資訊.

5 操作集合別名(操作成功, 但未查出區別)

Solr允許使用者建立獨立的指向一個或多個真實集合的虛擬集合, 可以在執行時修改別名.

  • 建立或修改別名:

    curl 'http://localhost:8080/solr/admin/collections?action=CREATEALIAS&name=alias&collections=collection1&indent=true'
    

    用來修改的別名應該只對映一個獨立的集合, 讀取的別名能對映一個或多個集合.

  • 移除存在的別名:

    curl 'http://localhost:8080/solr/admin/collections?action=DELETEALIAS&name=alias&indent=true'
    

6 切割分片

curl 'http://localhost:8080/solr/admin/collections?action=SPLITSHARD&collection=mycollection&shard=shard2&indent=true'

collection: 集合的名稱;

shard: 將被切割的分片ID, 必須存在, 且當前Collection的shard個數必須大於1個(已驗證).

  • 該特性發佈於Solr4.3, 測試結果如圖:
    圖01

SPLITSHARD命令不能用於使用了自定義雜湊的叢集, 因為這樣的叢集沒有一個明確的雜湊範圍. 它只用於具有plain或compositeid路由的叢集;

② 該命令將指定的shard的切割成 兩個新的具有相同資料的分片, 並根據新分片的路由範圍切割父分片 (被切割的shard) 中的文件. 新分片將被命名為xxx_0xxx_1;

③ 一旦新分片被成功建立, 它們就會被立即啟用, 同時父分片也將被暫停 —— 新的讀寫請求就不會被髮送到父分片中了, 而是直接路由到新的切割生成的新分片中;

該特徵能夠保證無縫切割和無故障時間: 父分片資料不會被刪除, 在切割操作完成之前, 父分片將繼續提供讀寫請求, 直到切割完成.

切割分片後, 再使用DELETESHARD命令刪除被切割的原始Shard, 就能保證資料不冗餘.

注意:
SolrCloud不支援對索引到其他Shard上的資料的動態遷移:
—— 通過切割分片實現SolrCloud的擴容, 只會對切割操作之後、路由的雜湊範圍仍然屬於原分片的資料進行擴容.

7 叢集配置的更新

叢集會發生變化的就是collection的配置, 因此當配置檔案發生變化後就應該使用命令更新ZooKeeper中的配置資訊. 對此, Solr提供了很好的運維工具:

7.1 將配置檔案上傳到ZooKeeper中

需要的jar包: $SOLR_HOME/example/lib/ext/* 以及 Solr專案的WEB-INF/lib/*, 這裡已經將ext下的jar包拷貝到了WEB-INF/lib下, 便捷很多.

cd /data/solr-cloud/tomcat/display/solr/WEB-INF/lib

下述一長串是一條命令, 為了便於檢視, 使用了反斜槓(\)來斷句, 如果使用中出現問題, 可將反斜槓(\)刪除, 並刪除所有的換行, 然後回車執行.

# 注意本地路徑, 以及../classes/log4j.properties檔案的路徑
java -classpath .:* \
-Dlog4j.configuration=file:../classes/log4j.properties \
org.apache.solr.cloud.ZkCLI -cmd upconfig \
-zkhost 10.0.20.50:2181,10.0.20.51:2181,10.0.20.52:2181 \
-confdir /data/solr-cloud/tomcat/solrhome/collection1/conf \
-confname myconf2  

建議: 將配置檔案單獨存放, 比如: 我這裡將整個 example/solr/collection1/conf 存放至 /data/solr-cloud/tomcat 下, 之後執行配置檔案的更新:

java -classpath .:* \
-Dlog4j.configuration=file:../classes/log4j.properties \
org.apache.solr.cloud.ZkCLI -cmd upconfig \
-zkhost 10.0.20.50:2181,10.0.20.51:2181,10.0.20.52:2181 \
-confdir /data/solr-cloud/tomcat/conf \
-confname myconf  

7.2 將ZooKeeper中的配置檔案與Collection相關聯

cd /data/solr-cloud/tomcat/display/solr/WEB-INF/lib

java -classpath .:* \
-Dlog4j.configuration=file:../classes/log4j.properties \
org.apache.solr.cloud.ZkCLI -cmd linkconfig \
-collection mycollection -confname myconf \
-zkhost 10.0.20.50:2181,10.0.20.51:2181,10.0.20.52:2181  

注意: 配置檔案若被刪除, 將會導致ZooKeeper中的配置檔案被同步刪除, 從而在建立collection時將出現問題.

可關閉Tomcat服務與ZooKeeper服務, 刪除ZooKeeper目錄下的配置檔案的版本資訊, 然後再次啟動ZooKeeper更新配置檔案, 最後啟動Tomcat服務繼續測試.

參考資料

https://lucene.apache.org/solr/guide/6_6/collections-api.html#CollectionsAPI-splitshard

https://blog.csdn.net/xyls12345/article/details/27504965

https://blog.csdn.net/woshiwanxin102213/article/details/18793271

版權宣告

作者: ma_shoufeng(馬瘦風)

出處: CSDN 馬瘦風的部落格

您的支援是對博主的極大鼓勵, 感謝您的閱讀.

本文版權歸博主所有, 歡迎轉載, 但未經博主同意必須保留此段宣告, 且在文章頁面明顯位置給出原文連結, 否則博主保留追究相關人員法律責任的權利.