-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdate-time.py
47 lines (35 loc) · 931 Bytes
/
date-time.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
import os
full_path = os.path.dirname(os.path.realpath(__file__))
full_path_spark = full_path + '/spark/'
# spark-version: spark-3.3.1-bin-hadoop3
os.environ["SPARK_HOME"] = full_path + "/spark/"
import findspark
findspark.init()
from pyspark.sql import SparkSession
from pyspark.sql.types import DoubleType, StringType
from pyspark.sql import functions as f
spark = SparkSession.builder \
.master('local[*]') \
.appName("Iniciando com Spark") \
.config('spark.ui.port', '4050') \
.getOrCreate()
data = [
{'data': 20221213},
{'data': 20221214},
{'data': 20221215},
{'data': 20221216}
]
# dados dicionario com datas
df = spark.createDataFrame(data)
# formato original
df.printSchema()
# formata data para padraodate
df = df \
.withColumn(
'data',
f.to_date(df.data.cast(StringType()), 'yyyyMMdd')
)
# formato novo
df.printSchema()
# mostra do tipo pandas
print(df.toPandas())