`
dalan_123
  • 浏览: 83664 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

spring hadoop之mapreduce batch

阅读更多
一、测试
// 定义hadoop configuration
Configuration conf = new Configuration();
// 指定hdfs上获取分析文件目录和输出分析结果目录
// 格式:hdfs://10.33.96.241:8020/user/tweets/input
//       hdfs://10.33.96.241:8020/user/tweets/output
// 最好使用当前hdfs系统用户目录;比如linux系统用户为tweets
String[] otherArgs = new GenericOptionsParser(conf, args)
.getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: HaashtagCount <in> <out>");
System.exit(2);
}
// 定义job 指定hadoop configuration和名称
Job job = new Job(conf, "hashtag count");
// 设定class所在的jar文件
job.setJarByClass(HashtagCount.class);
// 设定mapper
job.setMapperClass(TokenizerMapper.class);
// 设定job的合并类 一般为reduce实现对应的类
job.setCombinerClass(LongSumReducer.class);
// 设定reduce
job.setReducerClass(LongSumReducer.class);
// 由于mapreduce使用的key-value的格式
// 设定分析结果输出内容key的类型
job.setOutputKeyClass(Text.class);
// 设定分析结果输出内容value的类型
job.setOutputValueClass(LongWritable.class);
// 设定分析文件所在的路径
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
// 设定分析结果输出的路径
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
// 等待job完成
System.exit(job.waitForCompletion(true) ? 0 : 1);
二、map对应实现类
// 首先凡是map都必须继承Mapper 实现map方法
// 类Mapper四个参数:前两个执行mapper的key和value 最后两个为mapper执行
// 后输出的key和value
public static class TokenizerMapper extends
Mapper<Object, Text, Text, LongWritable> {
// 指定正则表达式
final static Pattern TAG_PATTERN = Pattern.compile("\"hashTags\":\\[([^\\]]*)");
// 指定执行mapper分解之后的输出结果key与value的类型
private final static LongWritable ONE = new LongWritable(1L);
private Text word = new Text();
// 必须实现的方面 执行mapper的操作均在该方法中
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
  // 获取符合要求的内容 循环进行分割内容
  Matcher matcher = TAG_PATTERN.matcher(value.toString());
  while (matcher.find()) {
String found = matcher.group();
String cleanedString = found.replaceFirst("\"hashTags\":\\[\\{\"text\":\"", "");
String superPolished = cleanedString.split("\",\"")[0];

        String useMe = superPolished;
if (superPolished.startsWith("\\u")) {
useMe = StringEscapeUtils.unescapeJava(superPolished);
}
useMe = useMe.split("\"")[0];
        // 将符合要求的内容和统计结果输出到对应分析结果中
       // 注:以上代码主要是对分析内容进行拆分 因为对应的统计结果ONE均为1
word.set(useMe.toLowerCase());
context.write(word, ONE);
      }
   }

}
三、reduce实现类
// 凡是实现reduce的必须继承Reducer类;前两个参数为mapper分析之后的结果
// key与value 最后两个参数为统计结果的key与value
// 同时每个类都必须实现reduce方法
public static class LongSumReducer extends
Reducer<Text, LongWritable, Text, LongWritable> {
  private LongWritable result = new LongWritable();

  // 实现reduce方法
  // 这个里面根据实际的业务需要实现对应的业务内容统计
  public void reduce(Text key, Iterable<LongWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (LongWritable val : values) {
    sum += val.get();
}
     // 由于该处针对的是同一个内容统计,因而只需要处理统计结果的value即可
result.set(sum);
    // 将统计结果提交到task的上下文环境中 输出
context.write(key, result);
    }
}
以上即为简单的mapreduce框架的实现过程,基于spring-hadoop api实现
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics