在jsp中使用runtime.getruntime().exec()除copy(copyfromlocal)以外的所有命令都不起作用

pxyaymoc  于 2021-06-04  发布在  Hadoop
关注(0)|答案(1)|浏览(481)

我试图在下面的代码中将一个文件从本地存储复制到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>

甚至-mkdir、-rmr等也能完美地工作。我认为从根文件夹复制文件有问题。
我是否需要做更多的事情,特别是复制文件。
感谢您的帮助。

yshpjwxd

yshpjwxd1#

抱歉刚才无意中误导了你。这个问题实际上存在于hadoop的存储系统(hdfs)中,我认为这肯定是tomcat或jsp的问题。我想一发现这个问题就把它删掉,但后来我想这会对其他人有好处,因为我已经两天找不到任何东西了。
我发现这有助于我最终在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;

?>

相关问题