Dropping Tables and DatabasesΒΆ
Let us understand how to DROP Spark Metastore Tables as well as Databases.
Let us start spark context for this Notebook so that we can execute the code provided. You can sign up for our 10 node state of the art cluster/labs to learn Spark SQL using our unique integrated LMS.
val username = System.getProperty("user.name")
import org.apache.spark.sql.SparkSession
val username = System.getProperty("user.name")
val spark = SparkSession.
builder.
config("spark.ui.port", "0").
config("spark.sql.warehouse.dir", s"/user/${username}/warehouse").
enableHiveSupport.
appName(s"${username} | Spark SQL - Managing Tables - Basic DDL and DML").
master("yarn").
getOrCreate
If you are going to use CLIs, you can use Spark SQL using one of the 3 approaches.
Using Spark SQL
spark2-sql \
--master yarn \
--conf spark.ui.port=0 \
--conf spark.sql.warehouse.dir=/user/${USER}/warehouse
Using Scala
spark2-shell \
--master yarn \
--conf spark.ui.port=0 \
--conf spark.sql.warehouse.dir=/user/${USER}/warehouse
Using Pyspark
pyspark2 \
--master yarn \
--conf spark.ui.port=0 \
--conf spark.sql.warehouse.dir=/user/${USER}/warehouse
We can use DROP TABLE command to drop the table.. Let us drop orders table
%%sql
CREATE DATABASE IF NOT EXISTS itversity_retail
%%sql
USE itversity_retail
%%sql
SHOW tables
%%sql
CREATE TABLE IF NOT EXISTS orders (
order_id INT,
order_date STRING,
order_customer_id INT,
order_status STRING
) ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
%%sql
DROP TABLE orders
%%sql
DROP TABLE IF EXISTS orders
DROP TABLE on managed table will delete both metadata in metastore as well as data in HDFS, while DROP TABLE on external table will only delete metadata in metastore.
We can drop database by using DROP DATABASE Command. However we need to drop all the tables in the database first.
Here is the example to drop the database itversity_retail -
DROP DATABASE itversity_retail
We can also drop all the tables and databases by adding CASCADE.
%%sql
DROP DATABASE itversity_retail
%%sql
DROP DATABASE IF EXISTS itversity_retail CASCADE