當然也說明了一些建立 Collection 的一些條件,包括 Shard 的數量以及 Config 須先上傳到 Zookeeper
接下來介紹一下如何用 SolrJ 以及 RESTful 的方式建立一個 Collection
SolrJ 是一個套件能夠讓開發者透過 Java 的方式來操作 Solr,以下是建立 Collection 的片段程式
String zkHosts = "locahost:9983";
String collectionName = "testCollection1";
String confName = "testCollection1"; //Configuration Name (You must upload to zookeeper first)
int shards = 3;
CloudSolrServer server = new CloudSolrServer(zkHosts);
CollectionAdminResponse response = CollectionAdminRequest.createCollection(
collectionName, shards, confName , server);
if(null != response.getErrorMessages()){
/* print error here */
for(Entry error: response.getErrorMessages()){
result.addErrorMsg(error.getValue());
}
}
server.shutdown();
CollectionAdminRequest 有很多的重載方法,可以去查它的 API