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.
Add json parsing example in java,python and csv example in scala
- Loading branch information
Showing
5 changed files
with
100 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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/oreilly/learningsparkexamples/java/BasicLoadJson.java
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,52 @@ | ||
/** | ||
* Illustrates loading a json file and finding out if people like pandas | ||
*/ | ||
package com.oreilly.learningsparkexamples.java; | ||
|
||
import java.io.StringReader; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Iterator; | ||
import java.lang.Iterable; | ||
import scala.Tuple2; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.apache.spark.api.java.JavaRDD; | ||
import org.apache.spark.api.java.JavaPairRDD; | ||
import org.apache.spark.api.java.JavaSparkContext; | ||
import org.apache.spark.api.java.function.FlatMapFunction; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class BasicLoadJson { | ||
|
||
public static class Person { | ||
public String name; | ||
public Boolean lovesPandas; | ||
} | ||
|
||
public static class ParseJson implements FlatMapFunction<Iterator<String>, Person> { | ||
public Iterable<Person> call(Iterator<String> lines) throws Exception { | ||
ArrayList<Person> people = new ArrayList<Person>(); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
while (lines.hasNext()) { | ||
String line = lines.next(); | ||
people.add(mapper.readValue(line, Person.class)); | ||
} | ||
return people; | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
if (args.length != 2) { | ||
throw new Exception("Usage BasicLoadJson [sparkMaster] [jsoninput]"); | ||
} | ||
String master = args[0]; | ||
String fileName = args[1]; | ||
JavaSparkContext sc = new JavaSparkContext( | ||
master, "basicloadjson", System.getenv("SPARK_HOME"), System.getenv("JARS")); | ||
JavaRDD<String> input = sc.textFile(fileName); | ||
JavaRDD<Person> result = input.mapPartitions(new ParseJson()); | ||
List<Person> resultCollection = result.collect(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/scala/com/oreilly/learningsparkexamples/scala/BasicParseCsv.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 au.com.bytecode.opencsv.CSVReader | ||
|
||
object BasicParseCsv { | ||
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, "BasicParseCsv", System.getenv("SPARK_HOME")) | ||
val input = sc.textFile(inputFile) | ||
val result = input.map{ line => | ||
val reader = new CSVReader(new StringReader(line)); | ||
reader.readNext(); | ||
} | ||
println(result.collect().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,13 @@ | ||
from pyspark import SparkContext | ||
import json | ||
import sys | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) != 3: | ||
print "Error usage: LoadJson [sparkmaster] [file]" | ||
sys.exit(-1) | ||
sc = SparkContext(sys.argv[1], "LoadJson") | ||
input = sc.textFile(sys.argv[2]) | ||
data = input.map(lambda x: json.loads(x)) | ||
print data.collect() | ||
|