0

I am new to spark, struck here by trying to replace values of string with new values.

Data in URL="a=1,
b=2,
c=3"
var header="a,b,c"
for (line <- Source.fromURL("/u/r/l").getLines) {
     if(header.contains(line.split("=")(0).toLowerCase().trim)) {
       header.replaceAll(line.split("=")(0).toLowerCase().trim,line.split("=")(1).toLowerCase().trim)
     }
      }

I am expecting my final output as

println(header)
1,2,3

How can I achieve this, when I run this, still getting "a,b,c"

1 Answer 1

1

all you need is assignment as

for (line <- Source.fromURL("/u/r/l").getLines) {
     if(header.contains(line.split("=")(0).toLowerCase().trim)) {
       header = header.replaceAll(line.split("=")(0).toLowerCase().trim,line.split("=")(1).toLowerCase().trim)
     }
}

You can do it more functionally as

Source.fromURL("/u/r/l").getLines.map(_.split("=")).map(line => header = header.replaceAll(line(0).toLowerCase().trim,line(1).toLowerCase().trim))

going one step further you can change the url to Map and apply the mapping as

val kv = Source.fromURL("/u/r/l").getLines.map(_.split("=")).map(x => (x(0).toLowerCase.trim, x(1).toLowerCase.trim)).toMap
header = header.split(",").map(x => if(kv.keySet.contains(x)) kv(x);else x).mkString(",")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.