草庐IT

java - Apache Hadoop 没有在我的程序中合并和减少它应该做的工作

coder 2024-01-06 原文

我是 Apache Hadoop 的初学者,尝试了 Apache 的字数统计程序,它运行良好。但是现在我想制作自己的室外温度程序来计算每日平均值。平均计算不符合我的预期;没有对数据进行合并和平均。

更具体地说,这里是我的 sample2.txt 输入文件的一部分:

25022016 00:00:00 -10.3
25022016 00:01:00 -10.3
25022016 00:02:00 -10.3
25022016 00:03:00 -10.3
...
25022016 00:59:00 -11.2

我想要的输出应该是:

25022016 7.9

这是该日期所有温度观测值的平均值。所以我有 60 个观察值,想要一个平均值。将来我想用同一个程序在更多的日子里处理更多的观察结果。 1. 列是日期(文本),2. 时间,第三列是温度。温度计算在代码中以Java的float数据类型完成。

现在发生的是输出:

25022016    -10.3
25022016    -10.3
25022016    -10.3
25022016    -10.3
...
25022016    -11.2

因此计算每个一个观察值的平均值(从一个数字计算出一个数字的平均值)。我想要 60 次观察的平均值(一个数字)!

所以我的输入输出文件在上面。我的 Java 代码(我在 Windows 7 -> VirtualBox -> Ubuntu 64 位上运行它)如下:


package hadoop; 

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

import org.apache.commons.cli.Options;

public class ProcessUnits2 
{ 
    public static class E_EMapper extends
    Mapper<Object, Text, Text, FloatWritable>
    { 
        private FloatWritable temperature = new FloatWritable();
        private Text date = new Text();       

        public void map(Object key, Text value, 
        Context context) throws IOException, InterruptedException 
        { 
            StringTokenizer dateTimeTemperatures = new StringTokenizer(value.toString());

            while(dateTimeTemperatures.hasMoreTokens()) {
                date.set(dateTimeTemperatures.nextToken());

                while(dateTimeTemperatures.hasMoreTokens()) {
                    dateTimeTemperatures.nextToken();    
                    temperature.set(Float.parseFloat(dateTimeTemperatures.nextToken()));

                    context.write(date, temperature);
                }
            }
        } 
    } 


    public static class E_EReduce extends Reducer<Text,Text,Text,FloatWritable>
    {
        private FloatWritable result = new FloatWritable();

        public void reduce( Text key, Iterable<FloatWritable> values, Context context
        ) throws IOException, InterruptedException 
        { 
            float sumTemperatures=0, averageTemperature;
            int countTemperatures=0;

            for (FloatWritable val : values) {
                sumTemperatures += val.get();
                countTemperatures++;
            } 

            averageTemperature = sumTemperatures / countTemperatures;

            result.set(averageTemperature);
            context.write(key, result);

        } 
    }  

    public static void main(String args[])throws Exception 
    { 
        Configuration conf = new Configuration();
        String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

        if (otherArgs.length < 2) {
            System.err.println("Usage: wordcount <in> [<in>...] <out>");
            System.exit(2);
        }
        Job job = Job.getInstance(conf, "VuorokaudenKeskilampotila");
        job.setJarByClass(ProcessUnits2.class);

        job.setMapperClass(E_EMapper.class);
        job.setCombinerClass(E_EReduce.class);
        job.setReducerClass(E_EReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FloatWritable.class);
        for (int i = 0; i < otherArgs.length - 1; ++i) {
            FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
        }
        FileOutputFormat.setOutputPath(job,
        new Path(otherArgs[otherArgs.length - 1]));
        job.setNumReduceTasks(0);

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    } 
} 
---------------------------------------------------

Hadoop 版本为 2.7.2 和 Ubuntu 14.04 LTS。我在独立模式下运行 hadoop(最基本的设置)。

以下是我用来构建程序的命令(如果有帮助?):

rm -rf output2 
javac -Xdiags:verbose -classpath hadoop-core-1.2.1.jar:/usr/local/hadoop/share/hadoop/common/lib/commons-cli-1.2.jar -d units2 ProcessUnits2.java
jar -cvf units2.jar -C units2/ .
hadoop jar units2.jar   hadoop.ProcessUnits2 input2 output2
cat output2/part-m-00000

作为初学者,我很困惑,在我看来,hadoop 在这里没有做任何合并和减少(= 平均)的工作,默认设置应该是它最最终的目的。我承认我从各处挑选代码(示例),因为没有任何效果,而且我确信这只是解决问题的一小步,但我猜不出它是什么。我可以很容易地用例如 C++ 做到这一点,根本不需要任何映射减少框架,但问题是我希望基本操作正常工作,所以我可以继续更复杂的例子,最终生产使用和真正的分布式映射-组合-减少.

如果有任何帮助,我将不胜感激。我现在被困在这个(很多很多小时......)。如果您需要任何额外数据来帮助找到解决方案,我会发送给他们。

最佳答案

您没有正确实现 reducer 。应该是:

public static class E_EReduce extends Reducer<Text, FloatWritable, Text, FloatWritable>
{
    @Override
    public void reduce( Text key, Iterable<FloatWritable> values, Context context) throws IOException, InterruptedException 
    { 

永远不要忘记@Override,否则编译器不会捕捉到错误。

关于java - Apache Hadoop 没有在我的程序中合并和减少它应该做的工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35667351/

有关java - Apache Hadoop 没有在我的程序中合并和减少它应该做的工作的更多相关文章

  1. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  2. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  3. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  4. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  5. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  6. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  7. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  8. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  9. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  10. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

随机推荐