In this tutorial, you will learn "How to Change column names from CamelCase to Snake_Case by using Scala" in Databricks.
💡Imagine we have an input Dataframe (as in the image). Our goal is to achieve the desired output Dataframe (also in the image).
Basically, you have to change the names of column as follows-
Age -> Age , FirstName -> First_Name,
CityName -> City_Name,
CountryName -> Country_Name
To create a Dataframe in Scala, you can use Apache Spark's Dataframe API.
In this example:
💎Import necessary Spark classes for Dataframe operations.
💎Create a SparkSession which is the entry point to Spark SQL functionality.
💎Define a schema for our Dataframe using StructType and StructField.
💎Define the data as a sequence of rows, where each row represents a record in the Dataframe.
💎Create the Dataframe using createDataFrame method of SparkSession, passing in the data and schema.
💎Display the Dataframe using show() method.
💎Create Variable to store Regex Pattern
💎Create Variable to store new Snake Case columns
💎Create new Dataframe with Snake Case Columns
💎Finally, display the data from the Dataframe