简体   繁体   English

如何使用Java在Spark中将数据库的列名更改为大写

[英]How to change column name of database to upper case in Spark using Java

I have some column names in mixed cases in my Dataframe like sum(TXN_VOL) I want to convert them to uppercase like SUM(TXN_VOL) 我在Dataframe中有一些混合情况下的列名,例如sum(TXN_VOL)我想将它们转换为大写,例如SUM(TXN_VOL)

I won't be knowing all the column names so I cant convert them using hard coding. 我不会知道所有列名,因此无法使用硬编码转换它们。

Either I have to iterate through all column names and convert each of them to UPPER CASE. 要么必须遍历所有列名,然后将每个列名都转换为大写。 OR there is any built in functionality to change all column names to UPPER CASE 有任何内置功能可将所有列名更改为大写

What I tried is : 我试过的是:

String[] columnNames = finalBcDF.columns();
                    Dataset<Row> x = null;
                    for(String columnName : columnNames) {
                    x = finalBcDF.withColumnRenamed(columnName, columnName.toUpperCase());
                }

But this will create new Dataframe each time so, This won't give desired result. 但这每次都会创建一个新的Dataframe,这将不会产生期望的结果。

I have checked on many site but I am not able to see how can I do so in Java. 我已经检查了许多站点,但是看不到如何用Java进行检查。

Can anyone help here? 有人可以帮忙吗?

EDIT 编辑

In one of the answers : 在答案之一中:

How to lower the case of column names of a data frame but not its values? 如何减少数据框的列名而不是其值的大小写?

answer is given for Scala and PySpark but I am not able to convert it to Java, can anyone help? 给出了Scala和PySpark的答案,但是我无法将其转换为Java,有人可以帮忙吗?

Here is how you can convert the column names to upper case using Java 8 . 这是使用Java 8将列名转换为大写字母的方法。

import static org.apache.spark.sql.functions.col;
import org.apache.spark.sql.Column;

df.select(Arrays.asList(df.columns()).stream().map(x -> col(x).as(x.toUpperCase())).toArray(size -> new Column[size])).show(false);

Iterating would be good to go approach. 迭代将是很好的方法。 Even though new DataFrame java class instance is created. 即使创建了新的DataFrame java类实例。 Since spark evaluated lazily so there will be no performance penalty. 由于spark的评估比较延迟,因此不会有性能损失。

Reference: https://data-flair.training/blogs/apache-spark-lazy-evaluation/ 参考: https : //data-flair.training/blogs/apache-spark-lazy-evaluation/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM