配置单元“ANALYZE TABLE”如何从java执行 [英] Hive "ANALYZE TABLE" how to execute from java

查看:438
本文介绍了配置单元“ANALYZE TABLE”如何从java执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要计算配置单元表中的行数,对于
我使用的是查询:

I need to compute the number of rows in a hive table, for that I am using the query:

ANALYZE TABLE p_7 COMPUTE STATISTICS noscan

我想通过java获取结果,低于
的代码并且没有运气。我得到的错误是:

I want to fetch the results through java, I am trying with the below code and have no luck. the error I get is :

Exception in thread "main" java.sql.SQLException: The query did not generate a result set!
at org.apache.hive.jdbc.HiveStatement.executeQuery(HiveStatement.java:393)
at HiveJdbcClient.main(HiveJdbcClient.java:22)

我正在使用的代码是:

code I am using is :

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import java.sql.DriverManager;

public class HiveJdbcClient {
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
public static void main(String[] args) throws SQLException {
    try {
        Class.forName(driverName);
    } catch (ClassNotFoundException e) {

        e.printStackTrace();
        System.exit(1);
    }
    Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "hive", "");
    System.out.println("connected");
    Statement statement = con.createStatement();
    String query = "ANALYZE TABLE p_7 COMPUTE STATISTICS noscan";
    ResultSet res = statement.executeQuery(query);
}
}

我不知道如何执行查询,例如:

I dont know how to execute a query such as:

ANALYZE TABLE p_7 COMPUTE STATISTICS noscan

通过java。任何帮助对我来说都是很有帮助的。谢谢。

through java. Any help on this would be of great help to me. Thanks.

推荐答案

使用不带'NOSCAN'的ANALYZE TABLE语句来计算行数。
注意:此语句不会生成resultSet对象。

Use the ANALYZE TABLE statement without 'NOSCAN' to compute the number of rows. Note: This statement does not produce resultSet object.

要获取存储的统计信息,请使用以下语句。

To fetch the stored stats, use the following statement.

DESCRIBE FORMATTED tableName

在输出中,行数在参数数组中列出。使用正则表达式提取它。

In the output, the number of rows is listed in parameters array. Use regex to extract it.

以下是示例代码:

Here is the sample code:

String analyzeQuery = "ANALYZE TABLE p_7 COMPUTE STATISTICS";
String describeQuery = "DESCRIBE FORMATTED p_7";

stmt.execute(analyzeQuery);
StringBuilder sb = new StringBuilder();
try (ResultSet rs = stmt.executeQuery(describeQuery)) {
   while (rs.next()) {
       int count = rs.getMetaData().getColumnCount();
       for (int j = 1; j <= count; j++) {
          sb.append(rs.getString(j));
       }
   }
}
System.out.println("Output: "+ sb.toString());

请参阅 https://cwiki.apache.org/confluence/display/Hive/StatsDev 了解表格和分区统计信息。

Refer https://cwiki.apache.org/confluence/display/Hive/StatsDev for details on Table and Partition statistics.

这篇关于配置单元“ANALYZE TABLE”如何从java执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆