-
Notifications
You must be signed in to change notification settings - Fork 2
/
defining_schema.py
80 lines (68 loc) · 2.65 KB
/
defining_schema.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType,StructField,StringType,IntegerType,ArrayType,MapType
spark = (SparkSession
.builder
.master("local")
.appName("defining_schema")
.getOrCreate())
spark.sparkContext.setLogLevel("WARN")
#print(spark.sparkContext.uiWebUrl)
data = [("James","","Smith","36636","M",3000),
("Michael","Rose","","40288","M",4000),
("Robert","","Williams","42114","M",4000),
("Maria","Anne","Jones","39192","F",4000),
("Jen","Mary","Brown","","F",-1)
]
schema = StructType([
StructField("firstname",StringType(),True),
StructField("middlename",StringType(),True),
StructField("lastname",StringType(),True),
StructField("id",StringType(),True),
StructField("gender",StringType(),True),
StructField("salary",IntegerType(),True)
])
df = spark.createDataFrame(data=data,schema=schema)
df.printSchema()
df.show(truncate=False)
structuredData = [(("James","","Smith"),"36636","M",3000),
(("Michael","Rose",""),"40288","M",4000),
(("Robert","","Williams"),"42114","M",4000),
(("Maria","Anne","Jones"),"39192","F",4000),
(("Jen","Mary","Brown"),"","F",-1)
]
structuredSchema = StructType([
StructField("name",
StructType([
StructField("firstname",StringType(),True),
StructField("middlename",StringType(),True),
StructField("lastname",StringType(),True)
]),True
),
StructField("id",StringType(),True),
StructField("gender",StringType(),True),
StructField("salary",IntegerType(),True)
])
df2 = spark.createDataFrame(data=structuredData,schema=structuredSchema)
df2.printSchema()
df2.show(truncate=False)
arrayStructuredSchema = StructType([
StructField("name",StructType([
StructField("firstname",StringType(),True),
StructField("middlename",StringType(),True),
StructField("lastname",StringType(),True)
])),
StructField("hobbies",ArrayType(StringType()),True),
StructField("properties",MapType(StringType(),StringType()),True)
])
arrayStructuredData = [(("James","","Smith"),["cricket","badminton","football"],{"noida":"Sector 59","delhi":"NSP"}),
(("Michael","Rose",""),["table tennis","hockey"],{"etawah":"civil lines"}),
(("Robert","","Williams"),["basketball","table tennis","hockey"],{"kanpur":"mall road"}),
(("Maria","Anne","Jones"),["tennis"],{"noida":"Atta"}),
(("Jen","Mary","Brown"),[],{})
]
df3= spark.createDataFrame(data=arrayStructuredData,schema=arrayStructuredSchema)
df3.printSchema()
print(df3.schema.simpleString())
df3.show(truncate=False)
x = input("Press any key")
spark.stop()