Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename columns in spark using @JsonProperty while creating Datasets

Is there way to rename the column names in dataset using Jackson annotations while creating a Dataset?

My encoder class is as follows:

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;
import scala.Serializable;
import javax.persistence.Table;

      @Builder 
      @Data 
      @AllArgsConstructor
      @EqualsAndHashCode 
      @Table(name = "sample_table")
      public class SampleRecord implements Serializable {
         @JsonProperty("sample_id")
         private Long sampleId;
         @JsonProperty("sample_name")
         private String name;
         @JsonProperty("sample_desc")
         private String description; 
      }

My aim is to rename the columns according to the @JsonProperty, so that I can re-use the same class and json functionality.

Please find related versions of modules: - Spark : 2.4.0 (with scala 2.11) - jackson-module-scala_2.11 : 2.9.6

Let me know if you need more information. Help appreciated.

like image 787
Arjav96 Avatar asked Jan 24 '19 17:01

Arjav96


People also ask

How do I rename a column in spark dataset?

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 a column in a data frame?

One way of renaming the columns in a Pandas Dataframe is by using the rename() function. This method is quite useful when we need to rename some selected columns because we need to specify information only for the columns which are to be renamed.


1 Answers


public class SampleRecord implements Serializable {
         private Long sampleId;

         private String name;

         private String description; 


@JsonProperty("sample_id")
public void setSampleId(Long sampleId) {
        this.sampleId = sampleId;
    }

@JsonProperty("sample_name")
public void setName(String name) {
        this.name = name;
    }


@JsonProperty("sample_desc")
public void setDescription(String description) {
        this.description = description;
    }
}
like image 184
YouXiang-Wang Avatar answered Oct 18 '22 04:10

YouXiang-Wang