1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| package hadoopdemo;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; 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.Properties; import java.util.StringTokenizer;
public class average_s {
private static final String HDFS = "hdfs://hadoop01:9000/"; public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString(); StringTokenizer tokenizerArticle = new StringTokenizer(line, "\n");
while (tokenizerArticle.hasMoreElements()) {
StringTokenizer tokenizerLine = new StringTokenizer(tokenizerArticle.nextToken());
String strName = tokenizerLine.nextToken();
String strScore = tokenizerLine.nextToken();
Text name = new Text(strName);
int scoreInt = Integer.parseInt(strScore);
context.write(name, new IntWritable(scoreInt));
}
}
}
public static class Reduce extends
Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0; int count = 0;
for (IntWritable value : values) {
sum += value.get();
count++;
} int average = sum / count;
context.write(key, new IntWritable(average));
}
}
public static void main(String[] args) throws Exception {
Properties properties = System.getProperties(); properties.setProperty("HADOOP_USER_NAME", "hadoop");
Configuration conf = new Configuration(); conf.set("fs.defaultFS", HDFS); conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName()); conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName()); conf.set("dfs.client.use.datanode.hostname", "true");
Tools tool = new Tools(HDFS, conf); if (tool.exists("/1900301538/average_s")) tool.rmr("/1900301538/average_s"); tool.mkdirs("/1900301538/average_s"); tool.mkdirs("/1900301538/average_s/input"); tool.copyFile("D:\\li\\python.txt","/1900301538/average_s/input/python.txt"); tool.copyFile("D:\\li\\c++.txt","/1900301538/average_s/input/c++.txt"); tool.copyFile("D:\\li\\database.txt","/1900301538/average_s/input/database.txt");
Job job = Job.getInstance(conf, "求平均");
job.setMapperClass(Map.class); job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path("/1900301538/average_s/input/")); FileOutputFormat.setOutputPath(job, new Path("/1900301538/average_s/output/")); job.waitForCompletion(true); tool.cat("/1900301538/average_s/output/part-r-00000");
}
}
|