-
Notifications
You must be signed in to change notification settings - Fork 2
/
databricks-crud-api-setup.py
58 lines (45 loc) · 1.55 KB
/
databricks-crud-api-setup.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
# Databricks notebook source
from pyspark.sql.types import *
from pyspark.sql.functions import *
from pyspark.sql.window import *
from datetime import datetime
# COMMAND ----------
#Assign variable values
mountPoint = "/mnt/reservoir/databrickscrudapi/"
deltaLakeDb = "crudapidb"
deltaLakeTable = "customer"
deltaPath = mountPoint + deltaLakeDb + "/" + deltaLakeTable + "/"
print(deltaPath)
# COMMAND ----------
dropstatement = "DROP TABLE IF EXISTS " + deltaLakeDb + "." + deltaLakeTable
spark.sql(dropstatement)
dbutils.fs.rm(deltaPath, recurse=True)
# COMMAND ----------
spark.sql(f"CREATE DATABASE IF NOT EXISTS {deltaLakeDb}")
createTblCmd = f"""
CREATE TABLE IF NOT EXISTS {deltaLakeDb}.{deltaLakeTable}
(
Id INT
,Name STRING
,Email STRING
,Country STRING
,Car STRING
,FavoriteFood STRING
,Registered DATE
,Active BOOLEAN
,Age INTEGER
)
USING DELTA
LOCATION "{deltaPath}"
"""
spark.sql(createTblCmd)
# COMMAND ----------
rawdata = mountPoint + "crudapidata.csv"
df = spark.read.option("delimiter", ";").option("header", True).option("inferSchema", True).csv(rawdata)
df = df.withColumn("Registered", to_date(col('Registered'), "MM/dd/yyyy"))
df = df.withColumn("Id", row_number().over(Window.orderBy("Name", "Email")))
tabeleSchema = spark.read.table(deltaLakeDb + "." + deltaLakeTable)
df.select(tabeleSchema.columns).write.insertInto("{}.{}".format(deltaLakeDb, deltaLakeTable), overwrite=True)
# COMMAND ----------
# MAGIC %sql
# MAGIC select * from crudapidb.customer