-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Project/mentoria spark #9
base: project/mentoria-spark
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from app_name.configs.spark_helper import create_delta_lake_session | ||
from pyspark.sql import functions as F | ||
from pyspark.sql.types import * | ||
|
||
|
||
# bronze to silver function | ||
def bronze_to_silver(bronze_path: str, silver_path: str, schema:str, partition:str, file_type:str): | ||
spark = create_delta_lake_session('bronze_to_silver') | ||
spark.conf.set('spark.sql.sources.partitionOverwriteMode', 'dynamic') | ||
|
||
if file_type=='csv': | ||
|
||
# Create df | ||
df = (spark.read.option("delimiter", ";") | ||
.option("header", True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use args and kwargs para controlar as opções de leitura dos CSVs. Não é mais necessário passar os parâmetros de leitura como options. Todos podem ser passados dentro do método .csv. Verifique a documentação |
||
.option('encoding', 'ISO-8859-1') | ||
.option('dateFormat', 'dd-MM-yyyy') | ||
.schema(schema) | ||
.csv(bronze_path)) | ||
|
||
# write df to parquet | ||
df.write.parquet(path=silver_path, mode="overwrite", partitionBy=partition) | ||
|
||
spark.stop() | ||
|
||
else: | ||
print('Error: wrong file type!') |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from general_etl import bronze_to_silver | ||
from pyspark.sql.types import * | ||
|
||
|
||
# variable | ||
bronze_audit_path = "s3://bronze/auditoria_municipal/6-siap-net-orgaos-municipais-autoridades-2016.csv" | ||
silver_audit_path = "s3://silver/auditoria_municipal/" | ||
partition="AnoExercicio" | ||
file_type='csv' | ||
|
||
audit_schema=StructType([ | ||
|
||
StructField('CodigoMunicipio',IntegerType(), True) | ||
,StructField('NomeMunicipio',StringType(), True) | ||
,StructField('CodigoTipoOrgao',IntegerType(), True) | ||
,StructField('NomeTipoOrgao',StringType(), True) | ||
,StructField('AnoExercicio',IntegerType(), True) | ||
,StructField('SequenciaOrgao',IntegerType(), True) | ||
,StructField('NomeOrgao',StringType(), True) | ||
,StructField('CodigoAutoridade',IntegerType(), True) | ||
,StructField('Trata',StringType(), True) | ||
,StructField('mentoAutoridade',StringType(), True) | ||
,StructField('CargoAutoridade',StringType(), True) | ||
,StructField('SequenciaAutoridade',IntegerType(), True) | ||
,StructField('Nome',StringType(), True) | ||
,StructField('Sexo',StringType(), True) | ||
,StructField('InicioMandato',DateType(), True) | ||
,StructField('FimMandato',DateType(), True) | ||
]) | ||
|
||
|
||
if __name__ == '__main__': | ||
bronze_to_silver(bronze_audit_path, silver_audit_path, audit_schema, partition, file_type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use o factory pattern para lidar com tipos de leituras diferentes:
https://dagster.io/blog/python-factory-patterns