description Code
visibility 85 comment 0 access_time 8 months ago language English

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.

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


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]
|       5|

val df1 = spark.sql("select * from test_db.test_table;");
//df1: org.apache.spark.sql.DataFrame = [id: int, attr: string]

//res5: org.apache.spark.sql.DataFrame = [count: bigint]

//[count: bigint]

|    5|
fork_right Fork
copyright This page is subject to Site terms.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

comment Comments
No comments yet.