简体   繁体   English

使用Java驱动程序跟踪Cassandra查询时出现空指针错误

[英]Null pointer error when tracing Cassandra query using java driver

I'm trying to measure the server side query execution time for a query in Cassandra using the java driver. 我正在尝试使用Java驱动程序来衡量Cassandra中查询的服务器端查询执行时间。 I'm using rSet.getExecutionInfo().getQueryTrace().getDurationMicros() where rSet is the result set for the query I'm executing. 我正在使用rSet.getExecutionInfo().getQueryTrace().getDurationMicros() ,其中rSet是我正在执行的查询的结果集。 I manually turned on tracing using cqlsh and was able to get the trace through cqlsh but I get a null value when I try it with the java driver. 我使用cqlsh手动打开了跟踪,并且能够通过cqlsh进行跟踪,但是当我使用java驱动程序尝试时,我得到的是空值。 Is there something else I should be doing to enable tracing while using the java driver? 使用Java驱动程序时,我还应该做些其他事情来启用跟踪吗?

EDIT 编辑

Adding code used to query and connect to the cluster. 添加用于查询和连接到集群的代码。

The code snippet I use to query and measure server side execution time 我用来查询和衡量服务器端执行时间的代码片段

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());

The code I use to connect to the Cassandra cluster 我用来连接到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();  
   }  

Based on your provided code: 根据您提供的代码:

ResultSet rSet = session.execute("SELECT * from quelea.users where user_id = " + userId +" ALLOW Filtering");

It appears that you are not enabling tracing, which is why getQueryTrace() returns null, thus why getDurationMicros() throws a NullPointerException . 看来您没有启用跟踪,这就是为什么getQueryTrace()返回null,为什么getDurationMicros()抛出NullPointerException ( Note : when using TRACING on; in cqlsh, it only applies to your cqlsh session, not globally). 注意 :在cqlsh中使用TRACING on; ,它仅适用于cqlsh会话,不适用于全局)。

To enable tracing you can use enableTracing() , ie: 要启用跟踪,可以使用enableTracing() ,即:

Statement statement = new SimpleStatement("SELECT * from quelea.users where user_id = " + userId +" ALLOW Filtering").enableTracing();
ResultSet rSet = session.execute(statement);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM