Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PySpark support for core AUT functionality. #12, #13. #100

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4b46c1f
copy over scala code to new directory for pyspark
Sep 28, 2017
23129e8
Merge remote-tracking branch 'upstream/master'
Oct 3, 2017
2ea2d69
add pyspark files
Oct 18, 2017
d1b2659
add spark-sql_2.11 dependency to pom.xml
Oct 18, 2017
6d940ce
Merge remote-tracking branch 'upstream/master'
Oct 18, 2017
f871b40
delete aut/python directory
Oct 20, 2017
7d1be99
add python files
Oct 20, 2017
8e2e436
remove extraneous pyspark directory
Oct 20, 2017
a40d93e
add RecordLoaderPythonHelper.scala
Oct 20, 2017
1133c9f
Merge remote-tracking branch 'upstream/master'
Oct 20, 2017
e1b1ad4
remove runpyspark script
Oct 20, 2017
bfb2678
fix some typos and add two example scripts
Oct 20, 2017
c8a29f8
delete some commented out code
Oct 22, 2017
b8629ea
remove unneeded TODOs
Oct 22, 2017
1845d34
remove python scripts
Oct 23, 2017
a6de005
Merge remote-tracking branch 'upstream/master'
Oct 30, 2017
ed1bd88
remove loadArc and loadWarc
Oct 30, 2017
79e7475
Removes old RecordRDD call
ianmilligan1 Nov 20, 2017
7118169
Updated urlparse call for Python 3 support
ianmilligan1 Nov 21, 2017
e292af2
Merge branch 'master' into master
greebie Nov 23, 2017
bafa23f
- Remove errant type reference in DetectLanguage
greebie Nov 23, 2017
8c5fb17
Merge branch 'master' into master
greebie Nov 23, 2017
9731e25
Fix bug with keepContent using re.search instead re.match.
greebie Nov 23, 2017
f9fa7ab
Merge branch 'master' of https://github.com/MapleOx/aut
greebie Nov 23, 2017
9e23524
Swapped match for search in DFTransformations.py
ianmilligan1 Nov 24, 2017
d3859c2
Include git search instead of replace on RowTransformations as well.
greebie Nov 24, 2017
58fb708
removed _java2py import
ianmilligan1 Nov 28, 2017
c569d6a
Merge branch 'master' into master
greebie Nov 30, 2017
85fade6
Merge remote-tracking branch 'upstream/master'
ianmilligan1 Dec 5, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
copy over scala code to new directory for pyspark
  • Loading branch information
Billy Jin authored and Billy Jin committed Sep 28, 2017
commit 4b46c1f82d31a3158de78f5bcfd80da7945e8403
48 changes: 48 additions & 0 deletions src/main/pyspark/archive/io/ArcRecord.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Archives Unleashed Toolkit (AUT):
* An open-source platform for analyzing web archives.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.archivesunleashed.pyspark.archive.io

import org.apache.spark.SerializableWritable
import io.archivesunleashed.data.ArcRecordUtils
import io.archivesunleashed.io.ArcRecordWritable
import io.archivesunleashed.spark.matchbox.ExtractDate.DateComponent
import io.archivesunleashed.spark.matchbox.{RemoveHttpHeader, ExtractDate, ExtractDomain}

class ArcRecord(r: SerializableWritable[ArcRecordWritable]) extends ArchiveRecord {
val getCrawlDate: String = ExtractDate(r.t.getRecord.getMetaData.getDate, DateComponent.YYYYMMDD)

val getCrawlMonth: String = ExtractDate(r.t.getRecord.getMetaData.getDate, DateComponent.YYYYMM)

val getMimeType: String = r.t.getRecord.getMetaData.getMimetype

val getUrl: String = r.t.getRecord.getMetaData.getUrl

val getDomain: String = ExtractDomain(r.t.getRecord.getMetaData.getUrl)

val getContentBytes: Array[Byte] = ArcRecordUtils.getBodyContent(r.t.getRecord)

val getContentString: String = new String(getContentBytes)

val getImageBytes: Array[Byte] = {
if (getContentString.startsWith("HTTP/"))
getContentBytes.slice(
getContentString.indexOf(RemoveHttpHeader.headerEnd)
+ RemoveHttpHeader.headerEnd.length, getContentBytes.length)
else
getContentBytes
}
}
35 changes: 35 additions & 0 deletions src/main/pyspark/archive/io/ArchiveRecord.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Archives Unleashed Toolkit (AUT):
* An open-source platform for analyzing web archives.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.archivesunleashed.pyspark.archive.io

trait ArchiveRecord extends Serializable {
val getCrawlDate: String

val getCrawlMonth: String

val getUrl: String

val getDomain: String

val getMimeType: String

val getContentString: String

val getContentBytes: Array[Byte]

val getImageBytes: Array[Byte]
}
96 changes: 96 additions & 0 deletions src/main/pyspark/archive/io/GenericArchiveRecord.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Archives Unleashed Toolkit (AUT):
* An open-source platform for analyzing web archives.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.archivesunleashed.pyspark.archive.io

import java.text.SimpleDateFormat

import org.apache.spark.SerializableWritable
import org.archive.io.arc.ARCRecord
import org.archive.io.warc.WARCRecord

import org.archive.util.ArchiveUtils
import io.archivesunleashed.data.{ArcRecordUtils, WarcRecordUtils}
import io.archivesunleashed.io.GenericArchiveRecordWritable
import io.archivesunleashed.io.GenericArchiveRecordWritable.ArchiveFormat
import io.archivesunleashed.spark.matchbox.ExtractDate.DateComponent
import io.archivesunleashed.spark.matchbox.{RemoveHttpHeader, ExtractDate, ExtractDomain}

class GenericArchiveRecord(r: SerializableWritable[GenericArchiveRecordWritable]) extends ArchiveRecord {
var arcRecord: ARCRecord = null
var warcRecord: WARCRecord = null

if (r.t.getFormat == ArchiveFormat.ARC)
arcRecord = r.t.getRecord.asInstanceOf[ARCRecord]
else if (r.t.getFormat == ArchiveFormat.WARC)
warcRecord = r.t.getRecord.asInstanceOf[WARCRecord]


val ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX")

val getCrawlDate: String = {
if (r.t.getFormat == ArchiveFormat.ARC) {
ExtractDate(arcRecord.getMetaData.getDate, DateComponent.YYYYMMDD)
} else {
ExtractDate(ArchiveUtils.get14DigitDate(ISO8601.parse(warcRecord.getHeader.getDate)), DateComponent.YYYYMMDD)
}
}

val getCrawlMonth: String = {
if (r.t.getFormat == ArchiveFormat.ARC) {
ExtractDate(arcRecord.getMetaData.getDate, DateComponent.YYYYMM)
} else {
ExtractDate(ArchiveUtils.get14DigitDate(ISO8601.parse(warcRecord.getHeader.getDate)), DateComponent.YYYYMM)
}
}

val getContentBytes: Array[Byte] = {
if (r.t.getFormat == ArchiveFormat.ARC) {
ArcRecordUtils.getBodyContent(arcRecord)
} else {
WarcRecordUtils.getContent(warcRecord)
}
}

val getContentString: String = new String(getContentBytes)

val getMimeType = {
if (r.t.getFormat == ArchiveFormat.ARC) {
arcRecord.getMetaData.getMimetype
} else {
WarcRecordUtils.getWarcResponseMimeType(getContentBytes)
}
}

val getUrl = {
if (r.t.getFormat == ArchiveFormat.ARC) {
arcRecord.getMetaData.getUrl
} else {
warcRecord.getHeader.getUrl
}
}

val getDomain: String = ExtractDomain(getUrl)

val getImageBytes: Array[Byte] = {
if (getContentString.startsWith("HTTP/"))
getContentBytes.slice(
getContentString.indexOf(RemoveHttpHeader.headerEnd)
+ RemoveHttpHeader.headerEnd.length, getContentBytes.length)
else
getContentBytes
}
}
53 changes: 53 additions & 0 deletions src/main/pyspark/archive/io/WarcRecord.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Archives Unleashed Toolkit (AUT):
* An open-source platform for analyzing web archives.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.archivesunleashed.pyspark.archive.io

import java.text.SimpleDateFormat

import org.apache.spark.SerializableWritable
import org.archive.util.ArchiveUtils
import io.archivesunleashed.data.WarcRecordUtils
import io.archivesunleashed.io.WarcRecordWritable
import io.archivesunleashed.spark.matchbox.ExtractDate.DateComponent
import io.archivesunleashed.spark.matchbox.{RemoveHttpHeader, ExtractDate, ExtractDomain}

class WarcRecord(r: SerializableWritable[WarcRecordWritable]) extends ArchiveRecord {
val ISO8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssX")

val getCrawlDate: String = ExtractDate(ArchiveUtils.get14DigitDate(ISO8601.parse(r.t.getRecord.getHeader.getDate)), DateComponent.YYYYMMDD)

val getCrawlMonth: String = ExtractDate(ArchiveUtils.get14DigitDate(ISO8601.parse(r.t.getRecord.getHeader.getDate)), DateComponent.YYYYMM)

val getContentBytes: Array[Byte] = WarcRecordUtils.getContent(r.t.getRecord)

val getContentString: String = new String(getContentBytes)

val getMimeType = WarcRecordUtils.getWarcResponseMimeType(getContentBytes)

val getUrl = r.t.getRecord.getHeader.getUrl

val getDomain = ExtractDomain(getUrl)

val getImageBytes: Array[Byte] = {
if (getContentString.startsWith("HTTP/"))
getContentBytes.slice(
getContentString.indexOf(RemoveHttpHeader.headerEnd)
+ RemoveHttpHeader.headerEnd.length, getContentBytes.length)
else
getContentBytes
}
}
41 changes: 41 additions & 0 deletions src/main/pyspark/matchbox/ComputeImageSize.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Archives Unleashed Toolkit (AUT):
* An open-source platform for analyzing web archives.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.archivesunleashed.pyspark.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)
}
}
}
}
35 changes: 35 additions & 0 deletions src/main/pyspark/matchbox/ComputeMD5.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Archives Unleashed Toolkit (AUT):
* An open-source platform for analyzing web archives.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.archivesunleashed.pyspark.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 src/main/pyspark/matchbox/DetectLanguage.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Archives Unleashed Toolkit (AUT):
* An open-source platform for analyzing web archives.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.archivesunleashed.pyspark.matchbox

import org.apache.tika.language.LanguageIdentifier

object DetectLanguage {
def apply(input: String): String = {
if (input.isEmpty) ""
else new LanguageIdentifier(input).getLanguage
}
}
39 changes: 39 additions & 0 deletions src/main/pyspark/matchbox/DetectMimeTypeTika.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Archives Unleashed Toolkit (AUT):
* An open-source platform for analyzing web archives.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.archivesunleashed.pyspark.matchbox

import java.io.ByteArrayInputStream

import org.apache.tika.Tika
import org.apache.tika.detect.DefaultDetector
import org.apache.tika.parser.AutoDetectParser

/**
* A UDF to detect mime types
*/
object DetectMimeTypeTika {
def apply(content: String): String = {
if (content.isEmpty) "N/A"
else {
val is = new ByteArrayInputStream(content.getBytes)
val detector = new DefaultDetector()
val parser = new AutoDetectParser(detector)
val mimetype = new Tika(detector, parser).detect(is)
mimetype
}
}
}
Loading