2016-12-27 6 views
0

파일을 기존 시퀀스 파일에 추가하는 방법에 대한 샘플 코드 스 니펫을 제공해 줄 수 있습니까?// 사용자/{의 homedirectory} : 다음 기존 시퀀스 파일에 추가

내가 기존의 시퀀스 파일 OUTPUTFILE에 추가하는 데 사용되는 코드이지만 시퀀스 파일을 읽는 동안 후에는 체크섬 오류 던지고 추가 :

문제 열기 체크섬 파일을 데스크탑/샘플/시퀀스 파일/출력 파일. 무시 예외 : java.io.EOFException

public class AppendSequenceFile { 

    /** 
    * @param args 
    * @throws IOException 
    * @throws IllegalAccessException 
    * @throws InstantiationException 
    */ 
    public static void main(String[] args) throws IOException, 
      InstantiationException, IllegalAccessException { 

     Configuration conf = new Configuration(); 

     FileSystem fs = FileSystem.get(conf); 
     Path inputFile = new Path("/Users/{homedirectory}/Desktop/Sample/SequenceFile/sampleAppendTextFiles"); 
     Path sequenceFile = new Path("/Users/{homedirectory}/Desktop/Sample/SequenceFile/outputfile"); 
     FSDataInputStream inputStream; 
     Text key = new Text(); 
     Text value = new Text(); 
     SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, 
       sequenceFile, key.getClass(), value.getClass()); 
     FileStatus[] fStatus = fs.listStatus(inputFile); 

     for (FileStatus fst : fStatus) { 
      String str = ""; 
      System.out.println("Processing file : " + fst.getPath().getName() + " and the size is : " + fst.getPath().getName().length()); 
      inputStream = fs.open(fst.getPath()); 
      key.set(fst.getPath().getName()); 
      while(inputStream.available()>0) { 
       str = str+inputStream.readLine(); 
      } 
      value.set(str); 
      writer.append(key, value); 

     } 
    } 
} 

시퀀스 파일 리더 : 사전에

public class SequenceFileReader{ 
    public static void main(String[] args) throws Exception { 
     Configuration conf = new Configuration(); 
     FileSystem fs = FileSystem.get(conf); 
     Path path = new Path("/Users/{homedirectory}/Desktop/Sample/SequenceFile/outputfile"); 
     SequenceFile.Reader reader = null; 
     try { 
      reader = new SequenceFile.Reader(fs, path, conf); 
      Text key = new Text(); 
      Text value = new Text(); 
      while (reader.next(key, value)) { System.out.println(key); 
      System.out.println(value); 
      } 
     } finally { 
      IOUtils.closeStream(reader); 
     } 
    } 
} 

감사합니다.

+0

코드 작성자를 요청하는 것 같습니다. 이 솔루션을 찾기 위해 무엇을 스스로 시도 했습니까? –

+0

추가하려고 시도했지만 추가 후 시퀀스 파일을 읽는 동안 체크섬 오류 - – user3400887

+0

체크섬 파일 열 때 문제가 발생했습니다./Users/{homedirectory}/Desktop/Sample/SequenceFile/outputfile. 무시되는 예외 : java.io.EOFException. 그게 내가 여기에 질문을 게시 한 이유 다. 코드로 질문 편집 나는 잠시 동안 노력했다. – user3400887

답변

1

본인이 직접 해본 적이 없지만 Hadoop API 문서를 탐색하면 알게되었습니다.

이 API를 사용하여 작성기를 만들 수 있습니다. 이 API에서 SequenceFile

public static org.apache.hadoop.io.SequenceFile.Writer createWriter(FileContext fc,Configuration conf,Path name,Class keyClass,Class valClass,org.apache.hadoop.io.SequenceFile.CompressionType compressionType,CompressionCodec codec,org.apache.hadoop.io.SequenceFile.Metadata metadata,EnumSet<CreateFlag> createFlag,org.apache.hadoop.fs.Options.CreateOpts... opts) throws IOException

를 참조하십시오, CreateFlag는 "APPEND"옵션을 지정할 수 있습니다.

+0

응답 해 주셔서 감사합니다. hadoop-core-0.20.2.jar을 사용하고 있는데 createFlag 인수를 전달하는 기능이없는 것처럼 보입니다. – user3400887

+0

그래, 이건 2.7 버전에서 사용할 수있게 만든 것 같아. – Amit