自定义流读取内存中的SequenceFIle数据

package org.apache.hadoop.hdfs.server.incHdfs;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.apache.hadoop.fs.FSInputStream;

public class ByteInputStream extends FSInputStream {
	ByteArrayInputStream  bis ;
	private long position;
	
	public ByteInputStream(byte[] buffer){
		this.bis = new ByteArrayInputStream(buffer) ;
	}
	
	@Override
	public void seek(long pos) throws IOException {
		  bis.skip(pos) ;
		  this.position = pos + position;
	}

	@Override
	public long getPos() throws IOException {
	      return this.position;
	}

	@Override
	public boolean seekToNewSource(long targetPos) throws IOException {
		return false;
	}

	@Override
	public int read() throws IOException {
	        int value = bis.read();
	        if (value >= 0) {
	          this.position++;
	        }
	        return value;
	}
	
    public void close() throws IOException { bis.close(); }

}
 
conf.set("fs.default.name", "hdfs://localhost:9000/");
  <span style="white-space:pre">		</span>  ByteInputStream byteStream = new ByteInputStream(buffer) ;
  <span style="white-space:pre">		</span>  BufferedFSInputStream bfs = new BufferedFSInputStream(byteStream, buffer.length) ;
  <span style="white-space:pre">		</span>  FSDataInputStream fis = new FSDataInputStream(bfs) ;
<span style="white-space:pre">				</span>SequenceFile.Reader seqReader = new Reader(fis, 0, buffer.length, conf, false) ;
<span style="white-space:pre">				</span>Text key = new Text();
<span style="white-space:pre">			</span>  Text value = new Text();
<span style="white-space:pre">		</span>    while(seqReader.next(key, value))
<span style="white-space:pre">		</span>    {
<span style="white-space:pre">		</span>    <span style="white-space:pre">	</span>adjList.put(Integer.parseInt(key.toString()),Integer.parseInt(value.toString())) ;
<span style="white-space:pre">		</span>    }  
<span style="white-space:pre">		</span>    seqReader.close();
<span style="white-space:pre">		</span>    byteStream.close() ;


你可能感兴趣的:(sequence)