电子科技大学成都学院实践专周设计报告
修改yarn-site.xml,在添加以下内容
图3-4 yarn-site.xml文件
修改mapred.site.xml,在添加以下内容
图3-5 mapred.site.xml文件
3.2.4 测试hadoop是否搭建成功
1.启动hadoop ,第一次启动hadoop时需要格式化 : bin/hdfs namenode -format 2.进入sbin目录执行 : ./start-all.sh
3.启动完成后查看hadoop的进程是否全部启动,执行命令jps
12
第4章 MapReduce并行设计实现
第4章 MapReduce并行设计实现
4.1 第一案例要求
倒排索引
案例1:现有一批电话通信清单,记录了用户A拨打某些特殊号码(如120,10086,13800138000等)的记录。需要做一个统计结果,记录拨打给用户B的所有用户A。
添加原始数据
图4-1 添加数据
处理后的数据
图4-2 显示数据
4.2 核心代码1
计数器 Counter 是一个计数器 可以记录这个程序一些数据用于统计。 public class Test extends Configured implements Tool {
13
电子科技大学成都学院实践专周设计报告
enum Counter { LINESKIP, // 出错的行 }
public static class Map extends Mapper
// 读取源文件,line得到的就是输入文件的一行数据 String line = value.toString(); try { // 数据处理
String[] lineSplit = line.split(\对源数据进行分割重组 String anum = lineSplit[0]; // 主叫 String bnum = lineSplit[1]; // 被叫
context.write(new Text(bnum), new Text(anum)); // 输出 } catch (ArrayIndexOutOfBoundsException e) {
context.getCounter(Counter.LINESKIP).increment(1); // 出错令计数器加1 2.Reduce函数
public static class Reduce extends Reducer
// 每个value代表Map函数发送的一个value
14
第4章 MapReduce并行设计实现
// 在这里代表拨打了这个被叫号码的一个主叫 for (Text value : values) { valueString = value.toString(); out += valueString + \ context.write(key, new Text(out)); 3.运行
public int run(String[] args) throws Exception { Configuration conf = getConf();
Job job = new Job(conf, \任务名 job.setJarByClass(Test.class); // 执行class
FileInputFormat.addInputPath(job, new Path(args[0])); // 输入路径 FileOutputFormat.setOutputPath(job, new Path(args[1])); // 输出路径 job.setMapperClass(Map.class); // 指定上面Map类作为MAP任务代码
job.setReducerClass(Reduce.class); // 指定上面Reduce类作为REDUCE任务代码 job.setOutputFormatClass(TextOutputFormat.class); job.setOutputKeyClass(Text.class); // 指定输出KEY的格式 job.setOutputValueClass(Text.class); // 指定输出VALUE的格式等哈 job.waitForCompletion(true); return job.isSuccessful() ? 0 : 1; }
public static void main(String[] args) throws Exception { int res = ToolRunner.run(new Configuration(), new Test(), args ); System.exit(res);
15
电子科技大学成都学院实践专周设计报告
4.3 第二案例要求
添加原始数据
图4-3 添加数据
显示原始数据
图4-4 结果图
4.4 核心代码2
private static Configuration conf = null; static {
16