Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Java代码运行Linux Hadoop fs命令_Java_Linux_Hadoop_Concatenation_Hdfs - Fatal编程技术网

从Java代码运行Linux Hadoop fs命令

从Java代码运行Linux Hadoop fs命令,java,linux,hadoop,concatenation,hdfs,Java,Linux,Hadoop,Concatenation,Hdfs,我正在尝试运行一个命令,从java代码2合并到文件! 命令是: hadoop fs -cat /user/clouder/Index_1/part-r-00000 /user/cloudera/Index_2/part-r-00000 | hadoop fs -put - /user/cloudera/mergedfile 该命令在Cloudera终端上运行得非常好,但当我从java代码中运行该命令时,它会在控制台上显示合并的内容,但不会在HDFS上的指定路径中创建合并文件。如果合并文件已存在

我正在尝试运行一个命令,从java代码2合并到文件! 命令是:

hadoop fs -cat /user/clouder/Index_1/part-r-00000 /user/cloudera/Index_2/part-r-00000 | hadoop fs -put - /user/cloudera/mergedfile
该命令在Cloudera终端上运行得非常好,但当我从java代码中运行该命令时,它会在控制台上显示合并的内容,但不会在HDFS上的指定路径中创建合并文件。如果合并文件已存在,则它输出文件的早期数据,但不输出新合并的数据;如果文件不存在,则它不创建新文件。其中,在终端上运行的上述命令创建新文件(如果不存在),否则会给出文件存在的错误

我的java代码如下:

process p;

try{

        p =Runtime.getRuntime().exec("hadoop fs -cat /user/cloudera/Index_1/part-r-00000 /user/cloudera/Index_2/part-r-00000 | hadoop fs -put - /user/cloudera/mergedfile");
        BufferredReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));

        while(s=br.readLine())!=null)
        {
            System.out.println(s);
        }
    }

catch(Exception e)
    {
        System.out.println(e.getMessage());
    }

我的目的是在存在现有文件时替换,或者在不存在java代码时创建新文件。

要使用java运行HDFS命令,应该使用。以下是如何使用它合并此文件的示例:

/** 
 * @param inputFiles a glob expression of the files to be merged
 * @param outputFile a destination file path
 * @param deleteSource delete source files after merging
 * @return
 * @throws IOException
 */
private static Path mergeTextFiles(String inputFiles,String outputFile,boolean deleteSource,boolean deleteDestinationFileIfExist) throws IOException {
  JobConf conf=new JobConf(FileMerger.class);
  FileSystem fs=FileSystem.get(conf);
  Path inputPath=new Path(inputFiles);
  Path outputPath=new Path(outputFile);
  if (deleteDestinationFileIfExist) {
    if (fs.exists(outputPath)) {
      fs.delete(outputPath,false);
      sLogger.info("Warning: remove destination file since it already exists...");
    }
  }
 else {
    Preconditions.checkArgument(!fs.exists(outputPath),new IOException("Destination file already exists..."));
  }
  FileUtil.copyMerge(fs,inputPath,fs,outputPath,deleteSource,conf,FILE_CONTENT_DELIMITER);
  sLogger.info("Successfully merge " + inputPath.toString() + " to "+ outputFile);
  return outputPath;
}
我认为在这种情况下,您需要先使用将要合并的文件复制到1个目录中。稍后,您将采用此类目录路径并将其作为inputFiles参数传递:

JobConf conf=new JobConf(FileMerger.class);
FileSystem fs=FileSystem.get(conf);
String tmpDir = "/user/cloudera/tmp_dir";
Path[] paths = {new Path("/user/clouder/Index_1/part-r-00000"), new Path("/user/clouder/Index_2/part-r-00000")};
Path pathToInputs = FileUtil.copy(fs, paths, fs, new Path(tmpDir));
mergeTextFiles(tmpDir, "/user/cloudera/mergedfile", false, true);

为什么不使用?谢谢Rosales,我也在研究相同的,以前从未在java中使用HDFSAPI!我现在就试试这个,谢谢你的代码。