运行自定义java类时出错

时间:2013-10-11 11:40:20

标签: java hadoop apache-pig hdfs

我已经从目录中创建了一个序列文件,然后根据我想要的组给出了索引,以便我可以使用该索引创建组。然后将这些组逐个给予我的自定义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();
    }
}

0 个答案:

没有答案