Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.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 如何为RDD/Dataframe中的整行创建sha1哈希_Scala_Apache Spark - Fatal编程技术网

Scala 如何为RDD/Dataframe中的整行创建sha1哈希

Scala 如何为RDD/Dataframe中的整行创建sha1哈希,scala,apache-spark,Scala,Apache Spark,我有一个带有一个模式的数据框架。现有数据帧已经有50列。现在我想在现有数据帧中添加一个新列。新列名为“hashing_id”,此hashing_id的逻辑为sha1(行)。我怎样才能做到这一点 我尝试了下面的代码。下面这两个方法位于主类使用的trait内部。此特性还扩展了可序列化 def addHashingKey():DataFrame={ val sha1 = java.security.MessageDigest.getInstance("SHA-1") val enCoder = ne

我有一个带有一个模式的数据框架。现有数据帧已经有50列。现在我想在现有数据帧中添加一个新列。新列名为“hashing_id”,此hashing_id的逻辑为sha1(行)。我怎样才能做到这一点

我尝试了下面的代码。下面这两个方法位于主类使用的trait内部。此特性还扩展了可序列化

 def addHashingKey():DataFrame={
val sha1 = java.security.MessageDigest.getInstance("SHA-1")
val enCoder = new sun.misc.BASE64Encoder()
//enCoder.encode(sha1.digest(row.mkString.getBytes))
createDataFrame(df.map(row => {
        Row.fromSeq(row.toSeq ++ enCoder.encode(sha1.digest(row.mkString.getBytes)))
}), df.schema.add("hashing_id", StringType))

 }


def createDataFrame(rdd: RDD[Row], schema: StructType): DataFrame = {
sqlContext.createDataFrame(rdd, schema)
}
如何使用rdd实现sha1

有人能帮我吗

当我运行代码时,它抛出下面的异常

 17/09/12 13:45:20 ERROR yarn.ApplicationMaster: User class threw exception: org.apache.spark.SparkException: Task not serializable
 org.apache.spark.SparkException: Task not serializable

 Caused by: java.io.NotSerializableException: sun.misc.BASE64Encoder
 Serialization stack:
 - object not serializable (class: sun.misc.BASE64Encoder, value:   sun.misc.BASE64Encoder@46c0813)

你不能试试这样的东西吗?在我刚刚运行的一些测试中,它似乎对我有效:

 val newDF = sqlContext.createDataFrame(
rdd.map(x => Row(x.toSeq ++ Seq(x.toSeq.hashCode()): _*)), StructType(schema.iterator.toSeq ++ Seq(StructField("hashing_id", StringType, true))))
显然,您需要为所需的哈希函数替换哈希代码

编辑:使用sha1函数

在另一个类中定义函数

object Encoder {
  def sha1(s: Row): String = MessageDigest.getInstance("SHA-1").digest(s.mkString.getBytes()).toString
}
然后,在原始类中,可以按如下方式调用函数

   val newDF = sqlContext.createDataFrame(wordsRDD.map(x => Row(x.toSeq ++ Seq(Encoder.sha1(x)): _*)), StructType(schema.iterator.toSeq ++ Seq(StructField("hashing_id", StringType, true)))).rdd.collect()

我尝试添加sha1哈希,它抛出java.io.NotSerializableException您需要将函数移动到顶级类。我将用一个例子更新我的答案