This repository has been archived by the owner on Apr 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UDF for computing MD5 checksum. Issue #211
- Loading branch information
Showing
9 changed files
with
134 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
25 changes: 25 additions & 0 deletions
25
warcbase-core/src/main/scala/org/warcbase/spark/matchbox/ComputeImageSize.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.warcbase.spark.matchbox | ||
|
||
import java.io.ByteArrayInputStream | ||
import javax.imageio.ImageIO | ||
|
||
/** | ||
* Created by youngbinkim on 7/7/16. | ||
*/ | ||
object ComputeImageSize { | ||
def apply(bytes: Array[Byte]): (Int, Int) = { | ||
val in = new ByteArrayInputStream(bytes) | ||
|
||
try { | ||
val image = ImageIO.read(in) | ||
if (image == null) | ||
return (0, 0) | ||
(image.getWidth(), image.getHeight()) | ||
} catch { | ||
case e: Throwable => { | ||
e.printStackTrace() | ||
return (0, 0) | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
warcbase-core/src/main/scala/org/warcbase/spark/matchbox/ComputeMD5.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.warcbase.spark.matchbox | ||
|
||
import java.security.MessageDigest | ||
|
||
|
||
/** | ||
* compute MD5 checksum.. | ||
* | ||
*/ | ||
object ComputeMD5 { | ||
/** | ||
* | ||
* @param bytes | ||
* @return | ||
*/ | ||
def apply(bytes: Array[Byte]): String = { | ||
new String(MessageDigest.getInstance("MD5").digest(bytes)) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
warcbase-core/src/main/scala/org/warcbase/spark/matchbox/ExtractPopularImages.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.warcbase.spark.matchbox | ||
|
||
import org.warcbase.spark.rdd.RecordRDD._ | ||
import org.apache.spark.rdd.RDD | ||
import org.warcbase.spark.archive.io.ArchiveRecord | ||
|
||
|
||
/** | ||
* Extract most popular images | ||
* | ||
* limit: number of most popular images in the output | ||
* timeoutVal: time allowed to connect to each image | ||
*/ | ||
object ExtractPopularImages { | ||
def apply(records: RDD[ArchiveRecord], limit: Int, minWidth: Int = 30, minHeight: Int = 30) = { | ||
val res = records | ||
.keepImages() | ||
.map(r => ((r.getUrl, r.getImageBytes), 1)) | ||
.map(img => (ComputeMD5(img._1._2), (ComputeImageSize(img._1._2), img._1._1, img._2))) | ||
.filter(img => img._2._1._1 >= minWidth && img._2._1._2 >= minHeight) | ||
.reduceByKey((image1, image2) => (image1._1, image1._2, image1._3 + image2._3)) | ||
.takeOrdered(limit)(Ordering[Int].on(x => -x._2._3)) | ||
res.foreach(x => println(x._2._2 + "\t" + x._2._3)) | ||
res | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
warcbase-core/src/main/scala/org/warcbase/spark/matchbox/RemoveHttpHeader.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.warcbase.spark.matchbox | ||
|
||
/** | ||
* Created by youngbinkim on 7/9/16. | ||
*/ | ||
object RemoveHttpHeader { | ||
val headerEnd = "\r\n\r\n" | ||
def apply(content: String): String = { | ||
try { | ||
if (content.startsWith("HTTP/")) | ||
content.substring(content.indexOf(headerEnd) + headerEnd.length) | ||
else | ||
content | ||
} catch { | ||
case e: Exception => { | ||
println(e) | ||
null | ||
} | ||
} | ||
} | ||
} |
This file contains 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