java - 从 servlet 调用映射作业时出错

标签 java hadoop servlets mapreduce

我是一个Hadoop爱好者,还在学习阶段,出于好奇尝试了一些东西,想做一个servlet调用hadoop job。我尝试了两种方法,但都失败了。等等,首先有人能告诉我这是否可行吗?如果是这样,请提供一些实时示例(不要告诉我 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

方法二的错误 HTTP 状态 500 - 无法运行程序“hadoop fs -copyFromLocal/home/asadgenx/filelist.txt/user/asad/myfile.txt”:error=2,没有这样的文件或目录

这里的任何 Hadoop 专家都可以帮我解决这个问题吗!!!

最佳答案

我希望现在回答您的问题还不算太晚。

首先,我会将问题的范围限定为从 tomcat servlet 访问 HDFS 文件系统,这正是您要尝试做的。我克服了很多陷阱,阅读了很多论坛帖子来克服它,更多的是你如何设置一切的问题。

要遵循方法 2,您应该必须处理 SecurityManager,而您不希望这样做。

要遵循方法 1,请查看此 list :

  1. 使您的网络应用程序可以访问适当的 jar 文件。我更喜欢为每个 webapp 放置 jar,而不是通过 tomcat 使它们可用。无论如何,您的 webapp 应该可以访问以下 jar 文件列表(我没有命名 jar 版本,也许其中一些是多余的,我试图从运行 Map Reduce 作业的项目中减少列表,然后得到结果):

    • hadoop-common
    • Guava
    • 公共(public)日志记录
    • commons-cli
    • log4j
    • 通用语言
    • 公共(public)配置
    • hadoop-auth
    • slf4j-log4j
    • slf4j-api
    • hadoop-hdfs
    • protobuf-java
    • htrace 核心

它们位于您的 hadoop 发行版中的许多目录中

  1. 确保您的网络配置正常。测试您的 hadoop 服务已启动并正在运行,并且您可以从 tomcat 服务器访问所有必需的主机和端口配置到 hadoop 服务器。如果它们都位于同一台服务器上,那就更好了。尝试从 tomcat 服务器访问您的 HDFS 监视器 ( http://hadoop-host:50070 ) 网页。

  2. 调整您将要读取/写入的文件的访问权限:

一个。从您的 webapp 中,您将只能访问位于 webapp 目录内的文件。

从 hadoop,您的 webapp 将作为用户“tomcat”连接。确保用户 tomcat 具有在 Hadoop DFS 中读取或写入预期文件的正确权限。

  1. 正如 Angus 所假设的,您的 Configuration 对象将为空。您需要在您的 servlet 中自行设置所需的配置参数。

一旦一切都设置好了,你可以在你的 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();

希望这对您有所帮助!

关于java - 从 servlet 调用映射作业时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33813900/

相关文章:

java - java中读写器的实现

java - 如何将 "add Maven nature"添加到现有的 netbeans 项目?

c++ - 用于 HTTP 编程的 C/C++ 库

java - 自定义 Tomcat 日志记录类需要 ServletContext

java - 如何使用 iText api 提取 PDF 水印内容

java - 我不断收到 NullPointerException 但找不到我犯错的地方

hadoop - 编译我的应用程序时出现问题

hadoop - 为什么有人会在 Tez 上运行 Spark/Flink?

python - PySpark 在 RDD 上运行多个函数

java - Servlets , JPA - 登录问题