How to run external program within mapper or reducer giving HDFS files as input and storing output files in HDFS?

后端 未结 2 1779
南旧
南旧 2021-01-24 12:10

I have a external program which take file as a input and give output file

     //for example 
     input file: IN_FILE
     output file: OUT_FILE

    //Run Exte         


        
相关标签:
2条回答
  • 2021-01-24 12:53

    So assuming that your external program doesnt know how to recognize or read from hdfs, then what you will want to do is load in the file from java and pass it as input directly to the program

    Path path = new Path("hdfs/path/to/input/file");
    FileSystem fs = FileSystem.get(configuration);
    FSDataInputStream fin = fs.open(path);
    ProcessBuilder pb = new ProcessBuilder("SHELL_SCRIPT");
    Process p = pb.start();
    OutputStream os = p.getOutputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(fin));
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
    
    String line = null;
    while ((line = br.readLine())!=null){
        writer.write(line);
    }
    

    The output can be done in the reverse manner. Get the InputStream from the process, and make a FSDataOutputStream to write to the hdfs.

    Essentially your program with these two things becomes an adapter that converts HDFS to input and output back into HDFS.

    0 讨论(0)
  • 2021-01-24 12:58

    You could emply Hadoop Streaming for that:

    $HADOOP_HOME/bin/hadoop jar $HADOOP_HOME/hadoop-streaming.jar \
    -input myInputDirs \
    -output myOutputDir \
    -mapper myPythonScript.py \
    -reducer /bin/wc \
    -file myPythonScript.py \
    -file myDictionary.txt
    

    See https://hadoop.apache.org/docs/r1.0.4/streaming.pdf for some examples.

    Also a nice article : http://princetonits.com/blog/technology/hadoop-mapreduce-streaming-using-bash-script/

    Hadoop streaming is a utility that comes with the Hadoop distribution. The utility allows you to create and run Map/Reduce jobs with any executable or script as the mapper and/or the reducer.

    Another example:

    $HADOOP_HOME/bin/hadoop  jar $HADOOP_HOME/hadoop-streaming.jar \
        -input myInputDirs \
        -output myOutputDir \
        -mapper /bin/cat \
        -reducer /bin/wc
    

    In the above example, both the mapper and the reducer are executables that read the input from stdin (line by line) and emit the output to stdout. The utility will create a Map/Reduce job, submit the job to an appropriate cluster, and monitor the progress of the job until it completes.

    When an executable is specified for mappers, each mapper task will launch the executable as a separate process when the mapper is initialized. As the mapper task runs, it converts its inputs into lines and feed the lines to the stdin of the process. In the meantime, the mapper collects the line oriented outputs from the stdout of the process and converts each line into a key/value pair, which is collected as the output of the mapper. By default, the prefix of a line up to the first tab character is the key and the rest of the line (excluding the tab character) will be the value. If there is no tab character in the line, then entire line is considered as key and the value is null. However, this can be customized, as discussed later.

    When an executable is specified for reducers, each reducer task will launch the executable as a separate process then the reducer is initialized. As the reducer task runs, it converts its input key/values pairs into lines and feeds the lines to the stdin of the process. In the meantime, the reducer collects the line oriented outputs from the stdout of the process, converts each line into a key/value pair, which is collected as the output of the reducer. By default, the prefix of a line up to the first tab character is the key and the rest of the line (excluding the tab character) is the value. However, this can be customized.

    0 讨论(0)
提交回复
热议问题