英文:
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_number
和format_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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论