在Hadoop中,从MapFile文件中读取指定键值的元素

问题导读:
Configured基类的作用是什么?
Tool接口的作用是什么?
从MapFile文件中读取指定键值的元素的流程是什么?






在本示例中,我们实现了一个继承自Configured基类,并且实现了Tool接口的ReadMapfile类。这样ReadMapfile类就成为可以在Hadoop运行环境中执行的应用类。

一)准备阶段
Configured基类

Configured基类实现了Configurable接口。Configurable接口的实现使该类具有在Hadoop环境下执行时,设置/获取Hadoop各种运行上下文参数的功能。在Hadoop运行时,可以使用-conf命令行参数来指定一个本地的xml配置文件。而在没有指定-conf参数的情况下,Hadoop则采用HADOOP_INSTALL/conf中xml作为其运行的配置文件。另外,也可以在Hadoop运行时,通过其它一些命令行参数为Hadoop运行提供单个的参数,这些参数构成了Hadoop执行的上下文--运行环境。比如:
Hadoop fs -conf /home/lianpeixun/hadoop-local.xml -ls
//指定一个本地配置文件作为Hadoop运行的配置文件
Hadoop fs -ls
//使用Hadoop安装目录下的conf中的配置文件作为其运行的环境参数
Hadoop fs -fs file:/// -ls
//指定特定的属性fs.default.name为file:///

Configured类实现了Configurable接口的getConf(),setConf()方法:
//获取Hadoop的运行上下文参数信息
public Configuration getConf();
//将Hadoop运行上下文参数信息传递给类
public void setConf(Configuration conf);

Tool接口(运行带参数的mapreduce,详细可参考如何编写运行带参数输入输出路径hadoop程序
Tool接口是一个支持对一般的命令行选项进行处理的工具接口。Tool接口是所有Map-Reduce类型的工具和应用的标准实现接口。这些工具和应用将标准命令行选项交给ToolRunner.run来执行,从而使工具和应用只需关注它自己的参数就可以。实现了Tool接口的Map-Reduce类型的工具和应用中,简化了应用在运行时命令行参数信息的处理,使应用只需关注自身的参数。

二)流程
1)我们首先验证应用的参数个数是否正确,参数存在错误时,给出如何使用该应用的参数提示信息。

2)参数验证正确后,我们得到Hadoop运行环境下的文件系统(FileSystem)、要读取的MapFile的路径path(Path)和要读取的键的对象key(Writable)。

3)接着我们使用MapFileOutputFormat.getReaders(FileSystem fs,Path path,Configuration conf)静态方法得到要读取MapFile文件的输出数组readers(MapFile.Reader[])。readers是MapFile文件包含的所有输出的一个数组,这些数组中的每一个Reader记录了MapFile中的部分数据,数据在Reader中按照数据对的键(Key)的Hash值进行分布。

4)为了得到键值key所在的readers数组中的哪一个Reader对象中,我们需要构建一个HashPartitioner类的对象partitioner。HashPartitioner类是一个利用键(key)/值(value)的哈希(object.hashCode())为数据进行分区的类。在创建了对象partitioner后,我们可以根据要查找的键key,得到键如果存在时,应该所在分区的索引--readers输出数组的下标。得到数组的下标后,我们就可以得到要查找的键Key所在的输出reader。

5)我们使用reader.get(key,value)在reader中得到键key是否存在,如果不存在则退出。如果存在可通过reader.next()方法得到该reader()中所有的内容,并通过与键key的对比就可以得到键为key的所有的信息。

三)代码

  1. /***
  2. * 读取Mapfile文件
  3. * @author Administrator
  4. *
  5. */
  6. public class ReadMapfile extends Configured implements Tool {

  7.     @Override
  8.     public int run(String[] args) throws Exception {
  9.         //判断args参数个数
  10.         if(args.length!=2){
  11.             System.err.print("Usage:");
  12.             ToolRunner.printGenericCommandUsage(System.err);
  13.             
  14.             return -1;
  15.         }
  16.         FileSystem fs=FileSystem.get(this.getConf());
  17.         Path path=new Path(args[1]);
  18.         IntWritable key=new IntWritable(Integer.parseInt(args[0]));
  19.         
  20.         Reader[] readers=MapFileOutputFormat.getReaders(fs, path, this.getConf());
  21.         //得到指定值所在的reader
  22.         HashPartitioner<IntWritable,Text> partitioner=new HashPartitioner<IntWritable,Text>();
  23.         Text value=new Text();
  24.         int index=partitioner.getPartition(key, value, readers.length);
  25.         Reader reader=readers[index];
  26.         //检查是否存在,如果不存在就算了,不再麻烦 了。
  27.         Writable entry=reader.get(key, value);
  28.         if(entry==null){
  29.             return -1;
  30.         }
  31.         //如果存在,通过遍历的方式得到键为Key的就要得到所有的
  32.         IntWritable nextKey=new IntWritable();
  33.         do{
  34.             //
  35.             System.out.println(value.toString());
  36.         }while(reader.next(nextKey, value) && key.equals(nextKey));
  37.         
  38.         return 0;
  39.     }

  40.     /**
  41.      * @param args
  42.      * @throws Exception
  43.      */
  44.     public static void main(String[] args) throws Exception {
  45.         int nExitCode=ToolRunner.run(new ReadMapfile(),args);
  46.         
  47.         System.exit(nExitCode);
  48.     }
  49. }
复制代码


关键代码如下:

  1. String keyStr="122";
  2.                         Configuration conf=new Configuration();
  3.                         conf.set("fs.default.name", "hdfs://myhadoop:9000");
  4.                         FileSystem fs=FileSystem.get(conf);
  5.                         Path path=new Path("/mapFile");
  6.                         Reader[] readers=MapFileOutputFormat.getReaders(fs, path, conf);  
  7.                         //得到指定值所在的reader   
  8.                         HashPartitioner<IntWritable,Text> partitioner=new HashPartitioner<IntWritable,Text>();  
  9.                         Text value=new Text();
  10.                         IntWritable key=new IntWritable(Integer.parseInt(keyStr));
  11.                     int index=partitioner.getPartition(key, value, readers.length);   
  12.             System.out.println(index);
  13.                     Reader reader=readers[index];  
  14.             Writable w=reader.get(new Text(keyStr), new Text());
  15.             System.out.println(w.toString());
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值