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

Exposing Scala DataFrames in PySpark #214

Merged
merged 3 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,29 @@
<artifactId>build-helper-maven-plugin</artifactId>
<version>${build-helper.plugin.version}</version>
</plugin>

<!-- this is to create a zip of PySpark modules -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<configuration>
<descriptors>
<descriptor>src/main/assembly/python.xml</descriptor>
</descriptors>
<finalName>aut</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

Expand Down
13 changes: 13 additions & 0 deletions src/main/assembly/python.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>python</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>src/main/python/aut/</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
5 changes: 5 additions & 0 deletions src/main/python/aut/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from aut.common import WebArchive
from aut.udfs import extract_domain

__all__ = ['WebArchive', 'extract_domain']

15 changes: 15 additions & 0 deletions src/main/python/aut/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from pyspark.sql import DataFrame

class WebArchive:
def __init__(self, sc, sqlContext, path):
self.sc = sc
self.sqlContext = sqlContext
self.loader = sc._jvm.io.archivesunleashed.DataFrameLoader(sc._jsc.sc())
self.path = path

def pages(self):
return DataFrame(self.loader.extractValidPages(self.path), self.sqlContext)

def links(self):
return DataFrame(self.loader.extractHyperlinks(self.path), self.sqlContext)

11 changes: 11 additions & 0 deletions src/main/python/aut/udfs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from pyspark.sql.functions import udf
from pyspark.sql.types import StringType

def extract_domain_func(url):
url = url.replace('http://', '').replace('https://', '')
if '/' in url:
return url.split('/')[0].replace('www.', '')
else:
return url.replace('www.', '')

extract_domain = udf(extract_domain_func, StringType())
16 changes: 16 additions & 0 deletions src/main/scala/io/archivesunleashed/DataFrameLoader.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.archivesunleashed

import org.apache.spark.SparkContext
import org.apache.spark.sql._

class DataFrameLoader(sc: SparkContext) {
def extractValidPages(path: String): DataFrame = {
RecordLoader.loadArchives(path, sc)
.extractValidPagesDF()
}

def extractHyperlinks(path: String): DataFrame = {
RecordLoader.loadArchives(path, sc)
.extractHyperlinksDF()
}
}