2017-11-01 7 views
2

Iam이 hadoop (mapreduce)을 배우려고합니다. 나는 파서를 만들기 위해 Date 클래스를 사용하는 mapper 메서드를 가지고있다; epoch_time; 필드는 데이터 집합의 밀리 초 단위로 표시됩니다. 데이터 세트는 25.05.2015에서 10.08.2015 사이의 기간으로 구성됩니다.에포크를 날짜 및 시간으로 변환 - HADOOP

시간을 날짜/시간으로 변환하고 싶지만 05.06.2015에서 15.06.2015 사이의 날짜/시간 만 반환하고 싶습니다.

여기까지 내가 지금까지 성취 한 바가 있습니다. 원하는 출력을

2015년 5월 6일 5 // 카운트를

2015년 5월 25일

2015년 6월 25일

:

출력 : 아래의 코드는 다음을 생성 이 날짜에 단어 발생

06.06. 2015 53

2015년 7월 6일 41

매퍼

public class mapper extends Mapper<Object, Text, Text, IntWritable> { 
    private Text data = new Text(); 
    private IntWritable one = new IntWritable(1); 
    String time; 

     public void map(Object key, Text value, Context context) throws IOException,  InterruptedException { 

String[] userinput = value.toString().split(";"); 
try{  


     LocalDateTime epoch = LocalDateTime.ofEpochSecond(Long.parseLong(userinput[0])/1000, 0, ZoneOffset.UTC); 
     DateTimeFormatter f = DateTimeFormatter.ofPattern("dd.MM.yyyy"); 
     time = epoch.format(f); 




    data.set(time); 
    context.write(data,one); 
} 
catch(Exception e){ 
    System.out.println("Error: " + e); 
} 

    } 
} 

감속기는

 public class reducer extends Reducer<Text, IntWritable, Text, IntWritable> { 

private IntWritable one = new IntWritable(); 

public void reduce(Text key, Iterable<IntWritable> values, Context context) 

    throws IOException, InterruptedException { 

    int sum = 0; 

    for (IntWritable value : values) { 

     sum+=value.get(); 

    } 

    one.set(sum); 
    context.write(key, one); 

} 

}

+0

안녕을 확인, 코드 내 매퍼 클래스의 스 니펫이다. 나는 매퍼 (mapper), 감속기 (reducer), 운전자 클래스를 가지고있다. 사용할 날짜 패턴을 조언 해 주시겠습니까? 많은 감사합니다 – user2023

+0

그리고 하둡에 관한 것이 아니라는 요지는 여전히 정확합니다. 날짜 범위를 식별하기위한 단위 테스트 또는 일반 Java 프로그램을 작성한 후, 날짜 범위의 서브 세트를 추출 할 수있는 매퍼에 조건을 입력하십시오. –

+0

안녕하십니까, 날짜 형식에 대한 기간이 있습니다. 코드는 업데이트 된 코드 스 니펫을 참조하십시오. 기간에 어떻게 액세스합니까? 날짜 범위를 가져 오는 방법에 대한 예제를 제공해 주시겠습니까? – user2023

답변

0

그래서 당신은이 데이터를 괄호 쳐진 관심을 .. . 25.05.2015 [05.06.2015 ... 15.06.2015] 10.08.2015

필요한 경우 if 문과 같이 간단합니다.

내가 자바 8이 익숙하지 해요,하지만이 Java: how do I check if a Date is within a certain range?

public class mapper extends Mapper<Object, Text, Text, IntWritable> { 
    private Text data = new Text(); 
    private static final IntWritable ONE = new IntWritable(1); 
    private static final DateTimeFormatter FMT = DateTimeFormatter.ofPattern("dd.MM.yyyy"); 
    String time; 

    // Define the boundaries 
    private LocalDateTime start = LocalDateTime.parse("2015.06.05", FMT); 
    private LocalDateTime end = LocalDateTime.parse("2015.06.15", FMT); 

    @Override 
    public void map(Object key, Text value, Context context) throws IOException,  InterruptedException { 

     String[] userinput = value.toString().split(";"); 
     try { 
      Long ms = Long.parseLong(userinput[0])/1000;  
      LocalDateTime inputEpoch = LocalDateTime.ofEpochSecond(ms, 0, ZoneOffset.UTC); 

      // Filter your data 
      if (inputEpoch.isAfter(start) && inputEpoch.isBefore(end)) { 
       data.set(inputEpoch.format(FMT)); 
       context.write(data,ONE); 
      } 
     } catch (...) { } 
    } 
} 
+0

클래스를 수정했습니다. 귀하의 제안을 적용했지만 2 오류가 발생합니다. 방법 ChronoLocalDateTime.isAfter (ChronoLocalDateTime 가 있으면) (inputEpoch.isAfter (시작) && inputEpoch.isBefore (단부)) { [javac의]^ [javac의] 방법 ChronoLocalDateTime.isBefore (ChronoLocalDateTime )가 적용되지 않으며, 적용되지 않는다. (인수 불일치 : LocalDate를 ChronoLocalDateTime 으로 변환 할 수 없습니다.) – user2023

+0

'LocalDateTime'이 필요합니다. 다시 시도하십시오. –

+0

많은 분들께서 cricket_007에게 감사드립니다. 이것은 효과가있다. – user2023