Hadoop 全局变量与数据传递
来源:互联网 发布:淘宝卖家15天账期延长 编辑:程序博客网 时间:2024/05/22 01:41
首先明确:
1.Hadoop不支持全局变量,也不建议使用全局变量。
我的理解是,这是因为hadoop具有map类和reducer类,并且不同的task一般执行的是不同的map或reduce。所以全局变量是无法传递的。(但是一般情况下,我们也许会需要一个对于所有map和reduce都能访问的全局变量),暂时我知道的解决方法如下:
2.如果Mapper类和Reducer类都是主类的内部类,可以在主类中使用 private static string global = "global variable";
但是这种方法不通用,因为Mapper类和Reducer类在概念上是与主类无关的。很多情况下,他们不是主类的内部类。所以该task所拥有的信息,只能从context上下文中获得,这也正体现了常说的:充足的上下文信息才是关键!
修正:Mapper类(或Reducer类) 使用 主类名.global 也可以获取全局变量。。
3.设置xml文件,然后在map函数中读取即可;
这种方法有一个缺点,值只能从客户端传递到Mapper中,reduce不能读出来。实际上reducer所知道的只有context信息。
4.直接在Configuration中设置属性值,然后读取这个属性值,在Mapper或Reducer中使用变量来存储这个值即可。
可以看出这种方式最为麻烦。可以将各个属性值放入文件中,然后在文件读取即可。
关键代码如下:
//初始化configuration后,使用conf.set("propertyName“,properyValue); //在mapper或reducer中, Configuration conf = context.getConfiguration(); String g = conf.get("propertyName"); //g作为可以使用的全局变量即可;
这里的文章讲述了hadoop下数据的传递:http://blog.csdn.net/jackydai987/article/details/6441241
代码如下:
- public class xml_test {
- public static int getFileInt(String filename) //从文件中读取预设值
- {
- int temp = 0;
- Configuration config = new Configuration();
- FSDataInputStream dis = null;
- try {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- FileSystem hdfs = FileSystem.get(config);
- dis = hdfs.open(new Path(filename));
- IOUtils.copyBytes(dis, baos, 4096, false); //写入ByteArrayOutputStream
- String str = baos.toString(); //这里我假定只有一行,多行可以使用循环。
- str = str.substring(0, str.length() - 1); //最后一个是回车,需要过滤掉,不然在整形转换时会出错
- temp = Integer.parseInt(str);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- IOUtils.closeStream(dis);
- }
- return temp;
- }
- public static class wordcountMapper extends
- Mapper<LongWritable, Text, Text, IntWritable>{
- int temp2 = getFileInt("/user/jackydai/int"); //从文件中读取预设的值
- //这里也可以通过args参数传一个文件进来,这样更灵活
- private Text word = new Text();
- public void map(LongWritable key, Text value, Context context)throws IOException, InterruptedException{
- int temp1 = 0;
- Configuration mapconf = context.getConfiguration();
- temp1 = mapconf.getInt("count", 0); //map读取值
- IntWritable one = new IntWritable(temp1 + temp2); //求和后输出,检测是否正确
- String line = value.toString();
- StringTokenizer itr = new StringTokenizer(line);
- while(itr.hasMoreElements()){
- word.set(itr.nextToken());
- context.write(word, one);
- }
- }
- }
- public static class wordcountReduce extends
- Reducer<Text, IntWritable, Text, IntWritable>{
- public void reduce(Text key, Iterable<IntWritable>values, Context context)throws IOException, InterruptedException{
- int sum = 0;
- for (IntWritable str : values){
- sum += str.get();
- }
- context.write(key, new IntWritable(sum));
- }
- }
- public static void main(String args[])throws Exception{
- Configuration conf = new Configuration();
- conf.setInt("count", 2); //设置值为2,需要注意的是设置值需要在new job之前
- Job job = new Job(conf, "xml_test");
- job.setJarByClass(xml_test.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- job.setMapperClass(wordcountMapper.class);
- job.setReducerClass(wordcountReduce.class);
- job.setCombinerClass(wordcountReduce.class);
- FileInputFormat.setInputPaths(job, new Path(args[1]));
- FileOutputFormat.setOutputPath(job, new Path(args[2]));
- job.waitForCompletion(true);
- }
- }
- Hadoop 全局变量与数据传递
- 使用全局变量传递数据
- Android通过全局变量传递数据
- [Android]使用全局变量传递数据
- Android使用意图传递数据之全局变量
- 使用全局变量在Activity之间传递数据
- GUI数据传递01-全局变量法
- android 使用Intent传递数据之全局变量传递
- android使用意图传递数据之全局变量传递
- 【Android数据传递】全局变量的使用方法(附源码)
- android开发入门-4(使用全局变量传递数据)
- android传递数据方式1---通过Application类实现全局变量
- 控制器之间的数据传递——全局变量传值
- 利用函数组的全局变量实现数据传递
- 数据传递与内存
- MySQL数据传递 Hadoop 非实时
- Intent传递参数简介与android方式的全局变量
- hadoop与大数据
- C++类overlode、override和overwrite详细解释
- vim的分割窗口
- PHP原理之内存管理
- 关于C/C++中结构体(Struct)类型的sizeof结果的探讨
- python-计算资源图标的crc值
- Hadoop 全局变量与数据传递
- 关于伸缩对话框的使用
- redhat enterprise linux as4 update 8(RHEL AS4 U8) 下载
- myeclipse中的快捷键
- Android中ProgressDialog的简单示例
- iOS中后台运行
- 界面无延迟显示数据后台计算
- Oracle常用小知识(一)
- poj 1323 Game Prediction