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.
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.
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.
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With