I have '|' delimited huge text files, I want to merge all the text files and create one huge spark dataframe, it will be later used for ETL process, using pyspark.
Inefficient way
1) Create an empty spark dataframe, df
2) In a loop,read the text file as to spark dataframe df1 and appending it to empty spark dataframe df
df = spark.createDataFrame([],schema)
for x in os.listdir(textfiles_dir):
filepath = '{}/{}'.format(textfiles_dir,x)
df1 = spark.read.format("csv") \
.option("header", "true") \
.option("delimiter", "|") \
.option("inferSchema","true") \
.load(filepath)
df = df.union(df1)
This is not an efficient spark way.
Can anyone suggest an efficient way of doing this? That would be great if explained with sample code.
Thanks :)