Find Number of Rows of Hive Table via Scala

Find Number of Rows of Hive Table via Scala

To find the number of rows/records in a Hive table, we can use Spark SQL `count `aggregation function: [Hive SQL - Aggregate Functions Overview with Examples](https://kontext.tech/article/1083/hive-sql-aggregate-functions-overview-with-examples#h-count-and-count-distinct-function). This code snippet provide example of Scala code to implement the same. `spark-shell` is used directly for simplicity.  The code snippet can also run Jupyter Notebooks or Zeppelin with Spark kernel. Alternatively, it can be compiled to jar file and then submit as job via `spark-submit`. ![2022082315649-image.png](/api/flex/medias/obj-2739 "2022082315649-image.png")

Kontext Kontext 0 689 0.66 index 8/23/2022

Code description

To find the number of rows/records in a Hive table, we can use Spark SQL count aggregation function: Hive SQL - Aggregate Functions Overview with Examples.

This code snippet provide example of Scala code to implement the same. spark-shell is used directly for simplicity.  The code snippet can also run Jupyter Notebooks or Zeppelin with Spark kernel. Alternatively, it can be compiled to jar file and then submit as job via spark-submit

2022082315649-image.png

Code snippet

    val sql="select count(*) from test_db.test_table;";
    //sql: String = select count(*) from test_db.test_table;
    
    val df = spark.sql(sql);
    //df: org.apache.spark.sql.DataFrame = [count(1): bigint]
    
    df.show()
    /*
    +--------+
    |count(1)|
    +--------+
    |       5|
    +--------+
    */
    
    val df1 = spark.sql("select * from test_db.test_table;");
    //df1: org.apache.spark.sql.DataFrame = [id: int, attr: string]
    
    df1.groupBy().count()
    //res5: org.apache.spark.sql.DataFrame = [count: bigint]
    
    println(df1.groupBy().count())
    //[count: bigint]
    
    df1.groupBy().count().show()
    /*+-----+
    |count|
    +-----+
    |    5|
    +-----+
    */
    
hive scala

Join the Discussion

View or add your thoughts below

Comments