我试图使用AspectJ与MapReduce的例子,虽然我不理解一件事。但首先,让我给你我的代码。
[1] Wordcount示例
package org.apache.hadoop.mapred.examples;
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.mapred.*;
import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
/**
* Common Wordcount example
*/
public class WordCount {
public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
word.set(tokenizer.nextToken());
output.collect(word, one);
}
}
}
public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
int sum = 0;
while (values.hasNext()) {
sum += values.next().get();
}
output.collect(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception {
JobConf conf = new JobConf(WordCount.class);
conf.setJobName("wordcount");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
conf.setMapperClass(Map.class);
conf.setCombinerClass(Reduce.class);
conf.setReducerClass(Reduce.class);
conf.setNumReduceTasks(2);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
}
}
[2]我的mapreduce方面
package org.apache.hadoop.mapred.aspects;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MapReduceAspects {
@Before("execution(* map(..))")
public void mymap(JoinPoint joinPoint) {
System.out.println("My Map Execution: " + joinPoint.getArgs() + ":" + joinPoint.getTarget());
Object[] obj = joinPoint.getArgs();
for (Object o : obj){
System.out.println(o.toString());
}
}
@Before("execution(* reduce(..))")
public void myreduce() { System.out.println("My Reduce Execution"); }
@Before("execution(* collect(..))")
public void updatehash(JoinPoint joinPoint) {
System.out.println("Output collect: Args: " + joinPoint.getArgs());
}
}
' ' '
[3] bean-aspects.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">
<aop:aspectj-autoproxy proxy-target-class="true">
<aop:include name="mapreduceAspect"/>
</aop:aspectj-autoproxy>
<bean id="mapreduceAspect" class="org.apache.hadoop.mapred.aspects.MapReduceAspects"/></beans>
[4] OutputCollector接口
package org.apache.hadoop.mapred;
import java.io.IOException;
import org.apache.hadoop.classification.InterfaceAudience.Public;
import org.apache.hadoop.classification.InterfaceStability.Stable;
@Public
@Stable
public interface OutputCollector<K, V> {
void collect(K var1, V var2) throws IOException;
}
在[1]中,我有一个具有map
和reduce
函数的单词计数示例。当我在MapReduce框架中启动我的应用程序时,框架将创建一个执行map
和reduce
函数的作业。map
函数接受输入的目录,reduce
函数输出结果。
我可以用AspectJ拦截map
和reduce
函数调用,但我不能在map
函数中的指令output.collect(word, one)
中拦截collect
调用。为什么会这样呢?是因为接口中的collect
方法没有标注[4]吗?或者我没有正确配置方面?
我希望有人能解释一下为什么AspectJ是这样的行为。
谢谢,
答案很简单:
-
map
和reduce
方法在你自己的代码中,也就是说,它们受制于execution()
切入点的方面编织。 -
collect
方法在第三方库中,在正常情况下不受方面编织的影响。因此,您不能使用execution()
切入点来拦截它,只能使用call()
切入点。 - 试试
call(* collect(..))
这样的东西,它会工作的。警告:Spring AOP不支持call()
切入点,你必须使用成熟的AspectJ才能使用它。有关如何激活AspectJ LTW(加载时编织)的更多信息,请参阅Spring手册第10.8章。如果你通过LTW或二进制CTW(编译时编织)使用完整的AspectJ,也可以编织第三方代码并使用execution()
切入点。您只需要确保在任何目标第三方代码之前加载编织代理,这通常是情况,因为这就是Java代理被发明的目的。