forked from spark-examples/pyspark-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.py
28 lines (21 loc) · 880 Bytes
/
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
# -*- coding: utf-8 -*-
"""
Created on Thu Oct 24 22:42:50 2019
@author: prabha
"""
import pyspark
from pyspark.sql import SparkSession
from pyspark.sql.functions import col
from pyspark.sql.functions import to_timestamp
from pyspark.sql.types import StructType, StructField, StringType, IntegerType,DateType
spark = SparkSession.builder.appName('SparkByExamples.com').getOrCreate()
schema = StructType([
StructField("city", StringType(), True),
StructField("dates", StringType(), True),
StructField("population", IntegerType(), True)])
dates = ["1991-02-25","1998-05-10", "1993/03/15", "1992/07/17"]
cities = ['Caracas', 'Ccs', ' São Paulo ', '~Madrid']
population = [37800000, 19795791, 12341418, 6489162]
# Dataframe:
df = spark.createDataFrame(list(zip(cities, dates, population)), schema=schema)
df.show(truncate=False)