Last active
June 29, 2018 01:23
-
-
Save smdmts/1fd6492a092126d3b8712239910691ef to your computer and use it in GitHub Desktop.
Spark2.0 : how to add hash row
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.apache.spark.sql.types._ | |
| import java.security._ | |
| val df = sc.parallelize(Seq( | |
| (1.0, 2.0), (0.0, -1.0), | |
| (3.0, 4.0), (6.0, -2.3))).toDF("x", "y") | |
| def mkMD5(text:String) = MessageDigest.getInstance("MD5").digest(text.getBytes).map("%02x".format(_)).mkString | |
| def transformRow(row: Row): Row = Row.fromSeq(row.toSeq ++ Array[String](mkMD5(row.mkString(",")))) | |
| def transformRows(iter: Iterator[Row]): Iterator[Row] = iter.map(transformRow) | |
| val newSchema = StructType(df.schema.fields ++ Array(StructField("row_hash", StringType, false))) | |
| sqlContext.createDataFrame(df.rdd.mapPartitions(transformRows), newSchema).show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.