使用Javaservlet在hdfs中创建目录

iyfjxgzm  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(365)

我正在构建一个可以从hdfs中创建和删除目录和文件的web应用程序,就像hadoopwebui一样。但是我在第一步就失败了。我甚至不能在hdfs中创建目录。

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.net.URI;

// Extend HttpServlet class
public class Saurab extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String hdfsUri = "hdfs://saurab:9000/";
        String dirName = "ekbana113";
        FileSystem hdfs = null;
        Configuration con = new Configuration();

        try {
            con.addResource(new Path("/home/saurab/hadoopec/hadoop/etc/hadoop/core-site.xml"));
            con.addResource(new Path("/home/saurab/hadoopec/hadoop/etc/hadoop/hdfs-site.xml"));

            hdfs = FileSystem.get(URI.create(hdfsUri), con); //hdfs is null, I don't know why ?
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            hdfs.mkdirs(new Path(hdfsUri + "/" + dirName)); // Null pointer exception
        } catch (IOException e) {
            e.printStackTrace();
        }
}
}

我遵循这个问题,我正在使用tomcat服务器来启动我的webapp。hadoop已经启动并运行。我创建了一个war文件并将其放入 /opt/tomcat/webapps/ . 后来我做到了 saurab:8080/servlet-name/mapper-url 这是正确的做法,还是我做错了?

csbfibhn

csbfibhn1#

您的servlet没有 @WebServlet 注解。你创造了什么 web.xml ? 如果没有,您应该添加 @WebServlet 或者创建web-inf/web.xml并在其中编写urlMap。此外,您应该访问 http://saurab:8080/warfile-name/mapped-url .
如果您的war文件名是test1.war @WebServlet(urlPatterns = { "/test2" }) 之前 public class Saurab extends HttpServlet { ,则可以访问 http://saurab:8080/test1/test2 .

相关问题