在jsp中使用Runtime.getRuntime()。exec()。除copy(copyFromLocal)之外的所有命令都不起作用

时间:2014-07-23 08:39:23

标签: jsp hadoop runtime.exec

我正在尝试使用以下代码将文件从本地存储复制到hdfs。如果我使用命令列出文件,即hadoop fs -ls Alert,但它在复制文件时不起作用,则可以正常工作。

这是我的代码。

<html><%@ page import="java.util.*,java.io.*"%>
<%@ page import="java.lang.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.nio.file.Files"%>
<%@ page import="java.nio.file.Paths"%>
<%@ page import="java.nio.file.attribute.PosixFilePermission"%>
<body>
<font color=black>
<%
   String jobId=request.getParameter("jobId");
   String jobStatus=request.getParameter("jobStatus");
   String displayName=request.getParameter("displayName");
   String name=request.getParameter("name");
   String description=request.getParameter("description");
   String frequency=request.getParameter("frequestuency");
   String lastModifiedAt=request.getParameter("lastModifiedAt");
   String createdAt=request.getParameter("createdAt");  
   String createdBy=request.getParameter("createdBy");      
   String opPath=request.getParameter("opPath");        
   String env=request.getParameter("env");  
   PrintWriter writer = new PrintWriter("/var/lib/tomcat7/webapps/ROOT/log.txt", "UTF-8");
   writer.println("Job ID : "+jobId);
   writer.println("Job Status : "+jobStatus);
   writer.println("Rule Name : "+name);
   writer.println("Rule Display Name : "+displayName);
   writer.println("Rule Description : "+description);
   writer.println("Rule env : "+env);
   writer.println("Rule frequency : "+frequency);
   writer.println("Rule last modified at : "+lastModifiedAt);
   writer.println("Rule created at : "+createdAt);
   writer.println("Rule created by : "+createdBy);
   writer.println("Notification Path : "+opPath);
   writer.close();

   Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
   perms.add(PosixFilePermission.OWNER_READ);
   perms.add(PosixFilePermission.OWNER_WRITE);
   perms.add(PosixFilePermission.OWNER_EXECUTE);
   perms.add(PosixFilePermission.GROUP_READ);
   perms.add(PosixFilePermission.GROUP_WRITE);
   perms.add(PosixFilePermission.GROUP_EXECUTE);
   perms.add(PosixFilePermission.OTHERS_READ);
   perms.add(PosixFilePermission.OTHERS_WRITE);
   perms.add(PosixFilePermission.OTHERS_EXECUTE);         
   Files.setPosixFilePermissions(Paths.get("/var/lib/tomcat7/webapps/ROOT/log.txt"), perms);
   Process p = Runtime.getRuntime().exec("/usr/local/hadoop/bin/hadoop fs -copyFromLocal /var/lib/tomcat7/webapps/ROOT/log.txt Alert/Notify");

   OutputStream os = p.getOutputStream();
   InputStream in = p.getInputStream();
   DataInputStream dis = new DataInputStream(in);
   String disr = dis.readLine();
   while ( disr != null ) 
   {
   out.println(disr);
   disr = dis.readLine();
   }
%>
</body>
</font>
</html>

Even -mkdir,-rmr等完美无缺。我认为从ROOT文件夹复制文件有一些问题。

我是否需要做更多的事情,特别是复制文件。

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

抱歉关于无意中误导了。问题实际上是在hadoop的存储系统(hdfs)中撒谎,我认为这绝对是tomcat或jsp的问题。我想在发现这个问题后立即删除这个问题,但后来我认为这会让其他人受益,因为我在2天内找不到任何东西。

我发现这最终帮助了我Permission denied at hdfs

-copyFromLocal或-put需要具有一些我认为的权限,只需在hdfs-site.xml中添加以下内容然后运行就可以了。

 <property>
    <name>dfs.permissions</name>
    <value>false</value>
  </property>

我也在php中尝试过相同的操作,它比jsp或servlet更麻烦。

这就是我最终在php中所做的事情

<?php
  if( $_GET["jobId"] || $_GET["jobStatus"] )
  {
     echo "Job Id ". $_GET['jobId']. "<br />";
     echo "Job Status ". $_GET['jobStatus']. "";
  }
  $jobId=$_GET["jobId"];
  $jobStatus=$_GET["jobStatus"];
  $displayName=$_GET["displayName"];
  $name=$_GET["name"];
  $description=$_GET["description"];
  $frequency=$_GET["frequestuency"];
  $lastModifiedAt=$_GET["lastModifiedAt"];
  $createdAt=$_GET["createdAt"];    
  $createdBy=$_GET["createdBy"];        
  $opPath=$_GET["opPath"];      
  $env=$_GET["env"];
  $file = fopen("log.txt","w");
  echo fwrite($file,"Job ID : ".$jobId."\n"."Job Status : ".$jobStatus."\n"."Display Name : ".$displayName."\n"."Name : "."\n"."Description : ".$description."\n"."Frequency : ".$frequency."\n"."Last Modified At".$lastModifiedAt."\n"."Created At".$createdAt."\n"."Created By : ".$createdBy);
  fclose($file);
  chmod("log.txt", 0777);
  $last_line = system('/usr/local/hadoop/bin/hadoop fs -put /var/www/html/log.txt /user/hduser/Alert/', $retval);
  $last_line = system('/usr/local/hadoop/bin/hadoop fs -ls /user/hduser/Alert/', $retval);
  echo '<pre>';
  $output2 = exec('hadoop fs -copyFromLocal /var/www/html/log.txt /user/hduser/Alert/Notify');
  echo '
    </pre>
    <hr />' . $last_line . '
    <hr />' . $retval;
        '<hr />' . $output1;

?>