Spark SQL更改编号的格式 [英] Spark SQL change format of the number

查看:627
本文介绍了Spark SQL更改编号的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

show命令后,spark将显示以下内容:

After show command spark prints the following:

+-----------------------+---------------------------+
|NameColumn             |NumberColumn               |
+-----------------------+---------------------------+
|name                   |4.3E-5                     |
+-----------------------+---------------------------+

是否可以将NumberColumn格式更改为类似0.000043的格式?

Is there a way to change NumberColumn format to something like 0.000043?

推荐答案

,您可以使用format_number

这里5是要显示的小数位

here 5 is the decimal places you want to show

如您在上面的链接中所见,format_number函数返回 string列

As you can see in the link above that the format_number functions returns a string column

format_number(第x列,整数d)
将数字列x格式化为'#,###,###.##'之类的格式,四舍五入到d小数位,然后将结果作为字符串列返回.

format_number(Column x, int d)
Formats numeric column x to a format like '#,###,###.##', rounded to d decimal places, and returns the result as a string column.

如果不需要,,则可以调用定义为

If your don't require , you can call regexp_replace function which is defined as

regexp_replace(列e,字符串模式,字符串替换)
将与regexp匹配的指定字符串值的所有子字符串替换为rep.

regexp_replace(Column e, String pattern, String replacement)
Replace all substrings of the specified string value that match regexp with rep.

并将其用作

import org.apache.spark.sql.functions.regexp_replace
df.withColumn("NumberColumn", regexp_replace(format_number($"NumberColumn", 5), ",", ""))

因此,逗号 (,)应该被删除.

Thus comma (,) should be removed for large numbers.

这篇关于Spark SQL更改编号的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆