Reading and Writing Sequencefile using Hadoop 2.0 Apis

前端 未结 4 502
南旧
南旧 2020-12-14 08:50

I am looking for an example which is using the new API to read and write Sequence Files.

Effectively I need to know how to use these functions

 creat         


        
4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-14 09:50

    public class SequenceFilesTest {
      @Test
      public void testSeqFileReadWrite() throws IOException {
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.getLocal(conf);
        Path seqFilePath = new Path("file.seq");
        SequenceFile.Writer writer = SequenceFile.createWriter(conf,
                Writer.file(seqFilePath), Writer.keyClass(Text.class),
                Writer.valueClass(IntWritable.class));
    
        writer.append(new Text("key1"), new IntWritable(1));
        writer.append(new Text("key2"), new IntWritable(2));
    
        writer.close();
    
        SequenceFile.Reader reader = new SequenceFile.Reader(conf,
                Reader.file(seqFilePath));
    
        Text key = new Text();
        IntWritable val = new IntWritable();
    
        while (reader.next(key, val)) {
            System.err.println(key + "\t" + val);
        }
    
        reader.close();
      }
    }
    

提交回复
热议问题