[英]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.