目的:本案列的目的是统计500w条数据被搜索使用的次数
由于数据包无法在这上传,所以数据无法给你们,你们可以自己搞一些数据自己学习学习!
1.根据命令 hadoop fs -put ./sogou.500w.utf8 / 把500w这个包移动到hdfs 根目录下,一般(/)表示
2.
2.1 在shell命令中的运行格式:hadoop jar jar包 包名.类名 文件的路径
(首先你要把java代码打成jar包,当然还有一点文件的路径这需要一个不重复的路劲),如:
hadoop jar abc.jar cn.xdl.test.SogouUID /count02
2.2 或者先在eclipse上运行,如下图
3.代码如下:
package cn.xdl.reduce;
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;
class SogouMap extends Mapper<Object, Text, Text, IntWritable> {
//
public void map(
Object key,
Text value,
org.apache.hadoop.mapreduce.Mapper<Object, Text, Text, IntWritable>.Context context)
throws java.io.IOException, InterruptedException {
// 20111230133301 a5e535dc9cbadc510ee515982c817a0f 356期藏机图 3 3
// http://www.yibocai.com/ZMTM/201112/265336.html
String line = value.toString();
String[] ss = line.split("\t");
context.write(new Text(ss[1]), new IntWritable(1));
};
}
class SogouReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(
Text key,
java.lang.Iterable<IntWritable> values,
org.apache.hadoop.mapreduce.Reducer<Text, IntWritable, Text, IntWritable>.Context context)
throws java.io.IOException, InterruptedException {
int sum = 0;
for(IntWritable count : values){
sum+=count.get();
}
context.write(key, new IntWritable(sum));
};
}
public class SogouUID {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
String inpath = args[0];
String outpath = args[1];
//创建作业实例
Job job = Job.getInstance();
job.setJobName(SogouUID.class.getName());
// 打jar包
job.setJarByClass(SogouUID.class);
// 设置处理Map/Reduce阶段的类
job.setMapperClass(SogouMap.class);
job.setReducerClass(SogouReduce.class);
//设置reduce的任务个数
job.setNumReduceTasks(3);
// 设置最终输出key/value的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 设置输入/输出路径
FileInputFormat.addInputPath(job, new Path(inpath));
FileOutputFormat.setOutputPath(job, new Path(outpath));
// 提交作业
job.waitForCompletion(true);
}
}
4.查看运行后的情况,可使用命令Hadoop fs -cat /count02/path*