Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spark DataFrame and renaming multiple columns (Java)

Is there any nicer way to prefix or rename all or multiple columns at the same time of a given SparkSQL DataFrame than calling multiple times dataFrame.withColumnRenamed()?

An example would be if I want to detect changes (using full outer join). Then I'm left with two DataFrames with the same structure.

like image 669
JiriS Avatar asked Oct 08 '15 12:10

JiriS


2 Answers

I suggest to use the select() method to perform this. In fact withColumnRenamed() method uses select() by itself. Here is example how to rename multiple columns:

import org.apache.spark.sql.functions._

val someDataframe: DataFrame = ...

val initialColumnNames = Seq("a", "b", "c")
val renamedColumns = initialColumnNames.map(name => col(name).as(s"renamed_$name"))
someDataframe.select(renamedColumns : _*)
like image 115
Zyoma Avatar answered Oct 15 '22 23:10

Zyoma


I think this method can help you.

public static Dataset<Row> renameDataFrame(Dataset<Row> dataset) {
    for (String column : dataset.columns()) {
        dataset = dataset.withColumnRenamed(column, SystemUtils.underscoreToCamelCase(column));
    }
    return dataset;
}

    public static String underscoreToCamelCase(String underscoreName) {
        StringBuilder result = new StringBuilder();
        if (underscoreName != null && underscoreName.length() > 0) {
            boolean flag = false;
            for (int i = 0; i < underscoreName.length(); i++) {
                char ch = underscoreName.charAt(i);
                if ("_".charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();
    }



like image 22
Alsace Avatar answered Oct 15 '22 23:10

Alsace