Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a column in Databricks

How do you rename a column in Databricks?

The following does not work:

ALTER TABLE mySchema.myTable change COLUMN old_name new_name int

It returns the error:

ALTER TABLE CHANGE COLUMN is not supported for changing column 'old_name' with type 'IntegerType >(nullable = true)' to 'new_name' with type 'IntegerType (nullable = true)';

If it makes a difference, this table is using Delta Lake, and it is NOT partitioned or z-ordered by this "old_name" column.

like image 322
David Maddox Avatar asked Dec 26 '19 17:12

David Maddox


People also ask

How do I rename a column in spark DataFrame?

Spark has a withColumnRenamed() function on DataFrame to change a column name. This is the most straight forward approach; this function takes two parameters; the first is your existing column name and the second is the new column name you wish for. Returns a new DataFrame (Dataset[Row]) with a column renamed.

How do I rename an existing column?

1. Renaming a column name using the ALTER keyword. Line 2: RENAME COLUMN OldColumnName TO NewColumnName; For Example: Write a query to rename the column name “SID” to “StudentsID”.

How do I rename a view in Databricks?

RENAME TO to_view_name Renames the existing view within the schema. to_view_name specifies the new name of the view. If the to_view_name already exists, a TableAlreadyExistsException is thrown. If to_view_name is qualified it must match the schema name of view_name .


1 Answers

You can't rename or change a column datatype in Databricks, only add new columns, reorder them or add column comments. To do this you must rewrite the table using the overwriteSchema option.

Take this example below from this documentation:

spark.read.table(...)
  .withColumnRenamed("date", "date_created")
  .write
  .mode("overwrite")
  .option("overwriteSchema", "true")
  .table(...)
like image 69
LeandroHumb Avatar answered Oct 19 '22 02:10

LeandroHumb