After reading textFile How to create RDD to DF with out creating Case class


After reading texFile , how can we convert RDD to DF without creating case class

val textData=sc.textFile(“read orders textFile location”)>x.split("|)).map(rec.orders(rec(0).toInt,rec(1).toInt,rec(2).toString,rec(3).toString)).toDF(“orderid”,“order_amt”,“order_status”,“order_date”);

Can any one pls suggest the correct way ,


Hi @ethabhi2795,

Check the file path you used. It’s just text.

Please use the code snippet given below to see how to map fields a Data Frame.

val orders = sc.textFile("/public/retail_db/orders") => (x.split(",")(0).toInt,x.split(",")(1),x.split(",")(2).toInt,x.split(",")(3))).toDF(“orderId”,“date”,“customerId”,“status”).show()

If you still have queries Let me know.

result will be like the below image:



@ethabhi2795 If I understand your question correctly, we can’t create the expected layout of DF without a model (Case Class or Singleton Object or SturctFields) else you might ended up with one column or _1, _2, _3 etc).


@email2dgk to create a DF we can create DF without creating Case class like below . this will save time during exam time. => (x.split(",")(0).toInt,x.split(",")(1),x.split(",")(2).toInt,x.split(",")(3))).toDF(“orderId”,“date”,“customerId”,“status”).show()


@ethabhi2795 Agreed if its for exam but => {val z = x.split(","); (z(0).toInt, z(1), z(2).toInt}).toDF(“orderId”,“date”,“customerId”,“status”).show()