forked from databricks/learning-spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update a bunch of data loading examples and mention[ish] spark-submit
- Loading branch information
Showing
7 changed files
with
152 additions
and
28 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
31 changes: 31 additions & 0 deletions
31
src/main/scala/com/oreilly/learningsparkexamples/scala/BasicParseWholeFileCsv.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,31 @@ | ||
/** | ||
* Illustrates a simple map partition to parse CSV data in Scala | ||
*/ | ||
package com.oreilly.learningsparkexamples.scala | ||
|
||
import java.io.StringReader | ||
|
||
import org.apache.spark._ | ||
import play.api.libs.json._ | ||
import play.api.libs.functional.syntax._ | ||
import scala.util.parsing.json.JSON | ||
import scala.collection.JavaConversions._ | ||
import au.com.bytecode.opencsv.CSVReader | ||
|
||
object BasicParseWholeFileCsv { | ||
def main(args: Array[String]) { | ||
if (args.length < 2) { | ||
println("Usage: [sparkmaster] [inputfile]") | ||
exit(1) | ||
} | ||
val master = args(0) | ||
val inputFile = args(1) | ||
val sc = new SparkContext(master, "BasicParseWholeFileCsv", System.getenv("SPARK_HOME")) | ||
val input = sc.wholeTextFiles(inputFile) | ||
val result = input.flatMap{ case (_, txt) => | ||
val reader = new CSVReader(new StringReader(txt)); | ||
reader.readAll() | ||
} | ||
println(result.collect().map(_.toList).mkString(",")) | ||
} | ||
} |
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,44 @@ | ||
from pyspark import SparkContext | ||
import csv | ||
import sys | ||
import StringIO | ||
|
||
def loadRecord(line): | ||
"""Parse a CSV line""" | ||
input = StringIO.StringIO(line) | ||
reader = csv.DictReader(input, fieldnames=["name", "favouriteAnimal"]) | ||
return reader.next() | ||
|
||
def loadRecords(fileNameContents): | ||
"""Load all the records in a given file""" | ||
input = StringIO.StringIO(fileNameContents[1]) | ||
reader = csv.DictReader(input, fieldnames=["name", "favouriteAnimal"]) | ||
return reader | ||
|
||
def writeRecords(records): | ||
"""Write out CSV lines""" | ||
output = StringIO.StringIO() | ||
writer = csv.DictWriter(output, fieldnames=["name", "favouriteAnimal"]) | ||
for record in records: | ||
writer.writerow(record) | ||
return [output.getvalue()] | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) != 4: | ||
print "Error usage: LoadCsv [sparkmaster] [inputfile] [outputfile]" | ||
sys.exit(-1) | ||
master = sys.argv[1] | ||
inputFile = sys.argv[2] | ||
outputFile = sys.argv[3] | ||
sc = SparkContext(master, "LoadCsv") | ||
# Try the record-per-line-input | ||
input = sc.textFile(inputFile) | ||
data = input.map(loadRecord) | ||
pandaLovers = data.filter(lambda x: x['favouriteAnimal'] == "panda") | ||
pandaLovers.mapPartitions(writeRecords).saveAsTextFile(outputFile) | ||
# Try the more whole file input | ||
fullFileData = sc.wholeTextFiles(inputFile).flatMap(loadRecords) | ||
fullFilePandaLovers = fullFileData.filter(lambda x: x['favouriteAnimal'] == "panda") | ||
fullFilePandaLovers.mapPartitions(writeRecords).saveAsTextFile(outputFile+"fullfile") | ||
sc.stop() | ||
print "Done!" |
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