如何在Spark SQL中格式化整数?

huangapple go评论81阅读模式
英文:

How to format integer in Spark SQL?

问题

我对Spark还不熟悉。任何帮助将不胜感激。在Spark SQL中是否有sql的FORMAT()的替代方法。我的核心逻辑是用SQL编写的,并且通过spark.sql("query")运行。我有一个将ID转换为4位数的要求。

例如,如果ID是1,应转换为0001;如果是12,应为0012。我知道在SQL中,我们可以使用FORMAT("%04d", id)来实现。但在Spark SQL中,这给我带来错误,提示FORMAT未注册为函数。在Spark的文档中找到了format_numberformat_string,但对我的情况没有帮助。

注意:我不想在Java代码中执行此操作,而是希望在SQL查询中执行。

英文:

I am new to Spark. Any help will be appreciated. Is there any alternative for sql's FORMAT() in Spark SQL. My core logic is written as SQL and running with spark.sql("query"). I have a requirement to convert the id to 4 digit.

For example, if if is 1, it should be converted to 0001, if it is 12, then 0012. I know in SQL, we can do it as FORMAT("%04d", id) as id. But this is giving me error in Spark SQL saying FORMAT is not a function registered. Found format_number and format_string in Spark's documentation, but not helping in my case.

Note: I don't want to do this in my java code, but would like to do in SQL query itself.

答案1

得分: 1

使用 lpad 函数。

spark.sql("SELECT lpad('1', 4, '0')").show

+-------------+
|lpad(1, 4, 0)|
+-------------+
|         0001|
+-------------+

你可以将 '1' 更改为 id

英文:

Use lpad function.

spark.sql("SELECT lpad('1', 4, '0')").show

+-------------+
|lpad(1, 4, 0)|
+-------------+
|         0001|
+-------------+

You can change '1' to id.

huangapple
  • 本文由 发表于 2020年8月19日 13:07:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63480329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定