복붙노트

[HADOOP] 매퍼 (mapper)에서 라인 단위로 HDFS에서 텍스트 파일 읽기

HADOOP

매퍼 (mapper)에서 라인 단위로 HDFS에서 텍스트 파일 읽기

HDFS에서 텍스트 파일을 올바르게 읽는 Mappers에 대한 다음 코드가 있습니까? 그리고 그것이 있다면 :

내 코드는 다음과 같습니다.

Path pt=new Path("hdfs://pathTofile");
FileSystem fs = FileSystem.get(context.getConfiguration());
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
String line;
line=br.readLine();
while (line != null){
System.out.println(line);

해결법

  1. ==============================

    1.일부 수정안과 함께 작동합니다. 붙여 넣은 코드가 잘린 것으로 가정합니다.

    일부 수정안과 함께 작동합니다. 붙여 넣은 코드가 잘린 것으로 가정합니다.

    Path pt=new Path("hdfs://pathTofile");
    FileSystem fs = FileSystem.get(context.getConfiguration());
    BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(pt)));
    try {
      String line;
      line=br.readLine();
      while (line != null){
        System.out.println(line);
    
        // be sure to read the next line otherwise you'll get an infinite loop
        line = br.readLine();
      }
    } finally {
      // you should close out the BufferedReader
      br.close();
    }
    

    둘 이상의 매퍼가 동일한 파일을 읽도록 할 수는 있지만 분산 캐시를 사용하는 것이 더 바람직합니다 (파일 블록을 호스트하는 데이터 노드의 부하를 줄일뿐만 아니라 더 효율적입니다) 작업 노드가있는 것보다 많은 수의 작업이있는 작업 인 경우)

  2. from https://stackoverflow.com/questions/14573209/read-a-text-file-from-hdfs-line-by-line-in-mapper by cc-by-sa and MIT license