mapreduce清洗mysql_MapReduce---数据清洗

该博客介绍了一个使用MapReduce进行MySQL数据清洗的Java程序。程序通过Mapper和Reducer类处理输入数据,Mapper读取文本输入并按键值对输出,Reducer收集相同键的所有值,并将清洗后的数据保存到HDFS上。
摘要由CSDN通过智能技术生成

package mapreduce;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

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 classdataclean{public static List ips=new ArrayList();public static List times=new ArrayList();public static List traffic=new ArrayList();public static List wen=new ArrayList();public static List shi=new ArrayList();public static class Map extends Mapper{private static Text Name =newText();private static Text num=newText();public voidmap(Object key,Text value,Context context) throws IOException, InterruptedException{

String line=value.toString();

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

Name.set(arr[0]);

num.set(arr[0]);

context.write(Name,num);

}

}public static class Reduce extends Reducer< Text, Text,Text, Text>{private static Text result= newText();int i=0;public void reduce(Text key,Iterablevalues,Context context) throws IOException, InterruptedException{for(Text val:values){

context.write(key, val);

ips.add(val.toString());

}

}

}public static intrun()throws IOException, ClassNotFoundException, InterruptedException

{

Configuration conf=newConfiguration();

conf.set("fs.defaultFS", "hdfs://localhost:9000");

FileSystem fs=FileSystem.get(conf);

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

job.setJarByClass(dataclean.class);

job.setMapperClass(Map.class);

job.setReducerClass(Reduce.class);

job.setOutputKeyClass(Text.class);

job.setOutputValueClass(Text.class);

job.setInputFormatClass(TextInputFormat.class);

job.setOutputFormatClass(TextOutputFormat.class);

Pathin=new Path("hdfs://localhost:9000/test2/in/result.txt");

Pathout=new Path("hdfs://localhost:9000/test2/out/ip/1");

FileInputFormat.addInputPath(job,in);

fs.delete(out,true);

FileOutputFormat.setOutputPath(job,out);return(job.waitForCompletion(true) ? 0 : 1);

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

run();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值