Using Dynamic Partition ModeΒΆ
Let us understand how we can insert data into partitioned table using dynamic partition mode.
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 - DML and Partitioning").
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
Using dynamic partition mode we need not pre create the partitions. Partitions will be automatically created when we issue INSERT command in dynamic partition mode.
To insert data using dynamic partition mode, we need to set the property
hive.exec.dynamic.partition
to trueAlso we need to set
hive.exec.dynamic.partition.mode
to nonstrict
Here is the example of inserting data into partitions using dynamic partition mode.
%%sql
USE itversity_retail
%%sql
SHOW tables
%%sql
SELECT count(1) FROM orders
%%sql
SELECT count(1) FROM orders_part
%%sql
SET hive.exec.dynamic.partition
%%sql
SET hive.exec.dynamic.partition.mode
%%sql
SET hive.exec.dynamic.partition=true
%%sql
SET hive.exec.dynamic.partition.mode=nonstrict
%%sql
INSERT INTO TABLE orders_part PARTITION (order_month)
SELECT o.*, date_format(order_date, 'yyyyMM') order_month
FROM orders o
WHERE order_date >= '2013-12-01 00:00:00.0'
import sys.process._
s"hdfs dfs -ls -R /user/${username}/warehouse/${username}_retail.db/orders_part" !
%%sql
SELECT count(1) FROM orders
%%sql
SELECT count(1) FROM orders_part
You will see new partitions created starting from 201312 to 201407.