Java examples for Big Data:Hadoop
Create hadoop mapper
import java.io.IOException; import java.util.Iterator; import org.apache.hadoop.conf.Configuration; 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.output.FileOutputFormat; public class CountAdmin { enum Counter {//ww w .ja v a 2 s. com TIMESKIP, IPSKIP, URLSKIP, } /** * * Map * */ public static class countAdminMapper extends Mapper<Object,Text,Text,IntWritable> { public final static IntWritable ONE = new IntWritable(1); public Text word = new Text(); @Override public void setup(Context context) { } @Override public void map(Object key,Text value,Context context) throws IOException, InterruptedException{ try { String url = Log.parser(value.toString()).getUrl(); if(url.startsWith("/admin.php")) { word.set(Log.parser(value.toString()).getIp()); context.write(word, ONE); } }catch(MyException e) { context.getCounter(Counter.IPSKIP).increment(1); } } @Override public void cleanup(Context context) { } } /** * * Reduce */ public static class countAdminReducer extends Reducer<Text,IntWritable,Text,IntWritable> { int sum = 0; @Override public void setup(Context context) throws IOException, InterruptedException { } public void reduce(Text key,Iterable<IntWritable> values,Context context) throws IOException, InterruptedException { Iterator<IntWritable> ite = values.iterator(); sum = 0; while(ite.hasNext()) { sum++; ite.next(); } context.write(key, new IntWritable(sum)); } @Override public void cleanup(Context context) throws IOException, InterruptedException { } } @SuppressWarnings("deprecation") public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Path input = new Path("hdfs://192.168.32.131/in/yourApp/"); Path output = new Path("hdfs://192.168.32.131/out/yourAppcountAdmin/"); Configuration conf = new Configuration(); Job job = new Job(conf,"countAdmin"); job.setJarByClass(CountAdmin.class); job.setMapperClass(countAdminMapper.class); job.setReducerClass(countAdminReducer.class); job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, input); FileOutputFormat.setOutputPath(job, output); System.exit(job.waitForCompletion(true)?0:1); } }