Last active
November 16, 2020 10:36
-
-
Save bejean/fa5471b8fd57559beb2ff3192508f7e3 to your computer and use it in GitHub Desktop.
Solr queries only on Shards leaders
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Solr 8 - Java sample in order to get shards leaders for a collection | |
# | |
package fr.eolya.solr.tests; | |
import org.apache.solr.client.solrj.impl.CloudSolrClient; | |
import org.apache.solr.common.cloud.Slice; | |
import org.apache.solr.common.cloud.ZkStateReader; | |
import org.apache.zookeeper.KeeperException; | |
import java.util.*; | |
public class GetLeaders { | |
public static void main(String[] args){ | |
final List<String> zkServers = new ArrayList<String>(); | |
zkServers.add("zk1:2181"); | |
zkServers.add("zk2:2181"); | |
zkServers.add("zk3:2181"); | |
CloudSolrClient cloudSolrClient = new CloudSolrClient.Builder(zkServers, Optional.empty()).build(); | |
try { | |
Map<String, String> leaders = getShardLeaders(cloudSolrClient, "sirene"); | |
for (String leader: leaders.values()) { | |
System.out.println(leader); | |
} | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} catch (KeeperException e) { | |
e.printStackTrace(); | |
} | |
} | |
static private Map<String, String> getShardLeaders(CloudSolrClient cloudSolrClient, String collection) throws InterruptedException, KeeperException { | |
Map<String, String> shardleaders = new TreeMap<String, String>(); | |
ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader(); | |
for (Slice slice : zkStateReader.getClusterState().getCollection(collection).getSlices()) { | |
shardleaders.put(slice.getName(), zkStateReader.getLeaderUrl(collection, slice.getName(), 30000)); | |
} | |
return shardleaders; | |
} | |
} | |
# | |
# Sample output | |
# | |
http://solr2:8983/solr/sirene_shard1_replica_t2/ | |
http://solr1:8983/solr/sirene_shard2_replica_t4/ | |
# | |
# Sample query using only shard's leaders | |
# | |
http://localhost:8983/solr/sirene/select?q=*:*&shards=solr2:8983/solr/sirene_shard1_replica_t2,solr1:8983/solr/sirene_shard2_replica_t4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment