1

我编写了应用程序,它每 10 分钟从网站下载数据并写入文件。然后将这些文件合并到一个文件中,然后在此合并文件上运行 R 程序以执行情感分析,并将结果存储在 hbase 中。

我想要合并文件、运行 R 然后存储到 HBase 以在下载的数据上连续运行的过程。

为了运行 R,我们从 java 程序运行 R 脚本。我们使用 Runtime.getRuntime().exec() 方法运行 R 程序,但它不等待 R 程序完成并且下一行中的方法开始执行。使用 p.waitFor() 没有帮助。

下面是代码片段。

公共类 runRprogram {

public static String rOutputFile; 
public static HashMap catMap;
public static HashMap dtMap;
public static HashMap sentMap;

public static void main(String[] arg) throws IOException, InterruptedException{
    //runR("D:\\workspace\\Out100316.txt","D:\\workspace\\Clean_Out100316");

    cleanupTempFiles();
    mergeFiles();
    rRun();
    Thread.sleep(60000); 
    //sleep for 10 secs and give time for R program to finish

    rOutputFile = "D:\\TweetsData\\TweetsProcessed\\Out1004224944.txt";
    incrementHBaseCounts();

}

公共静态无效 rRun() 抛出 IOException {

        Formatter formatter = new Formatter();
        String execom = "C:\\Program Files\\R\\R-2.15.1\\bin\\i386\\Rscript.exe";
        String rpath = "D:\\workspace\\R_scripts\\TextMining.Funtion.R";
        String inputFile = "D:\\TData\\TTemp\\ConcatenatedFile.txt";
        rOutputFile = "D:\\TData\\TProcessed\\Out" + formatter.format("%1$tm%1$td%1$tH%1$tM%1$tS", new Date()) + ".txt";
        String[] command = {"cmd","/c",execom,rpath,inputFile,rOutputFile };
        Runtime.getRuntime().exec(command);
        //Process p = Runtime.getRuntime().exec(command); 
        //int status=p.waitFor();


        System.out.println("R - Program executed");

}

}

我应该使用什么方法进行合并然后运行 ​​R 并最终将结果存储在 Hbase 中?我应该使用计时器类吗?

4

0 回答 0