hadoop这个有人会做吗 急

有大佬会做这个吗 急!!在集群服务器master的本地目录上有日志文件rotinstall.log,要求对文件中的单词进行统计,求单词的平均长度。

写一个java代码打包成jar包不就好了,示例代码如下

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;

public class WordCountJob extends Configured implements Tool {
    public static void main(String[] args) throws Exception {
        ToolRunner.run(new WordCountJob(),args);
    }

    @Override
    public int run(String[] strings) throws Exception {
        //1.创建配置累对象,设置参数
        Configuration conf=new Configuration();
        conf.set("fs.defaultFS","hdfs://hadoop10:9000");
        //2.创建Job对象
        Job job= Job.getInstance(conf);
        job.setJarByClass(WordCountJob.class);

        //3.设置读取文件和输出形式
        job.setInputFormatClass(TextInputFormat.class);
        job.setOutputFormatClass(TextOutputFormat.class);
//设置读取(必须存在)和输出(必须不存在)路径
        TextInputFormat.addInputPath(job,new Path("/bai/wordcount.txt"));
        TextOutputFormat.setOutputPath(job,new Path("/bai/demo"));

        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        //4.设置程序员自定义的mapper reducer
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        boolean b=job.waitForCompletion(true);//打印日志信息
        //waitForCompletion等待job运行结果,如果为真代表运行成功
        return b?1:0;
    }

    public static class WordCountMapper extends Mapper<LongWritable,Text,Text,IntWritable> {

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //1.key value 师Mapper输入的key-value ,分别代表偏移量和一行数据
//            2.对value进行拆分,然后对拆分的数组进行遍历
            String [] names=value.toString().split(" ");
            for(String name:names){
                //3.使用context对象输出key-value
                context.write(new Text(name),new IntWritable(1));
            }
        }
    }
    public static class WordCountReducer extends Reducer<Text,IntWritable,Text,IntWritable>{
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context)
                throws IOException, InterruptedException {
//            1.对集合遍历
//            对values集合遍历,获取里面的元素进行相加
            int sum=0;
            for(IntWritable v:values){
                sum+=v.get();
            }
            //3.输出reduce计算结果,key-value
            context.write(key,new IntWritable(sum));
        }
    }
}

如果对你有帮助,记得采纳一下哦,谢谢~

实现思路及步骤:

(1)上传otinstall.log 到HDFS目录useroov

(2)使用Hadoop官方的示例程序包

hadoop-mapreduce eamples-2 6.4.jar,以hadoop jar命令提交MapReduce任务。