如何使用 solrZkClient 和 zkStateReader 获取 Solr Cloud 的集群状态?
How to get the cluster state of Solr Cloud using the solrZkClient and zkStateReader?
我已经在我的本地系统上下载并安装了 Solr (SolrCloud) v. 4.10.3,并且可以 运行 它没有任何问题,索引文档,与其 web 交互 UI。我还可以使用以下命令行与其 ZooKeeper 进行交互:
zkcli.sh -z localhost:9983 -cmd get /clusterstate.json
它 returns 我关于 SolrCloud 的信息。现在,我正在尝试使用 Java.
以编程方式获取一组类似的信息
我试过以下方法:
SolrZkClient solrZkClient = new SolrZkClient("localhost:9983", 4000);
ZkStateReader zkStateReader = new ZkStateReader(solrZkClient);
System.err.println(zkStateReader.getClusterState());
System.err.println(zkStateReader.getClusterState().getLiveNodes());
可惜zkStateReader.getClusterState()returnsnull
.
在日志输出中我看到以下内容:
2015-04-23 15:19:04 INFO ZooKeeper:100 - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2015-04-23 15:19:04 INFO ZooKeeper:100 - Client environment:host.name=emre-ubuntu
2015-04-23 15:19:04 INFO ZooKeeper:100 - Client environment:java.version=1.8.0_25
2015-04-23 15:19:04 INFO ZooKeeper:100 - Client environment:java.vendor=Oracle Corporation
2015-04-23 15:19:04 INFO ZooKeeper:100 - Client environment:java.home=/usr/lib/jvm/java-8-oracle/jre
...
2015-04-23 15:19:04 INFO ZooKeeper:438 - Initiating client connection, connectString=localhost:9983 sessionTimeout=4000 watcher=org.apache.solr.common.cloud.ConnectionManager@3b22cdd0
2015-04-23 15:19:04 INFO ConnectionManager:207 - Waiting for client to connect to ZooKeeper
2015-04-23 15:19:04 INFO ClientCnxn:966 - Opening socket connection to server localhost/127.0.0.1:9983. Will not attempt to authenticate using SASL (unknown error)
2015-04-23 15:19:04 INFO ClientCnxn:849 - Socket connection established to localhost/127.0.0.1:9983, initiating session
2015-04-23 15:19:04 INFO ClientCnxn:1207 - Session establishment complete on server localhost/127.0.0.1:9983, sessionid = 0x14ce5f89eec000d, negotiated timeout = 4000
2015-04-23 15:19:04 INFO ConnectionManager:102 - Watcher org.apache.solr.common.cloud.ConnectionManager@3b22cdd0 name:ZooKeeperConnection Watcher:localhost:9983 got event WatchedEvent state:SyncConnected type:None path:null path:null type:None
2015-04-23 15:19:04 INFO ConnectionManager:225 - Client is connected to ZooKeeper
null
我错过了什么吗?我如何使用 Java 获取该信息?
我看到您正在尝试使用 java 获取 solrcloud 集群状态,您可以尝试这样的操作..
CloudSolrServer server = new CloudSolrServer("192.112.21.21:9983");
server.setZkConnectTimeout(15*60*1000);
server.setZkClientTimeout(15*60*1000);
server.setParser(new BinaryResponseParser());
server.setRequestWriter(new BinaryRequestWriter());
CollectionAdminRequest adminRequest = new CollectionAdminRequest();
adminRequest.setAction(CollectionAction.CLUSTERSTATUS);
CollectionAdminResponse adminResponse = adminRequest.process(server);
System.out.println(adminResponse.toString());
对于包含 2 个分片的集合,输出将类似于这样。,
{
responseHeader={
status=0,
QTime=1650
},
cluster={
collections={
collection1={
shards={
shard1={
range=80000000-ffffffff,
state=active,
replicas={
core_node2={
state=active,
core=collection1_shard1_replica1,
node_name=192.112.21.21: 8983_solr,
base_url=http: //192.112.21.21: 8983/solr,
leader=true
}
}
},
shard2={
range=0-7fffffff,
state=active,
replicas={
core_node1={
state=active,
core=collection1_shard2_replica1,
node_name=192.112.21.21: 8984_solr,
base_url=http: //192.112.21.21: 8984/solr,
leader=true
}
}
}
},
maxShardsPerNode=1,
router={
name=compositeId
},
replicationFactor=1,
autoAddReplicas=false,
autoCreated=true
}
},
live_nodes=[
192.112.21.21: 8983_solr,
192.112.21.21: 8984_solr
]
} }
@Vijay CloudSolrServer 已弃用。而是使用 CloudSolrClient。谢谢你的提示。从那里我找到了这个解决方案。谢谢。
如果你没有解决这个问题,我有一个解决方案给你。
这个方法我也需要它来检查来自另一个系统的副本。
final CloudSolrClient server = new CloudSolrClient("localhost:2181");
try {
//probably this is the line that missed from your code...
server.connect();
final ClusterState clusterState = server.getZkStateReader().getClusterState();
final DocCollection collection = clusterState.getCollection("collection1");
//EVRIKA! collection object
// and get the leader of the collection...pretty easy.
Replica leader = clusterState.getLeader("collection1", "shard1");
} catch (Exception e) {
// do your stuff
} finally {
server.close();
}
希望我最近的回答对其他人有用。
美好的一天。
我已经在我的本地系统上下载并安装了 Solr (SolrCloud) v. 4.10.3,并且可以 运行 它没有任何问题,索引文档,与其 web 交互 UI。我还可以使用以下命令行与其 ZooKeeper 进行交互:
zkcli.sh -z localhost:9983 -cmd get /clusterstate.json
它 returns 我关于 SolrCloud 的信息。现在,我正在尝试使用 Java.
以编程方式获取一组类似的信息我试过以下方法:
SolrZkClient solrZkClient = new SolrZkClient("localhost:9983", 4000);
ZkStateReader zkStateReader = new ZkStateReader(solrZkClient);
System.err.println(zkStateReader.getClusterState());
System.err.println(zkStateReader.getClusterState().getLiveNodes());
可惜zkStateReader.getClusterState()returnsnull
.
在日志输出中我看到以下内容:
2015-04-23 15:19:04 INFO ZooKeeper:100 - Client environment:zookeeper.version=3.4.5-1392090, built on 09/30/2012 17:52 GMT
2015-04-23 15:19:04 INFO ZooKeeper:100 - Client environment:host.name=emre-ubuntu
2015-04-23 15:19:04 INFO ZooKeeper:100 - Client environment:java.version=1.8.0_25
2015-04-23 15:19:04 INFO ZooKeeper:100 - Client environment:java.vendor=Oracle Corporation
2015-04-23 15:19:04 INFO ZooKeeper:100 - Client environment:java.home=/usr/lib/jvm/java-8-oracle/jre
...
2015-04-23 15:19:04 INFO ZooKeeper:438 - Initiating client connection, connectString=localhost:9983 sessionTimeout=4000 watcher=org.apache.solr.common.cloud.ConnectionManager@3b22cdd0
2015-04-23 15:19:04 INFO ConnectionManager:207 - Waiting for client to connect to ZooKeeper
2015-04-23 15:19:04 INFO ClientCnxn:966 - Opening socket connection to server localhost/127.0.0.1:9983. Will not attempt to authenticate using SASL (unknown error)
2015-04-23 15:19:04 INFO ClientCnxn:849 - Socket connection established to localhost/127.0.0.1:9983, initiating session
2015-04-23 15:19:04 INFO ClientCnxn:1207 - Session establishment complete on server localhost/127.0.0.1:9983, sessionid = 0x14ce5f89eec000d, negotiated timeout = 4000
2015-04-23 15:19:04 INFO ConnectionManager:102 - Watcher org.apache.solr.common.cloud.ConnectionManager@3b22cdd0 name:ZooKeeperConnection Watcher:localhost:9983 got event WatchedEvent state:SyncConnected type:None path:null path:null type:None
2015-04-23 15:19:04 INFO ConnectionManager:225 - Client is connected to ZooKeeper
null
我错过了什么吗?我如何使用 Java 获取该信息?
我看到您正在尝试使用 java 获取 solrcloud 集群状态,您可以尝试这样的操作..
CloudSolrServer server = new CloudSolrServer("192.112.21.21:9983");
server.setZkConnectTimeout(15*60*1000);
server.setZkClientTimeout(15*60*1000);
server.setParser(new BinaryResponseParser());
server.setRequestWriter(new BinaryRequestWriter());
CollectionAdminRequest adminRequest = new CollectionAdminRequest();
adminRequest.setAction(CollectionAction.CLUSTERSTATUS);
CollectionAdminResponse adminResponse = adminRequest.process(server);
System.out.println(adminResponse.toString());
对于包含 2 个分片的集合,输出将类似于这样。,
{
responseHeader={
status=0,
QTime=1650
},
cluster={
collections={
collection1={
shards={
shard1={
range=80000000-ffffffff,
state=active,
replicas={
core_node2={
state=active,
core=collection1_shard1_replica1,
node_name=192.112.21.21: 8983_solr,
base_url=http: //192.112.21.21: 8983/solr,
leader=true
}
}
},
shard2={
range=0-7fffffff,
state=active,
replicas={
core_node1={
state=active,
core=collection1_shard2_replica1,
node_name=192.112.21.21: 8984_solr,
base_url=http: //192.112.21.21: 8984/solr,
leader=true
}
}
}
},
maxShardsPerNode=1,
router={
name=compositeId
},
replicationFactor=1,
autoAddReplicas=false,
autoCreated=true
}
},
live_nodes=[
192.112.21.21: 8983_solr,
192.112.21.21: 8984_solr
]
} }
@Vijay CloudSolrServer 已弃用。而是使用 CloudSolrClient。谢谢你的提示。从那里我找到了这个解决方案。谢谢。
如果你没有解决这个问题,我有一个解决方案给你。 这个方法我也需要它来检查来自另一个系统的副本。
final CloudSolrClient server = new CloudSolrClient("localhost:2181");
try {
//probably this is the line that missed from your code...
server.connect();
final ClusterState clusterState = server.getZkStateReader().getClusterState();
final DocCollection collection = clusterState.getCollection("collection1");
//EVRIKA! collection object
// and get the leader of the collection...pretty easy.
Replica leader = clusterState.getLeader("collection1", "shard1");
} catch (Exception e) {
// do your stuff
} finally {
server.close();
}
希望我最近的回答对其他人有用。
美好的一天。