0

How to append a column to data frame and insert file name into column? I was able to remove headers and converted to dataframe using the below code:

I have 2 files as below:

file1.csv:

name:file1
dept: hr
id,name,age
1,ss,34
2,rr,35
3,aa,44

file2.csv:

name:file2
dept: hr
id,name,age
1,ps,34
2,er,35
3,qa,44
val ofcFile = sc.wholeTextFiles("file:///root/ofc/dataset").flatMap(_._2.split("\n").drop(3))
case class ofc_str(id : String, name: String, age : String)
val DF = houseFile.map(_.split(",")).map(p => ofc_str(p(0).toString,p(1).toString,p(2).toString)).toDF()
DF.show
+--+----+---+
|id|name|age|
+--+----+---+
|1 | ss | 34|
|2 | rr | 35|
|3 | aa | 44|
|1 | ps | 34|
|2 | er | 35|
|3 | qa | 44|

But, I am not able to identify the records from which file I got it so, how can I get a file name against each record and insert it into a new column filename into DF.

1 Answers1

0

As suggested in comments try:

sc.wholeTextFiles("file:///root/ofc/dataset")
  .flatMapValues(_.split("\n").drop(3))
  .mapValues(p => ofc_str(p(0).toString,p(1).toString,p(2).toString))
  .toDF()
  • @LostlnOverflow thank u for responding.. getting an error...-> 42: error: value _2 is not a member of String please help me to find the solution. – Srinathji Kyadari Aug 11 '16 at 08:26