Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 我空着回来了_Scala_Apache Spark_Sha256 - Fatal编程技术网

Scala 我空着回来了

Scala 我空着回来了,scala,apache-spark,sha256,Scala,Apache Spark,Sha256,我试图为数据帧中的每一行创建一个SHA256哈希 import org.apache.spark.sql.functions.{col, concat, sha2} val finalResultWithHash = finalResult.withColumn("ROWHASH", sha2(concat(finalResult.columns.map(col):_*), 256)) 当我在数据框中只有一列时,它似乎正在工作 在后面的代码中,我将数据帧写为CSV,并且rowhash列为空。

我试图为数据帧中的每一行创建一个SHA256哈希

import org.apache.spark.sql.functions.{col, concat, sha2}
val finalResultWithHash = finalResult.withColumn("ROWHASH", sha2(concat(finalResult.columns.map(col):_*), 256))
当我在数据框中只有一列时,它似乎正在工作

在后面的代码中,我将数据帧写为CSV,并且rowhash列为空。 我还没有找到任何关于我做错了什么的文档


提前谢谢。

出于某种原因,下面的代码适用于我的多列

val finalResultWithHash = personDF.withColumn("ROWHASH", sha2(concat(personDF.columns.map(col): _*), 256))

+-----+-----+---+------+--------------------+
|FName|LName|Age|Gender|             ROWHASH|
+-----+-----+---+------+--------------------+
|    A|    B| 29|     M|c4ae6946a295e9d74...|
|    A|    C| 12|      |89a18fdc3ddb3c2fd...|
|    B|    D| 35|     F|ef1c89dfc765c7e1e...|
|    Q|    D| 85|      |cd91aa387a7e6a180...|
|    W|    R| 14|      |e9ff9bb78fd93a13a...|
+-----+-----+---+------+--------------------+


可能只是支架放置错误…

另一种方法是使用
foldLeft()

在散列之前,折叠将从左到右覆盖所有列:

df.withColumn("rowsha",sha2(df.columns.foldLeft(lit(""))((x,y)=>concat(x,col(y))),256)).explain()
== Physical Plan ==
*(1) Project [c1#10, c2#11, c3#12, c4#13, sha2(cast(concat(, c1#10, c2#11, c3#12, 4#13) as binary), 256) AS rowsha#165]
+- *(1) ...
但是,如果串联中的任何列包含NULL,则结果也将为NULL。为了防止出现这种情况,您可能需要使用

val df2 = df.withColumn("rowsha",sha2(df.columns.foldLeft(lit(""))((x,y)=>concat(x,coalesce(col(y),lit("n/a"))),256))

对不起,我在写问题的时候把那个括号完全漏掉了。。。我的代码中确实有这样的代码,但是奇怪的是,我得到了一个错误,而你得到了结果。。。也许我有太多的列或某种行长度问题,即使使用这种解决方案,我仍然没有在数据帧中得到实际的哈希。我开始相信,对于sha2函数来说,数据点放在一起太长了。您的任何列是否碰巧包含空值?如果是这种情况,您应该将
concat(x,col(y))
更改为类似
concat(x,coalesce(col(y),lit(“n/a”))
。这很有效。。。空值是问题所在。非常感谢。
val df2 = df.withColumn("rowsha",sha2(df.columns.foldLeft(lit(""))((x,y)=>concat(x,coalesce(col(y),lit("n/a"))),256))