-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreating_unmanaged_table.py
42 lines (33 loc) · 1.04 KB
/
creating_unmanaged_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from pyspark.sql import SparkSession
spark=(
SparkSession
.builder
.config("spark.sql.warehouse.dir","spark-warehouse")
.appName("creating_unmanaged_table")
.getOrCreate()
)
#create a unmanaged table using Spark SQL
spark.sql("""CREATE TABLE us_flight_delays_tbl(
date STRING,
delay INT,
distance INT,
origin STRING,
destination STRING)
USING CSV
OPTIONS(PATH='data/flight_data/csv/departuredelays.csv',HEADER=True)""")
spark.sql("SELECT * FROM us_flight_delays_tbl").show(5)
print(spark.catalog.listTables())
#create a unmanaged table using DataFrame API
flight_df=(
spark.read.format("csv")
.option("header","True")
.schema("date STRING,delay INT,distance INT,origin STRING,destination STRING")
.load("data/flight_data/csv/departuredelays.csv")
)
(
flight_df.write
.option("path","/tmp/data/us_flight_data")
.saveAsTable("us_flight_delays_tbl_unmanaged")
)
print(spark.catalog.listTables())
spark.stop()