earl
earl

Reputation: 768

Create SOAP XML REQUEST from selected dataframe columns in Scala

Is there a way to create an XML SOAP REQUEST by extracting a few columns from each row of a dataframe ? 10 records in a dataframe means 10 separate SOAP XML REQUESTs.

How would you make the function call using map now?

Upvotes: 2

Views: 745

Answers (1)

Amit
Amit

Reputation: 1121

You can do that by applying a map function to the dataframe.

val df = your dataframe
df.map(x => convertToSOAP(x))

// convertToSOAP is your function.

Putting up an example based on your comment, hope you find this useful.

case class emp(id:String,name:String,city:String)
val list = List(emp("1","user1","NY"),emp("2","user2","SFO"))
val rdd = sc.parallelize(list)
val df = rdd.toDF
df.map(x => "<root><name>" + x.getString(1) + "</name><city>"+ x.getString(2)  +"</city></root>").show(false)

// Note: x is a type of org.apache.spark.sql.Row

Output will be as follows :

+--------------------------------------------------+
|value                                             |
+--------------------------------------------------+
|<root><name>user1</name><city>NY</city></root>    |
|<root><name>user2</name><city>SFO</city></root>   |
+--------------------------------------------------+

Upvotes: 3

Related Questions