java mapreduce 实例_Mapreduce实例--求平均值

package mapreduce;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.NullWritable;

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.input.TextInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;public classMyAverage{public static class Map extends Mapper{private static Text newKey=newText();public voidmap(Object key,Text value,Context context) throws IOException, InterruptedException{//将输入的纯文本文件数据转化成string

String line=value.toString();

System.out.println(line);//将值通过split()方法截取出来

String arr[]=line.split("\t");

newKey.set(arr[0]);int click=Integer.parseInt(arr[1]);//将数据和值输入到reduce处理

context.write(newKey, newIntWritable(click));

}

}public static class Reduce extends Reducer{public void reduce(Text key,Iterablevalues,Context context) throws IOException, InterruptedException{int num=0;int count=0;for(IntWritable val:values){//每个元素求和num

num+=val.get();//统计元素的次数count

count++;

}//统计次数

int avg=num/count;

context.write(key,newIntWritable(avg));

}

}public static voidmain(String[] args) throws IOException, ClassNotFoundException, InterruptedException{

Configuration conf=newConfiguration();

System.out.println("start");

Job job=new Job(conf,"MyAverage");

job.setJarByClass(MyAverage.class);

job.setMapperClass(Map.class);

job.setReducerClass(Reduce.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

job.setInputFormatClass(TextInputFormat.class);

job.setOutputFormatClass(TextOutputFormat.class);

Pathin=new Path("hdfs://172.18.74.137:9000/mymapreduce4/in/goods_click");

Pathout=new Path("hdfs://172.18.74.137:9000/mymapreduce4/out");

FileInputFormat.addInputPath(job,in);

FileOutputFormat.setOutputPath(job,out);

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce中进行数据清理可以分为多个阶段,第一阶段通常是数据清洗。下面简要介绍一下Java MapReduce实验中的数据清洗阶段。 数据清洗阶段的目的是将原始数据进行预处理,使得后续的计算能够更加准确和高效。具体而言,数据清洗阶段需要完成以下任务: 1. 去除无用数据:在数据中可能存在一些与计算无关的内容,比如注释、空行等,需要将这些内容去除。 2. 格式化数据:对于有些数据,可能存在格式上的不规范,需要将其进行规范化,比如日期格式、数字格式等。 3. 数据过滤:有些数据不符合计算要求,需要将其过滤掉。 4. 数据转换:有些数据需要进行转换,比如将字符串转为数字、将时间戳转为日期等。 下面是一个简单的Java MapReduce程序,用于进行数据清洗。该程序的输入为一个文本文件,包含多条记录,每条记录由多个字段组成,字段之间用逗号分隔。程序的输出为清洗后的数据集,每条记录仅包含需要的字段,并且字段之间用逗号分隔。 ```java public class DataCleanMapper extends Mapper<LongWritable, Text, NullWritable, Text> { private Text outputValue = new Text(); private StringBuilder builder = new StringBuilder(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String[] fields = value.toString().split(","); if (fields.length >= 5) { // 只保留前5个字段,其余字段丢弃 for (int i = 0; i < 5; i++) { builder.append(fields[i]).append(","); } builder.deleteCharAt(builder.length() - 1); outputValue.set(builder.toString()); context.write(NullWritable.get(), outputValue); builder.setLength(0); } } } ``` 该程序的主要逻辑如下: 1. 将输入的文本文件按行读取,每行数据作为一个键值对的value。 2. 根据逗号将每行数据分割成多个字段。 3. 判断字段个数是否大于等于5,如果是,则只保留前5个字段,其余字段丢弃。 4. 将清洗后的数据作为一个键值对的value,入输出文件中。键使用NullWritable.get()表示不需要指定键,输出的value为Text类型,值为清洗后的数据。 该程序的输出结果为清洗后的数据集,每条记录仅包含需要的字段,并且字段之间用逗号分隔。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值