tables

tables returns a DataFrame containing names of tables in the given database. The DataFrame returned will consist of 2 columns, tableName and isTemporary.

Syntax:

  • tables(sqlContext, “databaseName”)

Parameters:

  • sqlContext: SQLContext. This is already created for you in the Databricks notebooks, do not recreate!
  • databaseName: Optional, Name of database

Output:

  • SparkR DataFrame
# Create a database as an example
sql(sqlContext, "CREATE DATABASE testDB")

# Create SparkR DataFrame using the faithful dataset from R
df <- createDataFrame(sqlContext, faithful)

# Save df as temporary table in database testDB
registerTempTable(df, "tempTable")
# View tables in database testDB
collect(tables(sqlContext, "testDB"))
# tables() will display a full list of existing tables if no database name is specified
collect(tables(sqlContext))
# tables() returns the same results as sql's SHOW TABLES command
head(sql(sqlContext, "SHOW TABLES"))
# Drop Database testDB
head(sql(sqlContext, "DROP DATABASE testDB"))