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
12 changes: 6 additions & 6 deletions src/main/python/RecordLoader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,47 +20,47 @@
from RecordRDD import RecordRDD

def loadArcAsRDD(path, sc, spark):
rlph = sc._jvm.io.archivesunleashed.pyspark.matchbox.RecordLoaderPythonHelper
rlph = sc._jvm.io.archivesunleashed.spark.pythonhelpers.RecordLoaderPythonHelper
df = rlph.loadArc(path, sc._jsc, spark._jsparkSession)
df.createTempView("df")
pdf = spark.table("df")
spark.catalog.dropTempView("df")
return pdf.rdd

def loadArcAsDF(path, sc, spark):
rlph = sc._jvm.io.archivesunleashed.pyspark.matchbox.RecordLoaderPythonHelper
rlph = sc._jvm.io.archivesunleashed.spark.pythonhelpers.RecordLoaderPythonHelper
df = rlph.loadArc(path, sc._jsc, spark._jsparkSession)
df.createTempView("df")
pdf = spark.table("df")
spark.catalog.dropTempView("df")
return pdf

def loadWarcAsRDD(path, sc, spark):
rlph = sc._jvm.io.archivesunleashed.pyspark.matchbox.RecordLoaderPythonHelper
rlph = sc._jvm.io.archivesunleashed.spark.pythonhelpers.RecordLoaderPythonHelper
df = rlph.loadWarc(path, sc._jsc, spark._jsparkSession)
df.createTempView("df")
pdf = spark.table("df")
spark.catalog.dropTempView("df")
return pdf.rdd

def loadWarcAsDF(path, sc, spark):
rlph = sc._jvm.io.archivesunleashed.pyspark.matchbox.RecordLoaderPythonHelper
rlph = sc._jvm.io.archivesunleashed.spark.pythonhelpers.RecordLoaderPythonHelper
df = rlph.loadWarc(path, sc._jsc, spark._jsparkSession)
df.createTempView("df")
pdf = spark.table("df")
spark.catalog.dropTempView("df")
return pdf

def loadArchivesAsDF(path, sc, spark):
rlph = sc._jvm.io.archivesunleashed.pyspark.matchbox.RecordLoaderPythonHelper
rlph = sc._jvm.io.archivesunleashed.spark.pythonhelpers.RecordLoaderPythonHelper
df = rlph.loadArchives(path, sc._jsc, spark._jsparkSession)
df.createTempView("df")
pdf = spark.table("df")
spark.catalog.dropTempView("df")
return pdf

def loadArchivesAsRDD(path, sc, spark):
rlph = sc._jvm.io.archivesunleashed.pyspark.matchbox.RecordLoaderPythonHelper
rlph = sc._jvm.io.archivesunleashed.spark.pythonhelpers.RecordLoaderPythonHelper
df = rlph.loadArchives(path, sc._jsc, spark._jsparkSession)
df.createTempView("df")
pdf = spark.table("df")
Expand Down
37 changes: 37 additions & 0 deletions src/main/python/scripts/extractLinkScript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.

import RecordLoader
from DFTransformations import *
from ExtractDomain import ExtractDomain
from ExtractLinks import ExtractLinks
from pyspark.sql import SparkSession

if __name__ == "__main__":
# replace with your own path to archive file
path = "/Users/Prince/Projects/pyaut/aut/example.arc.gz"

spark = SparkSession.builder.appName("extractLinks").getOrCreate()
sc = spark.sparkContext

rdd = RecordLoader.loadArchivesAsRDD(path, sc, spark)
rdd1 = rdd.flatMap(lambda r: ExtractLinks(r.url, r.contentString))
rdd2 = rdd1.map(lambda r: (ExtractDomain(r[0]), ExtractDomain(r[1])))
rdd3 = rdd2.filter(lambda r: r[0] is not None and r[0]!= "" and r[1] is not None and r[1] != "")
rdd4 = countItems(rdd3).filter(lambda r: r[1] > 5)

print(rdd4.take(10))

spark.stop()
37 changes: 37 additions & 0 deletions src/main/python/scripts/filterByDateScript.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# 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.

import RecordLoader
from DFTransformations import *
from ExtractDomain import ExtractDomain
from ExtractLinks import ExtractLinks
from ExtractDate import DateComponent
from RemoveHTML import RemoveHTML
from pyspark.sql import SparkSession

if __name__ == "__main__":
# replace with your own path to archive file
path = "/Users/Prince/Projects/pyaut/aut/example.arc.gz"
Copy link
Contributor

@greebie greebie Oct 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't remember if there is an equivalent to the Resource Scala library for Python (Resource has a tool to finds paths in the package). It would be better for unit testing / coverage. If not, we could also move this script to aut-docs as we did with the Scala scripts.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the scripts were just to give some examples of how to use AUT in PySpark, and it doesn't really matter where the scripts are located. I just put them under aut for convenience.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I would push this down to the loader. Add an option to keep only valid pages, which is always true by default. So load('path') would be the default form, which is really load('path', true) - the user can suppress with load('path', false) if they really want all the crap...


spark = SparkSession.builder.appName("filterByDate").getOrCreate()
sc = spark.sparkContext

df = RecordLoader.loadArchivesAsDF(path, sc, spark)
filtered_df = keepDate(df, "2008", DateComponent.YYYY).filter(df['url'].like("%archive%"))
rdd = filtered_df.rdd
rdd.map(lambda r: (r.crawlDate, r.domain, r.url, RemoveHTML(r.contentString))) \
.saveAsTextFile("out/")

spark.stop()
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ import io.archivesunleashed.spark.matchbox.RecordLoader
import io.archivesunleashed.spark.rdd.RecordRDD._

object RecordLoaderPythonHelper {
// def loadArc(path: String, jssc: JavaSparkContext): JavaRDD[ArcRecordWritable] = {
// val sc = jssc.sc
// //val rdd = RecordLoader.loadArc(path, sc)
// val rdd = sc.newAPIHadoopFile(path, classOf[WacArcInputFormat], classOf[LongWritable], classOf[ArcRecordWritable])
// .map(r => r._2)
// val jrdd = new JavaRDD(rdd)
// jrdd
// }

def loadArc(path: String, jssc: JavaSparkContext, spark: SparkSession): DataFrame = {
val sc = jssc.sc
Expand All @@ -50,7 +42,4 @@ object RecordLoaderPythonHelper {
sc.textFile(path).filter(line => !line.startsWith("{\"delete\":"))
.map(line => try { parse(line) } catch { case e: Exception => null }).filter(x => x != null)

def add(a: Int, b: Int): Int = a + b

def square(a : List[Int]): List[Int] = a.map(x => x*x)
}