View Full Version : Using spring context in mapper, how to autowire
hsn
Jul 20th, 2012, 05:36 AM
What is recommended to use Spring container in mapper?
In hadoop mapper, i do not have object creation under control. Hadoop will create mapper object and then call its initialization method. I would like to use @Inject in mapper, it means to create application context by hand, this is fine with me, but how to get application context to autowire resources into already created object?
Costin Leau
Jul 27th, 2012, 01:45 AM
We don't support this scenario in master but there's a branch you might want to try that adds this ability.
In a nutshell, you need to deploy Spring on all nodes and use a Utility mapper (potentially as a base class) that bootstraps Spring from a well-known location and then applies self-injection or autowiring.
As for the autowiring process, see AutowireCapableBeanFactory & co [1]
http://j.mp/MoWmI3
hsn
Jul 27th, 2012, 04:14 AM
Thanks for reply, branch is here: https://github.com/SpringSource/spring-hadoop/tree/mr-pojo/src/main/java/org/springframework/data/hadoop/mapreduce
Last commit to this branch is 8 months old. This POJO design pattern was abandoned or there are plans to merge it with current version or place that code in separate module?
hsn
Aug 7th, 2012, 05:49 AM
I am using in Mapper this
springcontext.getAutowireCapableBeanFactory().auto wireBean(this);
but no autowiring is done. Fields annotated with @Inject are still null and no error message.
Costin Leau
Aug 7th, 2012, 05:59 AM
Do you have javax.inject in the classpath (that is on each cluster node)? You could try @Autowired as a replacement.
If the annotation library is not found, its declaration is simply ignored.
hsn
Aug 7th, 2012, 06:40 AM
javax.inject is part of hadoop since 0.23 (hadoop-2).
ls $YARN_HOME/share/hadoop/mapreduce/lib/javax*
/usr/local/hadoop/hadoop-0.23.3-SNAPSHOT/share/hadoop/mapreduce/lib/javax.inject-1.jar
its listed as guice dependency
[INFO] +- org.apache.hadoop:hadoop-mapreduce-client-core:jar:0.23.3-SNAPSHOT:provided
[INFO] | +- org.apache.hadoop:hadoop-yarn-common:jar:0.23.3-SNAPSHOT:provided
[INFO] | | +- org.apache.hadoop:hadoop-yarn-api:jar:0.23.3-SNAPSHOT:provided
[INFO] | | +- com.google.inject:guice:jar:3.0:provided
[INFO] | | | \- javax.inject:javax.inject:jar:1:provided
No difference with @Autowired but maybe i should call something different then autowireBean(this); there is lot of similar functions in autowirecapablebeanfactory
hsn
Aug 7th, 2012, 07:00 AM
springcontext.getAutowireCapableBeanFactory().auto wireBeanProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true); didnt worked either
hsn
Aug 7th, 2012, 08:07 AM
i didnt had context annotation processing turned on in XML file. now it works in combination with autowirebeanproperties
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.