/*** * 读取Mapfile文件 * @author Administrator * */ public class ReadMapfile extends Configured implements Tool { @Override public int run(String[] args) throws Exception { //判断args参数个数 if(args.length!=2){ System.err.print("Usage:"); ToolRunner.printGenericCommandUsage(System.err); return -1; } FileSystem fs=FileSystem.get(this.getConf()); Path path=new Path(args[1]); IntWritable key=new IntWritable(Integer.parseInt(args[0])); Reader[] readers=MapFileOutputFormat.getReaders(fs, path, this.getConf()); //得到指定值所在的reader HashPartitioner<IntWritable,Text> partitioner=new HashPartitioner<IntWritable,Text>(); Text value=new Text(); int index=partitioner.getPartition(key, value, readers.length); Reader reader=readers[index]; //检查是否存在,如果不存在就算了,不再麻烦 了。 Writable entry=reader.get(key, value); if(entry==null){ return -1; } //如果存在,通过遍历的方式得到键为Key的就要得到所有的 IntWritable nextKey=new IntWritable(); do{ // System.out.println(value.toString()); }while(reader.next(nextKey, value) && key.equals(nextKey)); return 0; } /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { int nExitCode=ToolRunner.run(new ReadMapfile(),args); System.exit(nExitCode); } }
关键代码如下:
String keyStr="122"; Configuration conf=new Configuration(); conf.set("fs.default.name", "hdfs://myhadoop:9000"); FileSystem fs=FileSystem.get(conf); Path path=new Path("/mapFile"); Reader[] readers=MapFileOutputFormat.getReaders(fs, path, conf); //得到指定值所在的reader HashPartitioner<IntWritable,Text> partitioner=new HashPartitioner<IntWritable,Text>(); Text value=new Text(); IntWritable key=new IntWritable(Integer.parseInt(keyStr)); int index=partitioner.getPartition(key, value, readers.length); System.out.println(index); Reader reader=readers[index]; Writable w=reader.get(new Text(keyStr), new Text()); System.out.println(w.toString());