Java로 Spark 작업을 만들고 있습니다. 다음은 내 코드입니다.
CSV 파일에서 레코드를 필터링하려고합니다. 헤더 OID
에 COUNTRY_NAME
,, ...... 필드가 있습니다 .
을 기준으로 필터링하는 대신을 기준으로 필터링하는 s.contains("CANADA")
것처럼 좀 더 구체적으로 지정하고 싶습니다 COUNTRY_NAME.equals("CANADA")
. 어떻게 할 수 있는지에 대한 생각이 있습니까?
public static void main(String[] args) {
String gaimFile = "hdfs://xx.yy.zz.com/sandbox/data/acc/mydata";
SparkConf conf = new SparkConf().setAppName("Filter App");
JavaSparkContext sc = new JavaSparkContext(conf);
try{
JavaRDD<String> gaimData = sc.textFile(gaimFile);
JavaRDD<String> canadaOnly = gaimData.filter(new Function<String, Boolean>() {
private static final long serialVersionUID = -4438640257249553509L;
public Boolean call(String s) {
// My file id csv with header OID, COUNTRY_NAME, .....
// here instead of just saying s.contains
// i would like to be more specific and say
// if COUNTRY_NAME.eqauls("CANADA)
return s.contains("CANADA");
}
});
}
catch(Exception e){
System.out.println("ERROR: G9 MatchUp Failed");
}
finally{
sc.close();
}
}
먼저 값을 사용자 정의 클래스에 매핑해야합니다.
rdd.map(lines=>ConvertToCountry(line))
.filter(country=>country == "CANADA")
class Country{
...ctor that takes an array and fills properties...
...properties for each field from the csv...
}
ConvertToCountry(line: String){
return new Country(line.split(','))
}
위의 내용은 Scala와 의사 코드의 조합이지만 요점을 이해해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다