I'm trying to create a dataframe from a directory full of csv files but I want to keep the filename of each file on the dataframe as an additional column, is that possible on pyspark without using pandas?, also I want to remove the path from the filename.
from pyspark.sql.functions import input_file_name
df = spark.read.option("delimiter", "\t").csv(mount_point_input)
df_.withColumn("filename", input_file_name())
I tried using input_file_name() but all rows on the dataframe got the same filename.
Input:
False 2021-06-05T14:45:09 Server True
True 2021-06-02T21:32:42 Server True
Output:
+-----+-----------------------+-------+-------+--------------------------------+
False 2021-06-05T14:45:09 Server True /2021-06-02-general/c32d3f47.csv
+-----+-----------------------+-------+-------+--------------------------------+
False 2021-06-02T21:32:42 Server True /2021-06-02-general/c32d3f47.csv
+-----+-----------------------+-------+-------+--------------------------------+
Expected output:
+-----+-----------------------+-------+-------+--------------------------------+
False 2021-06-05T14:45:09 Server True c32d3f47.csv
+-----+-----------------------+-------+-------+--------------------------------+
False 2021-06-02T21:32:42 Server True c32d3f48.csv
+-----+-----------------------+-------+-------+--------------------------------+