<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic List all delta tables in a database with total size, last snapshot size and user using python/sql in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/list-all-delta-tables-in-a-database-with-total-size-last/m-p/37934#M5475</link>
    <description>&lt;P&gt;I am trying to list all delta tables in a database and retrieve the following columns: `&lt;STRONG&gt;totalsizeinbyte&lt;/STRONG&gt;`, `&lt;STRONG&gt;sizeinbyte&lt;/STRONG&gt;` (i.e. the size of last snap shot size) and `&lt;STRONG&gt;created_by&lt;/STRONG&gt;` (`lastmodified_by` could also work). Checking online I came across the following post, where you can almost achieve this task without the user information in `scala`:&amp;nbsp;&lt;A href="https://stackoverflow.com/a/73893361/6903605" target="_blank" rel="noopener"&gt;https://stackoverflow.com/a/73893361/6903605&amp;nbsp;. &lt;/A&gt;&lt;/P&gt;&lt;P data-unlink="true"&gt;The problem is I would like to get results using `python` (`sql` could also work).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Attempt #1 : ``&lt;STRONG&gt;scala&lt;/STRONG&gt; solution using the post above retrieves&amp;nbsp;`&lt;STRONG&gt;totalsizeinbyte&lt;/STRONG&gt;`, `&lt;STRONG&gt;sizeinbyte&lt;/STRONG&gt;` but not `&lt;STRONG&gt;created_by`.&amp;nbsp;&lt;/STRONG&gt;Also this is scala, not python.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%scala
import com.databricks.sql.transaction.tahoe._
 
val databasePath = "dbfs:/user/hive/databasename.db"
 
def size(path: String): Long =
  dbutils.fs.ls(path).map { fi =&amp;gt; if (fi.isDir) size(fi.path) else fi.size }.sum
 
val tables = dbutils.fs.ls(databasePath).par.map { fi =&amp;gt;
  val totalSize = size(fi.path)
  val snapshotSize = DeltaLog.forTable(spark, fi.path).snapshot.sizeInBytes
  (fi.name, totalSize, snapshotSize)
}
display(tables.seq.sorted.toDF("name", "total_size_in_byte","snapshotSize_in_byte"))
// df.write.toTable("&amp;lt;table-name&amp;gt;")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Attempt #2:&lt;/P&gt;&lt;P&gt;Looping `sql` query using `python`. Here, &lt;STRONG&gt;total size&lt;/STRONG&gt; and &lt;STRONG&gt;created_by&lt;/STRONG&gt; is missing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from functools import reduce
from pyspark.sql import DataFrame

db_name = 'databasename'

#Create initial df of all tables in a database
tbl_lst= spark.sql("SHOW TABLES IN {}".format(db_name))

tbl_lst.createOrReplaceTempView("tbl_lst")
#Create array of all database tables
table_array= spark.sql("select collect_list(tableName) from tbl_lst where isTemporary == 'false'").collect()[0][0]
#For loop to get describe detail for each table in the array
sql_lst = [f"DESCRIBE DETAIL {db_name}.{table}" for table in table_array]

all_tbls=[]
success=0
fail=0
for sql in sql_lst:
  try:
    all_tbls.append(spark.sql(sql))
    success=success+1
  except:
    print('Error in:',sql)
    fail=fail+1

#Union multiple dataframe into one
tbl_details = reduce(DataFrame.unionAll, all_tbls)
tbl_details.createOrReplaceTempView("db_tbls_detail")
display(tbl_details)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jul 2023 08:34:28 GMT</pubDate>
    <dc:creator>Volkan_Gumuskay</dc:creator>
    <dc:date>2023-07-19T08:34:28Z</dc:date>
    <item>
      <title>List all delta tables in a database with total size, last snapshot size and user using python/sql</title>
      <link>https://community.databricks.com/t5/get-started-discussions/list-all-delta-tables-in-a-database-with-total-size-last/m-p/37934#M5475</link>
      <description>&lt;P&gt;I am trying to list all delta tables in a database and retrieve the following columns: `&lt;STRONG&gt;totalsizeinbyte&lt;/STRONG&gt;`, `&lt;STRONG&gt;sizeinbyte&lt;/STRONG&gt;` (i.e. the size of last snap shot size) and `&lt;STRONG&gt;created_by&lt;/STRONG&gt;` (`lastmodified_by` could also work). Checking online I came across the following post, where you can almost achieve this task without the user information in `scala`:&amp;nbsp;&lt;A href="https://stackoverflow.com/a/73893361/6903605" target="_blank" rel="noopener"&gt;https://stackoverflow.com/a/73893361/6903605&amp;nbsp;. &lt;/A&gt;&lt;/P&gt;&lt;P data-unlink="true"&gt;The problem is I would like to get results using `python` (`sql` could also work).&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Attempt #1 : ``&lt;STRONG&gt;scala&lt;/STRONG&gt; solution using the post above retrieves&amp;nbsp;`&lt;STRONG&gt;totalsizeinbyte&lt;/STRONG&gt;`, `&lt;STRONG&gt;sizeinbyte&lt;/STRONG&gt;` but not `&lt;STRONG&gt;created_by`.&amp;nbsp;&lt;/STRONG&gt;Also this is scala, not python.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%scala
import com.databricks.sql.transaction.tahoe._
 
val databasePath = "dbfs:/user/hive/databasename.db"
 
def size(path: String): Long =
  dbutils.fs.ls(path).map { fi =&amp;gt; if (fi.isDir) size(fi.path) else fi.size }.sum
 
val tables = dbutils.fs.ls(databasePath).par.map { fi =&amp;gt;
  val totalSize = size(fi.path)
  val snapshotSize = DeltaLog.forTable(spark, fi.path).snapshot.sizeInBytes
  (fi.name, totalSize, snapshotSize)
}
display(tables.seq.sorted.toDF("name", "total_size_in_byte","snapshotSize_in_byte"))
// df.write.toTable("&amp;lt;table-name&amp;gt;")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;Attempt #2:&lt;/P&gt;&lt;P&gt;Looping `sql` query using `python`. Here, &lt;STRONG&gt;total size&lt;/STRONG&gt; and &lt;STRONG&gt;created_by&lt;/STRONG&gt; is missing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from functools import reduce
from pyspark.sql import DataFrame

db_name = 'databasename'

#Create initial df of all tables in a database
tbl_lst= spark.sql("SHOW TABLES IN {}".format(db_name))

tbl_lst.createOrReplaceTempView("tbl_lst")
#Create array of all database tables
table_array= spark.sql("select collect_list(tableName) from tbl_lst where isTemporary == 'false'").collect()[0][0]
#For loop to get describe detail for each table in the array
sql_lst = [f"DESCRIBE DETAIL {db_name}.{table}" for table in table_array]

all_tbls=[]
success=0
fail=0
for sql in sql_lst:
  try:
    all_tbls.append(spark.sql(sql))
    success=success+1
  except:
    print('Error in:',sql)
    fail=fail+1

#Union multiple dataframe into one
tbl_details = reduce(DataFrame.unionAll, all_tbls)
tbl_details.createOrReplaceTempView("db_tbls_detail")
display(tbl_details)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jul 2023 08:34:28 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/list-all-delta-tables-in-a-database-with-total-size-last/m-p/37934#M5475</guid>
      <dc:creator>Volkan_Gumuskay</dc:creator>
      <dc:date>2023-07-19T08:34:28Z</dc:date>
    </item>
  </channel>
</rss>

