Create Dummy Data Frame¶
Let us go ahead and create data frame using dummy data to explore Spark functions.
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.
from pyspark.sql import SparkSession
import getpass
username = getpass.getuser()
spark = SparkSession. \
builder. \
config('spark.ui.port', '0'). \
config("spark.sql.warehouse.dir", f"/user/{username}/warehouse"). \
enableHiveSupport(). \
appName(f'{username} | Python - Processing Column Data'). \
master('yarn'). \
getOrCreate()
If you are going to use CLIs, you can use Spark SQL using one of the 3 approaches.
Using Spark SQL
Using Scala
Using Pyspark
Once Data Frame is created, we can use to understand how to use functions. For example, to get current date, we can run df.select(current_date()).show()
.
It is similar to Oracle Query SELECT sysdate FROM dual
Here is another example of creating Data Frame using collection of employees. We will be using this Data Frame to explore all the important functions to process column data in detail.
employees = [
(1, "Scott", "Tiger", 1000.0,
"united states", "+1 123 456 7890", "123 45 6789"
),
(2, "Henry", "Ford", 1250.0,
"India", "+91 234 567 8901", "456 78 9123"
),
(3, "Nick", "Junior", 750.0,
"united KINGDOM", "+44 111 111 1111", "222 33 4444"
),
(4, "Bill", "Gomes", 1500.0,
"AUSTRALIA", "+61 987 654 3210", "789 12 6118"
)
]
+-----------+----------+---------+------+--------------+----------------+-----------+
|employee_id|first_name|last_name|salary|nationality |phone_number |ssn |
+-----------+----------+---------+------+--------------+----------------+-----------+
|1 |Scott |Tiger |1000.0|united states |+1 123 456 7890 |123 45 6789|
|2 |Henry |Ford |1250.0|India |+91 234 567 8901|456 78 9123|
|3 |Nick |Junior |750.0 |united KINGDOM|+44 111 111 1111|222 33 4444|
|4 |Bill |Gomes |1500.0|AUSTRALIA |+61 987 654 3210|789 12 6118|
+-----------+----------+---------+------+--------------+----------------+-----------+