mapreduce清洗mysql_MapReduce实验——数据清洗

packagemapreduce;importjava.io.IOException;importorg.apache.hadoop.fs.Path;importorg.apache.hadoop.io.IntWritable;importorg.apache.hadoop.io.Text;importorg.apache.hadoop.mapreduce.Job;importorg.apache.hadoop.mapreduce.Mapper;importorg.apache.hadoop.mapreduce.Reducer;importorg.apache.hadoop.mapreduce.lib.input.FileInputFormat;importorg.apache.hadoop.mapreduce.lib.output.FileOutputFormat;public classCleanResult {public static void main(String[] args) throwsIOException,ClassNotFoundException,InterruptedException{

Job job=Job.getInstance();

job.setJobName("CleanResult");

job.setJarByClass(WordCount.class);

job.setMapperClass(doMapper.class);

job.setReducerClass(doReducer.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

Path in= new Path("hdfs://192.168.12.128:9000/mapreduce/in/result.txt");

Path out= new Path("hdfs://192.168.12.128:9000/mapreduce/out/clean");

FileInputFormat.addInputPath(job,in);

FileOutputFormat.setOutputPath(job,out);

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

}public static class doMapper extends Mapper{public static Text word = newText();public static final IntWritable id = newIntWritable();

@Overrideprotected void map(Object key,Text value,Context context) throwsIOException,InterruptedException{

String[] data= value.toString().split(",");

String[] time= data[1].split(":");

data[1] = "2016-11-10 " + time[1] + ":" + time[2] + ":" + time[3].split(" ")[0];

String traffic= "";for(int i=0;i= 48 && data[3].charAt(i)<=57) {

traffic+= data[3].charAt(i);

}

}

data[3] =traffic;

String newValue= data[0];for(int i=1;i<5;i++)

newValue+= "\t" +data[i];

id.set(Integer.parseInt(data[5]));

word.set(newValue);

context.write(word, id);

}

}public static class doReducer extends Reducer{private IntWritable result = newIntWritable();

@Overrideprotected void reduce(Text key, Iterable values, Context context) throwsIOException, InterruptedException {

System.out.println("key:" +key);for(IntWritable value:values){

result.set(value.get());

}

System.out.println("value:" +result);

context.write(key, result);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值