Spark Scala CSV Column names to Lower Case

Viewed 5602

Please find the code below and Let me know how I can change the Column Names to Lower case. I tried withColumnRename but I have to do it for each column and type all the column names. I just want to do it on columns so I don't want to mention all the column names as there are too many of them.

Scala Version: 2.11 Spark : 2.2

import org.apache.spark.sql.SparkSession
import org.apache.log4j.{Level, Logger}
import com.datastax


import org.apache.spark.SparkContext
import org.apache.spark.SparkConf
import com.datastax.spark.connector._
import org.apache.spark.sql._

object dataframeset {

  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName("Sample1").setMaster("local[*]")
    val sc = new SparkContext(conf)
    sc.setLogLevel("ERROR")
    val rdd1 = sc.cassandraTable("tdata", "map3")
    Logger.getLogger("org").setLevel(Level.ERROR)
    Logger.getLogger("akka").setLevel(Level.ERROR)
    val spark1 = org.apache.spark.sql.SparkSession.builder().master("local").config("spark.cassandra.connection.host","127.0.0.1")
      .appName("Spark SQL basic example").getOrCreate()

    val df = spark1.read.format("csv").option("header","true").option("inferschema", "true").load("/Users/Desktop/del2.csv")
    import spark1.implicits._
    println("\nTop Records are:")
    df.show(1)


    val dfprev1 = df.select(col = "sno", "year", "StateAbbr")

    dfprev1.show(1)
}
}

Required output:

|sno|year|stateabbr|    statedesc|cityname|geographiclevel

All the Columns names should be in lower case. 

Actual output:

Top Records are:
+---+----+---------+-------------+--------+---------------+----------+----------+--------+--------------------+---------------+---------------+--------------------+----------+--------------------+---------------------+--------------------------+-------------------+---------------+-----------+----------+---------+--------+---------+-------------------+
|sno|year|StateAbbr|    StateDesc|CityName|GeographicLevel|DataSource|  category|UniqueID|             Measure|Data_Value_Unit|DataValueTypeID|     Data_Value_Type|Data_Value|Low_Confidence_Limit|High_Confidence_Limit|Data_Value_Footnote_Symbol|Data_Value_Footnote|PopulationCount|GeoLocation|categoryID|MeasureId|cityFIPS|TractFIPS|Short_Question_Text|
+---+----+---------+-------------+--------+---------------+----------+----------+--------+--------------------+---------------+---------------+--------------------+----------+--------------------+---------------------+--------------------------+-------------------+---------------+-----------+----------+---------+--------+---------+-------------------+
|  1|2014|       US|United States|    null|             US|     BRFSS|Prevention|      59|Current lack of h...|              %|      AgeAdjPrv|Age-adjusted prev...|      14.9|                14.6|                 15.2|                      null|               null|      308745538|       null|   PREVENT|  ACCESS2|    null|     null|   Health Insurance|
+---+----+---------+-------------+--------+---------------+----------+----------+--------+--------------------+---------------+---------------+--------------------+----------+--------------------+---------------------+--------------------------+-------------------+---------------+-----------+----------+---------+--------+---------+-------------------+
only showing top 1 row

+---+----+---------+
|sno|year|StateAbbr|
+---+----+---------+
|  1|2014|       US|
+---+----+---------+
only showing top 1 row
2 Answers

Just use toDF:

df.toDF(df.columns map(_.toLowerCase): _*)
Related