运行自定义java类时出错

qoefvg9y  于 2021-06-03  发布在  Hadoop
关注(0)|答案(0)|浏览(204)

我在目录外创建了一个序列文件,然后根据我想要的组给出索引,这样我就可以使用该索引创建组。然后将这些组一个接一个地提供给我的定制java类,该类根据组中存在的文件提供信息。
我的问题是,有时它运行得很好,但有时会出现不同的错误,如空指针异常、未找到字段的数据类型。
问题可能是由于团队的规模。因为我正在创建基于文件夹的组,然后从自定义jar中的文件夹中获取信息。
那么我该如何解决这个问题呢?下面是我的java类代码:

public class OperateDirectory extends EvalFunc<DataBag>{
    public TupleFactory tupleFactory = TupleFactory.getInstance();
    public BagFactory bagFactory = BagFactory.getInstance();

    public DataBag exec(Tuple input) throws IOException{
        ArrayList<String> protoTuple = new ArrayList<>();
        DataBag dataBag = bagFactory.newDefaultBag();

        /* Create Directory */
        if(input == null)
            return dataBag;
        if(input.size() != 2)
            return dataBag;

        long id = (long)input.get(0);
        DataBag infoBag = (DataBag)input.get(1);
        Iterator<Tuple> it = infoBag.iterator();
        File dir = new File("/tmp/TestFolder"+id);
        if(dir.exists())
        {
            FileUtils.cleanDirectory(dir);
        }
        else
        {
            dir.mkdir();
        }

        while(it.hasNext())
        {
            Tuple file_details = (Tuple)it.next();
            if(file_details != null && file_details.size()==3)
            {
                String file_name = (String)file_details.get(1);
                BytesWritable file_contents = (BytesWritable)file_details.get(2);
                File f = new File(dir.getPath()+"/"+file_name);
                f.deleteOnExit();
                writeToFile(file_contents, f);
            }
        }

        /* Perform operation here */

        File f = new File("output"+id+".log");
        ProcessBuilder performProcess1 = new ProcessBuilder("processes/processor", dir.getPath(),f.getPath());
        Process process1 = performProcess1.start();

        try
        {
            process1.waitFor();
            if(f.exists() && f.length()>0)
            {
               ProcessBuilder performProcess2 = new ProcessBuilder("perl", "scripts/ParseFile.pl", f.getPath());
               Process process2 = performProcess2.start();

               InputStream is = process2.getInputStream();
               InputStreamReader isr = new InputStreamReader(is);
               BufferedReader br = new BufferedReader(isr);
               String line;

               while ((line = br.readLine()) != null)
               {
                   if(!line.isEmpty())
                   {
                       String [] tmpArray = line.split(",");
                       if(tmpArray.length == 2)
                       {
                           protoTuple.clear();
                           protoTuple.add(tmpArray[0]);
                           protoTuple.add(tmpArray[1]);
                           dataBag.add(tupleFactory.newTuple(protoTuple));
                       }
                   }
               }
            }
            else
            {
                protoTuple.clear();
                protoTuple.add("Error");
                protoTuple.add("File "+f.getPath()+" does not exists ");
                dataBag.add(tupleFactory.newTuple(protoTuple));
            }
        }
        catch(Exception e)
        {
            protoTuple.clear();
            protoTuple.add("Error ");
            protoTuple.add(e.getMessage());
            dataBag.add(tupleFactory.newTuple(protoTuple));
        }

        try
        {
            FileUtils.cleanDirectory(dir);
            FileUtils.deleteDirectory(dir);
        }
        catch(Exception e)
        {
        }
        return dataBag;
    }
    void writeToFile(BytesWritable value, File binaryFile) throws IOException{
        FileOutputStream fileOut = new FileOutputStream(binaryFile);
    fileOut.write(value.getBytes(), 0, value.getLength());
    fileOut.close();
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题