我试图使用java驱动程序测量Cassandra中查询的服务器端查询执行时间。我使用rSet.getExecutionInfo().getQueryTrace().getDurationMicros()
,其中rSet是我执行查询的结果集。我使用cqlsh手动打开跟踪,并且能够通过cqlsh获取跟踪,但是当我使用java驱动程序尝试时,我得到一个空值。在使用java驱动程序时,我还应该做些什么来启用跟踪吗?
修改
添加用于查询和连接到群集的代码。
我用来查询和测量服务器端执行时间的代码片段
ResultSet rSet = session.execute("SELECT * from quelea.users where user_id = " + userId +" ALLOW Filtering");
System.out.println("Server-side query Execution time : " + rSet.getExecutionInfo().getQueryTrace()).getDurationMicros());
我用来连接Cassandra集群的代码
public void connect(final String node, final int port)
{
this.cluster = Cluster.builder().addContactPoint(node).withPort(port).build();
final Metadata metadata = cluster.getMetadata();
out.printf("Connected to cluster: %s\n", metadata.getClusterName());
for (final Host host : metadata.getAllHosts())
{
out.printf("Datacenter: %s; Host: %s; Rack: %s\n",
host.getDatacenter(), host.getAddress(), host.getRack());
}
session = cluster.connect();
}
答案 0 :(得分:2)
根据您提供的代码:
ResultSet rSet = session.execute("SELECT * from quelea.users where user_id = " + userId +" ALLOW Filtering");
您似乎没有启用跟踪,这就是getQueryTrace()
返回null的原因,因此getDurationMicros()
会抛出NullPointerException
。 (注意:在cqlsh中使用TRACING on;
时,它仅适用于您的cqlsh会话,而不是全局)。
要启用跟踪,您可以使用enableTracing()
,即:
Statement statement = new SimpleStatement("SELECT * from quelea.users where user_id = " + userId +" ALLOW Filtering").enableTracing();
ResultSet rSet = session.execute(statement);