1. WordCount程序结构如下:
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;
import java.io.IOException;
import java.util.StringTokenizer;
/**
* Created by zhangyan on 2016/12/28.
*/
public class WordCount1 {
public static void main(String args[])
throws IOException, ClassNotFoundException, InterruptedException {
//获取系统当前的配置项,默认加载core-default.xml和core-site.xml这两个xml中的内容
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Word Count 1.0");
job.setJarByClass(WordCount1.class);
//设置实现map函数的类
job.setMapperClass(MyMap.class);
//设置实现combiner的类
job.setCombinerClass(MyReduce.class);
//设置实现reduce函数的类
job.setReducerClass(MyReduce.class);
//设置map阶段产生的key和value的类型,
//如果Map output的key和value的class与reduce output的key和value的class相同,以下两句可以不写
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//设置reduce阶段产生的key和value的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
//设置输入路径
FileInputFormat.addInputPath(job, new Path(args[0]));
// FileInputFormat.setInputPaths(job, new Path(args[0])); // 可以设置多个Path
//设置输出路径
FileOutputFormat.setOutputPath(job,new Path(args[1]));
//job.waitForCompletion(true) 是提交job
System.exit(job.waitForCompletion(true) ? 0 : 1 );
}
public static class MyMap extends Mapper<Object, Text, Text, LongWritable>{
public static final LongWritable one = new LongWritable(1); //不必是static的
public static Text word = new Text(); //不必是static的
public void map(Object key, Text value, Context context) throws IOException,InterruptedException{
String lines = value.toString();
String[] arr = lines.split("\t");
for (String wd: arr) {
word.set(wd);
context.write(word,one);
}
}
}
public static class MyReduce extends Reducer<Text, LongWritable, Text, LongWritable>{
public static LongWritable result = new LongWritable(); //不必是static的
public void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException,InterruptedException{
long sum = 0;
for (LongWritable value: values){
sum+=value.get();
}
result.set(sum);
context.write(key,result);
}
}
}
2. Configuration:
可以通过构造函数Configuration(boolean loadDefaults)以决定程序是否load系统的配置文件,参数为false则不load系统的配置文件
可以通过构造函数Configuration(Configuration other)加载其他的Configuration对象中的配置项
可以通过addResource()方法,加载其他的配置文件,例如:conf1.addResource(new
Path("../hdfs-site.xml"));
Configuration的示例程序
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
/**
* Created by zhangyan on 2017/1/5.
*/
public class TestConfiguration {
public static void main(String[] args) throws Exception{
Configuration conf1=new Configuration();
Configuration conf2=new Configuration(false);
conf1.addResource(new Path("/Users/zhangyan/Documents/AuraBigData/ScalaCode/MyExercise/target/classes/hdfs-site.xml"));
System.out.println(conf1.get("fs.defaultFS"));
System.out.println(conf1.getInt("dfs.replication",-1));
System.out.println(conf1.getResource("core-site.xml"));
System.out.println(conf2.get("fs.defaultFS"));
System.out.println(conf2.getInt("dfs.replication",-1));
System.out.println(conf2.getResource("core-site.xml"));
conf2.addResource("core-site.xml");
conf2.addResource("hdfs-site.xml");
System.out.println(conf2.get("fs.defaultFS"));
System.out.println(conf2.getInt("dfs.replication",-1));
}
}
3. Job对象
用来在程序中设置job、提交job、控制job执行、查询job状态,其set方法只能在提交job之前使用