[英]Error while calling a mapred job from a servlet
我是Hadoop爱好者,仍处于学习阶段,出于好奇,我尝试了一些尝试,我想让servlet称为hadoop工作。 我尝试了两种方法,但都失败了。 等等,首先有人可以告诉我是否可行吗? 如果是这样,请通过一些实时示例来启发(不要告诉我Hue ),或者干脆可以告诉我我疯了,浪费了我的时间。
好吧,如果您正在阅读本文,那么我并不疯。 现在请看一下我的代码,并告诉我我在做什么错!!!
package com.testingservlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
/**
* Servlet implementation class HelloServlets
*/
@WebServlet("/HelloServlets")
public class HelloServlets extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlets() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// TODO Auto-generated method stub
/*******************************************************************
* *Approach 1
*
* Using the Hadoop code directly into servlets
* *****************************************************************
*/
String localPath = "/home/asadgenx/filelist.txt";
FileSystem fs = FileSystem.get( new Configuration());
Path workingDir = fs.getWorkingDirectory();
out.println("DestinationPath path:"+workingDir);
Path hdfsDir = new Path(workingDir+"/servelets");
out.println("DestinationPath Directory:"+workingDir);
fs.mkdirs(hdfsDir);
out.println("Source path:"+localPath);
Path localFile = new Path(localPath);
Path newHdfsFile = new Path(hdfsDir+"/"+"ourTestFile1.txt");
out.println("Destination File path:"+hdfsDir+"/"+"ourTestFile1.txt");
fs.copyFromLocalFile(localFile, newHdfsFile);
/*******************************************************************
* *Approach 2
*
* Executing hadoop commands as string using runtime.exec()
* *****************************************************************
*/
String[] cmd = new String[] {"hadoop fs -copyFromLocal /home/asadgenx/filelist.txt /user/asad/myfile.txt"};
Process process = Runtime.getRuntime().exec(cmd);
out.println("File copied!!");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
方法一 HTTP状态500中的错误 -Mkdirs无法创建文件:/ var / lib / tomcat7 / servelets
错误在方法2中, HTTP状态500- 无法运行程序“ hadoop fs -copyFromLocal /home/asadgenx/filelist.txt /user/asad/myfile.txt”:错误= 2,没有这样的文件或目录
这里的任何Hadoop专家都可以帮我解决这个问题!!!!
希望回答您的问题还为时不晚。
首先,我将把问题的范围限定在要从tomcat servlet访问HDFS文件系统的位置。 我遇到了很多陷阱,并且阅读了很多论坛文章以了解它,而这更多地取决于如何设置所有内容。
要遵循方法2,您必须使用SecurityManager,而您不想这样做。
要遵循方法1,请查看此检查清单:
使适当的jar文件可用于您的Web应用程序。 我更喜欢为每个webapp放置jar,而不是通过tomcat使它们可用。 无论如何,您的Web应用程序应该可以访问以下jar文件列表(我没有命名jar版本,也许其中一些是多余的,我试图从运行Map Reduce作业的项目中删除该列表,然后得到结果):
它们位于hadoop分发中的许多目录中
确保您的网络配置正常。 测试您的hadoop服务是否已启动并正在运行,并且可以访问从Tomcat服务器到hadoop服务器的所有必需的主机和端口配置。 如果它们都位于同一服务器上,那就更好了。 尝试从tomcat服务器访问您的HDFS监视器( http:// hadoop-host:50070 )网页。
调整对要读取/写入的文件的访问权限:
一种。 在您的webapp中,您将只能访问位于webapp目录内的文件。
b。 从hadoop,您的Web应用将以用户“ tomcat”的身份连接。 确保用户tomcat具有读取或写入Hadoop DFS中所需文件的正确特权。
一切设置完成后,您可以在servlet中运行类似的内容:
//Set the root of the files I will work with in the local file system
String root = getServletContext().getRealPath("/") + "WEB-INF";
//Set the root of the files I will work with in Hadoop DFS
String hroot = "/home/tomcat/output/mrjob";
//Path to the files I will work with
String src = hroot + "/part-00000.avro";
String dest = root + "/classes/avro/result.avro";
//Open the HDFS file system
Configuration hdfsconf = new Configuration();
//Fake Address, replace with yours!
hdfsconf.set("fs.default.name", "hdfs://hadoop-host:54310");
hdfsconf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
hdfsconf.set("fs.file.impl", "org.apache.hadoop.fs.LocalFileSystem");
FileSystem hdfs = FileSystem.get(hdfsconf);
//Copy the result to local
hdfs.copyToLocalFile(new Path(src), new Path(dest));
//Delete result
hdfs.delete(new Path(hroot), true);
//Close the file system handler
hdfs.close();
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.