-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add AirportsByLatitudeSolution and AirportsByLatitudeProblem
- Loading branch information
James Lee
committed
Mar 10, 2017
1 parent
86444bb
commit e6b73d4
Showing
6 changed files
with
84 additions
and
25 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/com/sparkTutorial/rdd/airports/AirportsByLatitudeProblem.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,20 @@ | ||
package com.sparkTutorial.rdd.airports; | ||
|
||
public class AirportsByLatitudeProblem { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
/* Create a Spark program to read the airport data from in/airports.text, find all the airports whose latitude are bigger than 40. | ||
Then output the airport's name and the airport's latitude to out/airports_by_latitude.text. | ||
Each row of the input file contains the following columns: | ||
Airport ID, Name of airport, Main city served by airport, Country where airport is located, IATA/FAA code, | ||
ICAO Code, Latitude, Longitude, Altitude, Timezone, DST, Timezone in Olson format | ||
Sample output: | ||
"St Anthony", 51.391944 | ||
"Tofino", 49.082222 | ||
... | ||
*/ | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/sparkTutorial/rdd/airports/AirportsByLatitudeSolution.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,28 @@ | ||
package com.sparkTutorial.rdd.airports; | ||
|
||
import com.sparkTutorial.rdd.commons.Utils; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.apache.spark.SparkConf; | ||
import org.apache.spark.api.java.JavaRDD; | ||
import org.apache.spark.api.java.JavaSparkContext; | ||
|
||
public class AirportsByLatitudeSolution { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
SparkConf conf = new SparkConf().setAppName("airports").setMaster("local[2]"); | ||
|
||
JavaSparkContext sc = new JavaSparkContext(conf); | ||
|
||
JavaRDD<String> airports = sc.textFile("in/airports.text"); | ||
|
||
JavaRDD<String> airportsInUSA = airports.filter(line -> Float.valueOf(line.split(Utils.COMMA_DELIMITER)[6]) > 40); | ||
|
||
JavaRDD<String> airportsNameAndCityNames = airportsInUSA.map(line -> { | ||
String[] splits = line.split(Utils.COMMA_DELIMITER); | ||
return StringUtils.join(new String[]{splits[1], splits[6]}, ","); | ||
} | ||
); | ||
airportsNameAndCityNames.saveAsTextFile("out/airports_by_latitude.text"); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/sparkTutorial/rdd/airports/AirportsInUsaProblem.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,20 @@ | ||
package com.sparkTutorial.rdd.airports; | ||
|
||
public class AirportsInUsaProblem { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
/* Create a Spark program to read the airport data from in/airports.text, find all the airports which are located in United States | ||
and output the airport's name and the city's name to out/airports_in_usa.text. | ||
Each row of the input file contains the following columns: | ||
Airport ID, Name of airport, Main city served by airport, Country where airport is located, IATA/FAA code, | ||
ICAO Code, Latitude, Longitude, Altitude, Timezone, DST, Timezone in Olson format | ||
Sample output: | ||
"Putnam County Airport", "Greencastle" | ||
"Dowagiac Municipal Airport", "Dowagiac" | ||
... | ||
*/ | ||
} | ||
} |
11 changes: 6 additions & 5 deletions
11
...torial/rdd/airports/AirportsSolution.java → ...l/rdd/airports/AirportsInUsaSolution.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 |
---|---|---|
@@ -1,27 +1,28 @@ | ||
package com.sparkTutorial.rdd.airports; | ||
|
||
import com.sparkTutorial.rdd.commons.Utils; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.apache.spark.SparkConf; | ||
import org.apache.spark.api.java.JavaRDD; | ||
import org.apache.spark.api.java.JavaSparkContext; | ||
|
||
public class AirportsSolution { | ||
public class AirportsInUsaSolution { | ||
|
||
public static void main(String[] args) throws Exception { | ||
|
||
SparkConf conf = new SparkConf().setAppName("airports").setMaster("local[*]"); | ||
SparkConf conf = new SparkConf().setAppName("airports").setMaster("local[2]"); | ||
|
||
JavaSparkContext sc = new JavaSparkContext(conf); | ||
|
||
JavaRDD<String> airports = sc.textFile("in/airports.text"); | ||
|
||
JavaRDD<String> airportsInUSA = airports.filter(line -> line.split(",")[3].equals("\"United States\"")); | ||
JavaRDD<String> airportsInUSA = airports.filter(line -> line.split(Utils.COMMA_DELIMITER)[3].equals("\"United States\"")); | ||
|
||
JavaRDD<String> airportsNameAndCityNames = airportsInUSA.map(line -> { | ||
String[] splits = line.split(","); | ||
String[] splits = line.split(Utils.COMMA_DELIMITER); | ||
return StringUtils.join(new String[]{splits[1], splits[2]}, ","); | ||
} | ||
); | ||
airportsNameAndCityNames.saveAsTextFile("out/airports.text"); | ||
airportsNameAndCityNames.saveAsTextFile("out/airports_in_usa.text"); | ||
} | ||
} |
20 changes: 0 additions & 20 deletions
20
src/main/java/com/sparkTutorial/rdd/airports/AirportsProblem.java
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
package com.sparkTutorial.rdd.commons; | ||
|
||
public class Utils { | ||
|
||
private Utils(){ | ||
}; | ||
|
||
// a regular expression which matches commas but not commas within double quotations | ||
public static final String COMMA_DELIMITER = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)"; | ||
} |