abhishek kurasala
abhishek kurasala

Reputation: 305

Is there a way to get column names using hiveContext?

I have an "iplRDD" which is a json, and I do below steps and query through hivecontext. I get the results but without columns headers. Is there is a way to get the columns names along with the values?

val teamRDD = hiveContext.jsonRDD(iplRDD) 
teamRDD.registerTempTable("teams") 
hiveContext.cacheTable("teams") 

val result = hiveContext.sql("select * from teams where  team_name = "KKR" ) 
result.collect.foreach(println) 

Any thoughts please ?

Upvotes: 4

Views: 7568

Answers (3)

Ravi
Ravi

Reputation: 11

you can save your dataframe 'result' like this with header as csv file:

result.write().format("com.databricks.spark.csv").option("header", "true").save(outputPath);

Upvotes: 1

Mayur Maheshwari
Mayur Maheshwari

Reputation: 170

You can get it by using:

result.schema().fields();

Upvotes: 1

Marius Soutier
Marius Soutier

Reputation: 11274

teamRDD.schema.fieldNames should contain the header names.

Upvotes: 2

Related Questions