从servlet调用mapred作业时出错

时间:2015-11-19 20:36:48

标签: java hadoop servlets mapreduce

我是一名仍处于学习阶段的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

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

这里的任何一位Hadoop专家都可以帮助我解决这个问题吗,请

1 个答案:

答案 0 :(得分:2)

我希望回答你的问题还为时不晚。

首先,我将讨论范围从tomcat servlet访问HDFS文件系统,这是你想要做的。我遇到了很多陷阱,并阅读了很多论坛帖子来解决这个问题,更多的是你如何设置所有内容。

要遵循方法2,您必须处理SecurityManager,而您不想这样做。

要遵循方法1,请查看此检查清单:

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

    • Hadoop的共同
    • 番石榴
    • 共享记录
    • 公地-CLI
    • 的log4j
    • 公地琅
    • 公地构
    • 的hadoop-AUTH
    • SLF4J-log4j的
    • SLF4J-API
    • Hadoop的HDFS
    • 的protobuf-java的
    • HTRACE核
  2. 它们位于hadoop发行版的许多目录中

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

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

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

      湾从hadoop,您的webapp将以“tomcat”用户身份连接。确保用户tomcat具有在Hadoop DFS中读取或写入目标文件的正确权限。

      1. 正如Angus所假设的,您的Configuration对象将为空。您需要在servlet中自己设置所需的配置参数。
      2. 设置完所有内容后,您可以在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();
        

        希望这有帮助!