要计算 Apache Phoenix 的 count(*) 查询耗时,可以使用以下代码示例:
import java.sql.*;
public class PhoenixQueryTime {
public static void main(String[] args) {
String url = "jdbc:phoenix::";
String query = "SELECT COUNT(*) FROM ";
try {
// 创建连接
Connection conn = DriverManager.getConnection(url);
// 创建 Statement
Statement stmt = conn.createStatement();
// 获取开始时间
long startTime = System.currentTimeMillis();
// 执行查询
ResultSet rs = stmt.executeQuery(query);
// 获取结束时间
long endTime = System.currentTimeMillis();
// 计算耗时
long queryTime = endTime - startTime;
// 打印结果
while (rs.next()) {
System.out.println("Count: " + rs.getLong(1));
}
System.out.println("Query time: " + queryTime + "ms");
// 关闭连接
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
在上述代码中,需要将
替换为你的 Phoenix 服务器的主机名或 IP 地址,
替换为 Phoenix 服务器的端口号,
替换为你要查询的表名。
$ javac PhoenixQueryTime.java
$ java PhoenixQueryTime
上述代码将连接到 Phoenix 服务器,并执行 count(*) 查询。然后,它将打印查询结果和查询耗时(以毫秒为单位)。
注意:如果你使用的是 Kerberos 认证,还需要提供相应的认证配置。