Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.jbw.recommendsystem.guiyihua; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Partitioner; 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; // yarn jar com.jbw.recommendsystem.guiyihua.GYHMRD -Din= -Dout= /** * * @author alvin */ public class GYHMRD extends Configured implements Tool { static class XXPartition extends Partitioner<LongWritable, Text> { @Override public int getPartition(LongWritable key, Text value, int i) { double dd = Math.random(); if (dd > 0.8) { return 0; } else { return 1; } } } @Override public int run(String[] strings) throws Exception { Configuration conf = getConf(); Path in = new Path(conf.get("in")); Path out = new Path(conf.get("out")); Job job = Job.getInstance(conf); job.setJarByClass(GYHMRD.class); job.setJobName("fdsjh"); job.setMapOutputKeyClass(LongWritable.class); job.setMapOutputValueClass(Text.class); job.setInputFormatClass(TextInputFormat.class); job.setPartitionerClass(XXPartition.class); job.setOutputFormatClass(TextOutputFormat.class); job.setOutputKeyClass(LongWritable.class); job.setOutputValueClass(Text.class); job.setNumReduceTasks(2); TextInputFormat.addInputPath(job, in); TextOutputFormat.setOutputPath(job, out); return job.waitForCompletion(true) ? 0 : 1; } public static void main(String[] args) throws Exception { System.exit(ToolRunner.run(new GYHMRD(), args)); } }