Skip to content

Commit

Permalink
add AirportsByLatitudeSolution and AirportsByLatitudeProblem
Browse files Browse the repository at this point in the history
  • Loading branch information
James Lee committed Mar 10, 2017
1 parent 86444bb commit e6b73d4
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 25 deletions.
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
...
*/
}
}
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");
}
}
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"
...
*/
}
}
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 src/main/java/com/sparkTutorial/rdd/airports/AirportsProblem.java

This file was deleted.

10 changes: 10 additions & 0 deletions src/main/java/com/sparkTutorial/rdd/commons/Utils.java
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 = ",(?=([^\"]*\"[^\"]*\")*[^\"]*$)";
}

0 comments on commit e6b73d4

Please sign in to comment.