如何在R和Java中解决异常“eval failed,request status:error code:127”?

时间:2014-02-20 07:14:24

标签: r prediction java

我正在使用R和Java来显示预测。

我有5个小时的数据。我想预测四小时数据(相对于日期的内存)的5小时数据。 通过使用4小时的数据,我创建了新的集合,并在新的集合中插入了第5小时的预测数据。但我收到以下错误:

The Exception is eval failed, request status: error code: 127
org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127
at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:233)
at scheduler.scheduler.predictions.getPredictionsofData(predictions.java:45)
at pack.GetCollectionMultithreaded.getPredictionAndInsert(GetCollectionMultithreaded.java:386)
at pack.GetCollectionMultithreaded.runCustomerListAndPredictionEvery5Min(GetCollectionMultithreaded.java:155)
at pack.GetCollectionMultithreaded.main(GetCollectionMultithreaded.java:103)

这是代码:

public class predictions {

public void getPredictionsofData(DB dbObj){
 FileInputStream fis = null;

 DBCollection network_device_realtime = dbObj.getCollection("mycollectionname");
DBObject return_dobject = null;

// For Network device1 realtime
try{
 List<String> listOfIps = network_device_realtime.distinct("hostId");
 RConnection c = new RConnection(Rhost,Rport);
 c.eval("library(RMongo)");
 c.eval("library(plyr)");
 c.eval("library(randomForest)");
 c.eval(" db <- mongoDbConnect('demo','localhost',27017)");
 for( int i= 0 ;i<listOfIps.size(); i++){
     float my_predicted_date = 0 ;
             BasicDBObject criteria = new BasicDBObject();
     BasicDBObject projections = new BasicDBObject();
     criteria.put("hostId",listOfIps.get(i));
    projections.put("runtimeMillis", 1);
    DBCursor cursor = network_device_realtime.find(criteria,projections).sort(new BasicDBObject("runtimeMillis",-1)).limit(1);
    while(cursor.hasNext()) {   
        BasicDBObject obj = (BasicDBObject) cursor.next();
        my_predicted_date = (float) obj.getDouble("runtimeMillis");
    }
    // Set predict date for testing purpose 
     my_predicted_date = my_predicted_date-(4*60*60*1000);

    // for calculating predictions next 24 hrs
    for(int j = 1; j <= 12 ;j++){
            my_predicted_date = my_predicted_date+(300*1000);//j*60*60*1000calculating next hrs data
        System.out.println("Date Gen in network: " +my_predicted_date);

         c.eval("query <- dbGetQuery(db,'"+network_device_realtime+"','{\"hostId\":\""+listOfIps.get(i)+"\",\"cpuUtilization\":{\"$ne\":\"null\"},\"memoryUtilization\":{\"$ne\":\"NaN\"},\"runtimeMillis\":{\"$ne\":\"null\"}}')");

         c.eval("date <- query$runtimeMillis");
			 c.eval("host_id <- query$hostId");
         c.eval("cpu <-  query$cpuUtilization ");
			 c.eval("memory <- query$memoryutil");
         c.eval("all_data<-data.frame(cpu,date)");
         c.eval("training<- all_data");

         c.eval("rf_fit<-randomForest(memory~date,data=training)");
         c.eval("df <- data.frame(date="+my_predicted_date+ ")");
         c.eval("predictions<-predict(rf_fit,newdata=new)");
         REXP memory_predictions= c.eval("predictions");
         c.eval("rf_fit<-randomForest(cpu~date,data=training)");
         c.eval("df <- data.frame(date="+my_predicted_date+ ")");
         c.eval("predictions<-predict(rf_fit,newdata=new)");
         REXP cpu_predictions= c.eval("predictions");
         String json = ""; 
         json ="{\"memoryUtilization\":"+ memory_predictions + ",\"cpuUtilization\" : "+ cpu_predictions + ",\"hostId\" : \""+ listOfIps.get(i) + "\",\"runtimeMillis\":"+my_predicted_date+",\"deviceType\":\"snmp\"}";
         return_dobject=(DBObject) JSON.parse(json);
         dbObj.getCollection("prediction").insert(return_dobject);
        }
     }
         c.close();
    }
catch(Exception e){
     System.out.println("ERROR: In Connection to R ");
     System.out.println("The Exception is "+ e.getMessage());
     e.printStackTrace();
 }
}
}//class

在此代码中,我收到错误:

 c.eval("rf_fit<-randomForest(memory~date,data=training)");

如何解决此错误?

2 个答案:

答案 0 :(得分:4)

要获取正确的错误消息,请使用此代替简单的eval

REXP rResponseObject = rServeConnection.parseAndEval(
    "try(eval("+R_COMMAND_OR_SOURCE_FILE_PATH+"),silent=TRUE)");
if (rResponseObject.inherits("try-error")) {
  LOGGER.error("R Serve Eval Exception : "+rResponseObject.asString());
}

此记录器打印从R抛出的确切错误。

答案 1 :(得分:0)

此异常主要是由于语句中的数据

c.eval("rf_fit<-randomForest(memory~date,data=training)");

包含null。

这可能是由于数据框架中的错误造成的。 请检查一次。