-
Notifications
You must be signed in to change notification settings - Fork 2
/
creating_managed_table.py
62 lines (47 loc) · 1.52 KB
/
creating_managed_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from pyspark.sql import SparkSession
spark=(
SparkSession
.builder
.appName("creating_managed_tables")
.config("spark.sql.warehouse.dir", "spark-warehouse")
.enableHiveSupport()
.getOrCreate()
)
#default database
print(spark.catalog.listDatabases())
spark.sql("CREATE DATABASE IF NOT EXISTS learn_spark_db")
spark.sql("USE learn_spark_db")
print(spark.catalog.listDatabases())
#creating a managed table using SQL
spark.sql("""CREATE TABLE IF NOT EXISTS managed_us_delay_flights_tbl(
date STRING,
delay INT,
distance INT,
origin STRING,
destination STRING)""")
path="data/departuredelays.csv"
schema="date STRING,delay INT,distance INT,origin STRING,destination STRING"
#read the data into a DataFrame
df=(
spark.read.format("csv")
.option("header","True")
.schema(schema)
.load(path)
)
#register a temp view
df.createOrReplaceTempView("departuredelays_data")
#Insert data into table using temp view
spark.sql("INSERT INTO managed_us_delay_flights_tbl SELECT * FROM departuredelays_data")
spark.sql("SELECT * FROM managed_us_delay_flights_tbl").show(10)
#creating a managed table using DataFrame API
flight_df=(
spark.read.format("csv")
.option("header","True")
.schema(schema)
.load(path)
)
flight_df.write.saveAsTable("managed_us_delay_flights_tbl_using_df_api",mode="overwrite")
print(spark.catalog.listTables())
#drop database
#spark.sql("DROP DATABASE IF EXISTS learn_spark_db CASCADE;")
spark.stop()