简体   繁体   English

spark sql concat 和 cast 操作导致列的空值

[英]spark sql concat and cast operations resulting null values for column

I am doing concat and cast operation inside spark SQL query as follows:我在 spark SQL 查询中执行concatcast操作,如下所示:

spark.sql ("select cast(concat(df_view.col1,"  ") as Long) as new_col from df_view")

But I am getting null values in resulting DF.但是我在结果 DF 中得到了null值。 If I just perform the cast or concat operation, I am getting the correct results, but with both operations simultaneously, I get the null values.如果我只是执行 cast 或 concat 操作,我会得到正确的结果,但是同时进行这两个操作时,我会得到null值。

Please suggest if I'm missing something in the syntax, I checked other answers but couldn't figure out the issue, also I am using only spark SQL here not DF syntax operations.请建议我是否在语法中遗漏了某些内容,我检查了其他答案但无法弄清楚问题,而且我在这里只使用 spark SQL 而不是 DF 语法操作。

If you are writing the file as text then just don't cast it to Long, and preferably use pad functions to make sure you are writing the right width.如果您将文件作为文本写入,则不要将其强制转换为 Long,最好使用填充功能来确保您写入的宽度正确。

I take from the comments that your issue is fixed-width files but as a general thing it makes no sense to concat an empty space and then try to cast the result as a number.我从评论中了解到您的问题是固定宽度的文件,但作为一般的事情,连接一个空白空间然后尝试将结果转换为数字是没有意义的。 You've explicitly made it not a number before.您之前已经明确表示它不是数字。

Ideally you deal with the file format as a file format and not by arbitrarily manipulating each field, however the latter can work if you handle each field correctly.理想情况下,您将文件格式作为文件格式处理,而不是通过任意操作每个字段,但是如果您正确处理每个字段,后者可以工作。

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

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