weblogic下部署的项目执行hbase的mapreduce报空指针错误解决方法

在Weblogic环境下部署的项目运行HBase MapReduce任务时遇到空指针异常。通过对比Tomcat和Weblogic上的源码调试,发现在TableMapReduceUtil.findOrCreateJar方法中返回的字符串为空。解决此类问题需要深入理解HBase与应用服务器的集成机制。

最近做mapreduce的项目,通过mapreduce操作hbase,项目部署在tomcat上一切正常,部署在weblogic上出现空指针错误,错误详情:

java.lang.NullPointerException
	at java.io.File.<init>(File.java:222)
	at java.util.zip.ZipFile.<init>(ZipFile.java:75)
	at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.updateMap(TableMapReduceUtil.java:617)
	at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.findOrCreateJar(TableMapReduceUtil.java:597)
	at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.addDependencyJars(TableMapReduceUtil.java:557)
	at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.addDependencyJars(TableMapReduceUtil.java:518)
	at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.initTableMapperJob(TableMapReduceUtil.java:144)
	at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.initTableMapperJob(TableMapReduceUtil.java:221)
	at org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil.initTableMapperJob(TableMapReduceUtil.java:87)
	at com.easymap.ezserver6.map.source.hbase.convert.HBaseMapMerge.beginMerge(HBaseMapMerge.java:163)
	at com.easymap.ezserver6.app.servlet.EzMapToHbaseService.doPost(EzMapToHbaseService.java:32)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
	at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
	at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
	at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:292)
	at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
	at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3594)
	at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
	at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
	at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2202)
	at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2108)
	at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1432)
	at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
	at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)
> 

通过在tomcat和weblogic上分别断点跟踪hbase的源代码,发现

TableMapReduceUtil.findOrCreateJar返回字符串为空,以下为findOrCreateJar代码

/**
	 * Find a jar that contains a class of the same name, if any. It will return
	 * a jar file, even if that is not the first thing on the class path that
	 * has a class with the same name. Looks first on the classpath and then in
	 * the <code>packagedClasses</code> map.
	 * 
	 * @param my_class
	 *            the class to find.
	 * @return a jar file that contains the class, or null.
	 * @throws IOException
	 */
	private static String findContainingJar(Class<?> my_class,
			Map<String, String> packagedClasses) throws IOException {
		ClassLoader loader = my_class.getClassLoader();
		String class_file = my_class.getName().replaceAll("\\.", "/")
				+ ".class";

		// first search the classpath
		for (Enumeration<URL> itr = loader.getResources(class_file); itr
				.hasMoreElements();) {
			URL url = itr.nextElement();
			if ("jar".equals(url.getProtocol())) {
				String toReturn = url.getPath();
				if (toReturn.startsWith("file:")) {
					toReturn = toReturn.substring("file:".length());
				}
				// URLDecoder is a misnamed class, since it actually decodes
				// x-www-form-urlencoded MIME type rather than actual
				// URL encoding (which the file path has). Therefore it would
				// decode +s to ' 's which is incorrect (spaces are actually
				// either unencoded or encoded as "%20"). Replace +s first, so
				// that they are kept sacred during the decoding process.
				toReturn = toReturn.replaceAll("\\+", "%2B");
				toReturn = URLDecoder.decode(toReturn, "UTF-8");
				return toReturn.replaceAll("!.*$", "");
			} 
		}

		// now look in any jars we've packaged using JarFinder. Returns null
		// when
		// no jar is found.
		return packagedClasses.get(class_file);
	}

此方法作用是寻找jar包路径,在tomcat下jar包的getProtocol返回的为jar类型,而weblogic下返回为zip类型,导致代码块没有执行,解决此问题方法为加入判断zip类型,完整代码如下:

/**
	 * Find a jar that contains a class of the same name, if any. It will return
	 * a jar file, even if that is not the first thing on the class path that
	 * has a class with the same name. Looks first on the classpath and then in
	 * the <code>packagedClasses</code> map.
	 * 
	 * @param my_class
	 *            the class to find.
	 * @return a jar file that contains the class, or null.
	 * @throws IOException
	 */
	private static String findContainingJar(Class<?> my_class,
			Map<String, String> packagedClasses) throws IOException {
		ClassLoader loader = my_class.getClassLoader();
		String class_file = my_class.getName().replaceAll("\\.", "/")
				+ ".class";

		// first search the classpath
		for (Enumeration<URL> itr = loader.getResources(class_file); itr
				.hasMoreElements();) {
			URL url = itr.nextElement();
			if ("jar".equals(url.getProtocol())) {
				String toReturn = url.getPath();
				if (toReturn.startsWith("file:")) {
					toReturn = toReturn.substring("file:".length());
				}
				// URLDecoder is a misnamed class, since it actually decodes
				// x-www-form-urlencoded MIME type rather than actual
				// URL encoding (which the file path has). Therefore it would
				// decode +s to ' 's which is incorrect (spaces are actually
				// either unencoded or encoded as "%20"). Replace +s first, so
				// that they are kept sacred during the decoding process.
				toReturn = toReturn.replaceAll("\\+", "%2B");
				toReturn = URLDecoder.decode(toReturn, "UTF-8");
				return toReturn.replaceAll("!.*$", "");
			} else if ("zip".equals(url.getProtocol())) {
				String toReturn = url.getPath();
				if (toReturn.startsWith("file:")) {
					toReturn = toReturn.substring("file:".length());
				}
				toReturn = toReturn.replaceAll("\\+", "%2B");
				toReturn = URLDecoder.decode(toReturn, "UTF-8");
				if (System.getProperty("os.name").indexOf("Windows") != -1) {
					return "/" + toReturn.replaceAll("!.*$", "");
				} else {
					return "//" + toReturn.replaceAll("!.*$", "");
				}
			}
		}

		// now look in any jars we've packaged using JarFinder. Returns null
		// when
		// no jar is found.
		return packagedClasses.get(class_file);
	}




	

这样就可以正常返回jar包路径,编译生成class文件,替换hbase-xx.jar中相应class







                
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值